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_timeformat(self):
unixtime = 1378459200
iso = "2013-09-06 09:20:00+00"
date = datetime(2013, 9, 6, 9, 20, 0, tzinfo=timeformatutils.UTC())
self.assertEqual(unixtime, timeformatutils.timeformat(unixtime, "unix"))
self.assertEqual(iso, timeformatutils.timeformat(unixtime, "iso"))
self.assertEqual(date, timeformatutils.timeformat(unixtime, "date"))
self.assertEqual(unixtime, timeformatutils.timeformat(iso, "unix"))
self.assertEqual(iso, timeformatutils.timeformat(iso, "iso"))
self.assertEqual(date, timeformatutils.timeformat(iso, "date"))
self.assertEqual(unixtime, timeformatutils.timeformat(date, "unix"))
self.assertEqual(iso, timeformatutils.timeformat(date, "iso"))
self.assertEqual(date, timeformatutils.timeformat(date, "date"))
def test_trim_to(self):
ts = timeformatutils.to_date(1463041620) # 2016-05-12T08:27:00Z
self.assertEquals(self.__instance._trim_to(ts, 'minute'),
'2016-05-12T08:27Z')
self.assertEquals(self.__instance._trim_to(ts, 'hour'),
'2016-05-12T08Z')
self.assertEquals(self.__instance._trim_to(ts, 'day'),
'2016-05-12Z')
self.assertEquals(self.__instance._trim_to(ts, 'month'),
'2016-05Z')
self.assertEquals(self.__instance._trim_to(ts, 'year'),
'2016Z')
self.assertRaises(ValueError, self.__instance._trim_to,
ts, 'abcdef')
def test_get_so2(self):
# case: current SO2 index
params = {'lon': 8.25, 'lat': 43.75, 'start': None, 'interval': None}
def mock_func(uri, params=None, headers=None):
return 200, uri
self.__instance._client.cacheable_get_json = mock_func
expected_url = 'http://api.openweathermap.org/pollution/v1/so2/43.75,8.25/current.json?APPID=xyz'
result = self.__instance.get_so2(params)
self.assertEqual(expected_url, result)
ts = timeformatutils.to_date(1463041620) # 2016-05-12T08:27:00Z
# case: no interval specified
params = {'lon': 8.25, 'lat': 43.75, 'start': ts, 'interval': None}
expected_url = 'http://api.openweathermap.org/pollution/v1/so2/43.75,8.25/2016Z.json?APPID=xyz'
result = self.__instance.get_so2(params)
self.assertEqual(expected_url, result)
# case: 'minute' specified
params = {'lon': 8.25, 'lat': 43.75, 'start': ts, 'interval': 'minute'}
expected_url = 'http://api.openweathermap.org/pollution/v1/so2/43.75,8.25/2016-05-12T08:27Z.json?APPID=xyz'
result = self.__instance.get_so2(params)
self.assertEqual(expected_url, result)
# case: 'hour' specified
params = {'lon': 8.25, 'lat': 43.75, 'start': ts, 'interval': 'hour'}
expected_url = 'http://api.openweathermap.org/pollution/v1/so2/43.75,8.25/2016-05-12T08Z.json?APPID=xyz'
def get_reception_time(self, timeformat='unix'):
"""
Returns the GMT time telling when the observation has been received
from the OWM web API
:param timeformat: the format for the time value. May be:
'*unix*' (default) for UNIX time
'*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00``
'*date* for ``datetime.datetime`` object instance
:type timeformat: str
:returns: an int or a str
:raises: ValueError when negative values are provided
"""
return timeformatutils.timeformat(self._reception_time, timeformat)
def get_reception_time(self, timeformat='unix'):
"""Returns the GMT time telling when the meteostation history data was
received from the OWM web API
:param timeformat: the format for the time value. May be:
'*unix*' (default) for UNIX time
'*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00``
'*date* for ``datetime.datetime`` object instance
:type timeformat: str
:returns: an int or a str
:raises: ValueError
"""
return timeformatutils.timeformat(self._reception_time, timeformat)
def get_reception_time(self, timeformat='unix'):
"""
Returns the GMT time telling when the UV has been received from the API
:param timeformat: the format for the time value. May be:
'*unix*' (default) for UNIX time
'*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00``
'*date* for ``datetime.datetime`` object instance
:type timeformat: str
:returns: an int or a str
:raises: ValueError when negative values are provided
"""
return timeformatutils.timeformat(self._reception_time, timeformat)
def _will_be(self, timeobject, weather_condition):
"""
Tells if at the specified weather condition will occur at the specified
time. The check is performed on the *Weather* item of the forecast
which is closest to the time value conveyed by the parameter
:param timeobject: may be a UNIX time, a ``datetime.datetime`` object
or an ISO8601-formatted string in the format
``YYYY-MM-DD HH:MM:SS+00``
:type timeobject: long/int, ``datetime.datetime`` or str)
:param weather_condition: the weather condition to be looked up
:type weather_condition: str
:returns: boolean
"""
time_value = timeformatutils.to_UNIXtime(timeobject)
closest_weather = weatherutils.find_closest_weather(
self._forecast.get_weathers(),
time_value)
return weatherutils.status_is(closest_weather, weather_condition,
weather_code_registry)
def __repr__(self):
return "<%s.%s - id=%s, trigger id=%s, last update=%s>" % (
__name__,
self.__class__.__name__,
self.id,
self.trigger_id,
timeformatutils.to_ISO8601(self.last_update))
"""
Returns the GMT time of the end of the forecast coverage, which is the
time of the most recent *Weather* item in the forecast
:param timeformat: the format for the time value. May be:
'*unix*' (default) for UNIX time
'*iso*' for ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00``
'*date* for ``datetime.datetime`` object instance
:type timeformat: str
:returns: a long or a str
:raises: *ValueError* when invalid time format values are provided
"""
end_coverage = max([item.get_reference_time() \
for item in self._forecast])
return timeformatutils.timeformat(end_coverage, timeformat)
:type end: int, ``datetime.datetime`` or ISO8601-formatted string
:param conditions: the `Condition` objects representing the set of checks to be done on weather variables
:type conditions: list of `pyowm.utils.alertapi30.Condition` instances
:param area: the geographic are over which conditions are checked: it can be composed by multiple geoJSON types
:type area: list of geoJSON types
:param alert_channels: the alert channels through which alerts originating from this `Trigger` can be consumed.
Defaults to OWM API polling
:type alert_channels: list of `pyowm.utils.alertapi30.AlertChannel` instances
:returns: a *Trigger* instance
:raises: *ValueError* when start or end epochs are `None` or when end precedes start or when conditions or area
are empty collections
"""
assert start is not None
assert end is not None
unix_start = timeformatutils.to_UNIXtime(start)
unix_end = timeformatutils.to_UNIXtime(end)
if unix_start >= unix_end:
raise ValueError("Error: the start epoch must precede the end epoch")
the_time_period = {
"start": {
"expression": "exact",
"amount": unix_start
},
"end": {
"expression": "exact",
"amount": unix_end
}
}
assert conditions is not None
if len(conditions) == 0:
raise ValueError('A trigger must contain at least one condition: you provided none')