def test_from_dict(self): expected = Condition(WeatherParametersEnum.TEMPERATURE, OperatorsEnum.GREATER_THAN, 78.6, id='123456') the_dict = dict(name='temp', expression='$gt', amount=78.6, _id='123456') result = Condition.from_dict(the_dict) self.assertEqual(expected.weather_param, result.weather_param) self.assertEqual(expected.operator, result.operator) self.assertEqual(expected.amount, result.amount) self.assertEqual(expected.id, result.id) with self.assertRaises(pyowm.commons.exceptions.ParseAPIResponseError): Condition.from_dict(None) with self.assertRaises(pyowm.commons.exceptions.ParseAPIResponseError): Condition.from_dict(dict(nonexistent='key'))
def parse_JSON(self, JSON_string): """ Parses a `pyowm.alertapi30.alert.Alert` instance out of raw JSON data. :param JSON_string: a raw JSON string :type JSON_string: str :return: a `pyowm.alertapi30.alert.Alert` instance or ``None`` if no data is available :raises: *ParseResponseError* if it is impossible to find or parse the data needed to build the result """ if JSON_string is None: raise parse_response_error.ParseResponseError('JSON data is None') d = json.loads(JSON_string) try: alert_id = d['_id'] t = d['last_update'].split('.')[0].replace('T', ' ') + '+00' alert_last_update = timeformatutils._ISO8601_to_UNIXtime(t) alert_trigger_id = d['triggerId'] alert_met_conds = [ dict(current_value=c['current_value']['min'], condition=Condition.from_dict(c['condition'])) for c in d['conditions'] ] alert_coords = d['coordinates'] return Alert(alert_id, alert_trigger_id, alert_met_conds, alert_coords, last_update=alert_last_update) except ValueError as e: raise parse_response_error.ParseResponseError('Impossible to parse JSON: %s' % e) except KeyError as e: raise parse_response_error.ParseResponseError('Impossible to parse JSON: %s' % e)
def from_dict(cls, the_dict): """ Parses a *Alert* instance out of a data dictionary. Only certain properties of the data dictionary are used: if these properties are not found or cannot be parsed, an exception is issued. :param the_dict: the input dictionary :type the_dict: `dict` :returns: a *Alert* instance or ``None`` if no data is available :raises: *ParseAPIResponseError* if it is impossible to find or parse the data needed to build the result """ if the_dict is None: raise exceptions.ParseAPIResponseError('Data is None') try: alert_id = the_dict['_id'] t = the_dict['last_update'].split('.')[0].replace('T', ' ') + '+00' alert_last_update = formatting.ISO8601_to_UNIXtime(t) alert_trigger_id = the_dict['triggerId'] alert_met_conds = [ dict(current_value=c['current_value']['min'], condition=Condition.from_dict(c['condition'])) for c in the_dict['conditions'] ] alert_coords = the_dict['coordinates'] return Alert(alert_id, alert_trigger_id, alert_met_conds, alert_coords, last_update=alert_last_update) except ValueError as e: raise exceptions.ParseAPIResponseError( 'Impossible to parse JSON: %s' % e) except KeyError as e: raise exceptions.ParseAPIResponseError( 'Impossible to parse JSON: %s' % e)
def test_from_dict(self): expected = Condition(WeatherParametersEnum.TEMPERATURE, OperatorsEnum.GREATER_THAN, 78.6, id='123456') the_dict = dict(name='temp', expression='$gt', amount=78.6, _id='123456') result = Condition.from_dict(the_dict) self.assertEqual(expected.weather_param, result.weather_param) self.assertEqual(expected.operator, result.operator) self.assertEqual(expected.amount, result.amount) self.assertEqual(expected.id, result.id)
def from_dict(cls, the_dict): if the_dict is None: raise pyowm.commons.exceptions.ParseAPIResponseError( 'Data is None') try: # trigger id trigger_id = the_dict.get('_id', None) # start timestamp start_dict = the_dict['time_period']['start'] expr = start_dict['expression'] if expr != 'after': raise ValueError( 'Invalid time expression: "%s" on start timestamp. Only: "after" is supported' % expr) start = start_dict['amount'] # end timestamp end_dict = the_dict['time_period']['end'] expr = end_dict['expression'] if expr != 'after': raise ValueError( 'Invalid time expression: "%s" on end timestamp. Only: "after" is supported' % expr) end = end_dict['amount'] # conditions conditions = [ Condition.from_dict(c) for c in the_dict['conditions'] ] # alerts alerts_dict = the_dict['alerts'] alerts = list() for key in alerts_dict: alert_id = key alert_data = alerts_dict[alert_id] alert_last_update = alert_data['last_update'] alert_met_conds = [] for c in alert_data['conditions']: if isinstance(c['current_value'], int): cv = c['current_value'] else: cv = c['current_value']['min'] item = dict(current_value=cv, condition=Condition.from_dict(c['condition'])) alert_met_conds.append(item) alert_coords = alert_data['coordinates'] alert = Alert(alert_id, trigger_id, alert_met_conds, alert_coords, last_update=alert_last_update) alerts.append(alert) # area area_list = the_dict['area'] area = [GeometryBuilder.build(a_dict) for a_dict in area_list] # alert channels alert_channels = None # defaulting except ValueError as e: raise pyowm.commons.exceptions.ParseAPIResponseError( 'Impossible to parse JSON: %s' % e) except KeyError as e: raise pyowm.commons.exceptions.ParseAPIResponseError( 'Impossible to parse JSON: %s' % e) return Trigger(start, end, conditions, area=area, alerts=alerts, alert_channels=alert_channels, id=trigger_id)
def parse_JSON(self, JSON_string): """ Parses a `pyowm.alertapi30.trigger.Trigger` instance out of raw JSON data. As per OWM documentation, start and end times are expressed with respect to the moment when you create/update the Trigger. By design, PyOWM will only allow users to specify *absolute* datetimes - which is, with the `exact` expression - for start/end timestamps (will otherwise result in a `ParseResponseError` be raised) :param JSON_string: a raw JSON string :type JSON_string: str :return: a `pyowm.alertapi30.trigger.Trigger` instance or ``None`` if no data is available :raises: *ParseResponseError* if it is impossible to find or parse the data needed to build the result """ if JSON_string is None: raise parse_response_error.ParseResponseError('JSON data is None') d = json.loads(JSON_string) try: # trigger id trigger_id = d.get('_id', None) # start timestamp start_dict = d['time_period']['start'] expr = start_dict['expression'] if expr != 'after': raise ValueError('Invalid time expression: "%s" on start timestamp. Only: "after" is supported' % expr) start = start_dict['amount'] # end timestamp end_dict = d['time_period']['end'] expr = end_dict['expression'] if expr != 'after': raise ValueError('Invalid time expression: "%s" on end timestamp. Only: "after" is supported' % expr) end = end_dict['amount'] # conditions conditions = [Condition.from_dict(c) for c in d['conditions']] # alerts alerts_dict = d['alerts'] alerts = list() for key in alerts_dict: alert_id = key alert_data = alerts_dict[alert_id] alert_last_update = alert_data['last_update'] alert_met_conds = [ dict(current_value=c['current_value']['min'], condition=Condition.from_dict(c['condition'])) for c in alert_data['conditions'] ] alert_coords = alert_data['coordinates'] alert = Alert(alert_id, trigger_id, alert_met_conds, alert_coords, last_update=alert_last_update) alerts.append(alert) # area area_list = d['area'] area = [GeometryBuilder.build(a_dict) for a_dict in area_list] # alert channels alert_channels = None # defaulting except ValueError as e: raise parse_response_error.ParseResponseError('Impossible to parse JSON: %s' % e) except KeyError as e: raise parse_response_error.ParseResponseError('Impossible to parse JSON: %s' % e) return Trigger(start, end, conditions, area=area, alerts=alerts, alert_channels=alert_channels, id=trigger_id)