Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_test_from_dict_fails_when_JSON_data_is_None(self):
self.assertRaises(ParseAPIResponseError, NO2Index.from_dict, None)
def test_parse_JSON_fails_with_malformed_JSON_data(self):
self.assertRaises(ParseAPIResponseError, SO2Index.from_dict, json.loads(SO2INDEX_MALFORMED_JSON))
def test_from_dict_fails_when_JSON_data_is_None(self):
self.assertRaises(pyowm.commons.exceptions.ParseAPIResponseError, Ozone.from_dict, None)
def test_from_dict_fails_with_malformed_JSON_data(self):
self.assertRaises(pyowm.commons.exceptions.ParseAPIResponseError, Ozone.from_dict, json.loads(OZONE_MALFORMED_JSON))
def test_parse_JSON_fails_when_JSON_data_is_None(self):
self.assertRaises(ParseAPIResponseError, SO2Index.from_dict, None)
def from_dict(cls, the_dict):
"""
Parses an *Ozone* 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: an *Ozone* 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:
# -- reference time (strip away Z and T on ISO8601 format)
ref_t = the_dict['time'].replace('Z', '+00').replace('T', ' ')
reference_time = formatting.ISO8601_to_UNIXtime(ref_t)
# -- reception time (now)
reception_time = timestamps.now('unix')
# -- location
lon = float(the_dict['location']['longitude'])
lat = float(the_dict['location']['latitude'])
place = location.Location(None, lon, lat, None)
# -- ozone Dobson Units value
du = the_dict['data']
if du is not None:
# -- reception time (now)
reception_time = timestamps.now('unix')
# -- location
lon = float(the_dict['location']['longitude'])
lat = float(the_dict['location']['latitude'])
place = location.Location(None, lon, lat, None)
# -- CO samples
no2_samples = [dict(label=key,
precision=the_dict['data'][key]['precision'],
value=the_dict['data'][key]['value']) for key in the_dict['data']]
except KeyError:
raise exceptions.ParseAPIResponseError(
''.join([__name__, ': impossible to parse NO2Index']))
return NO2Index(reference_time, place, None, no2_samples, reception_time)
def from_dict(cls, the_dict):
"""
Parses a *COIndex* 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 *COIndex* 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:
# -- reference time (strip away Z and T on ISO8601 format)
t = the_dict['time'].replace('Z', '+00').replace('T', ' ')
reference_time = formatting.ISO8601_to_UNIXtime(t)
# -- reception time (now)
reception_time = timestamps.now('unix')
# -- location
lon = float(the_dict['location']['longitude'])
lat = float(the_dict['location']['latitude'])
place = location.Location(None, lon, lat, None)
# -- CO samples
co_samples = the_dict['data']
def from_dict(cls, the_dict):
"""
Parses an *AggregatedMeasurement* 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: an *AggregatedMeasurement* 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')
station_id = the_dict.get('station_id', None)
ts = the_dict.get('date', None)
if ts is not None:
ts = int(ts)
aggregated_on = the_dict.get('type', None)
temp = the_dict.get('temp', dict())
humidity = the_dict.get('humidity', dict())
wind = the_dict.get('wind', dict())
pressure = the_dict.get('pressure', dict())
precipitation = the_dict.get('precipitation', dict())
return AggregatedMeasurement(station_id, ts, aggregated_on, temp=temp, humidity=humidity, wind=wind,
pressure=pressure, precipitation=precipitation)
def from_dict(cls, the_dict):
"""
Parses a *SO2Index* 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 *SO2Index* 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:
# -- reference time (strip away Z and T on ISO8601 format)
t = the_dict['time'].replace('Z', '+00').replace('T', ' ')
reference_time = formatting.ISO8601_to_UNIXtime(t)
# -- reception time (now)
reception_time = timestamps.now('unix')
# -- location
lon = float(the_dict['location']['longitude'])
lat = float(the_dict['location']['latitude'])
place = location.Location(None, lon, lat, None)
# -- SO2 samples
so2_samples = the_dict['data']