Esempio n. 1
0
 def test_to_date(self):
     unixtime = 1378459200
     iso = "2013-09-06 09:20:00+00:00"
     date = datetime(2013, 9, 6, 9, 20, 0, 0, timezone.utc)
     self.assertEqual(date, formatting.to_date(unixtime))
     self.assertEqual(date, formatting.to_date(iso))
     self.assertEqual(date, formatting.to_date(date))
Esempio n. 2
0
    def get_no2(self, params_dict):
        """
        Invokes the NO2 Index endpoint

        :param params_dict: dict of parameters
        :returns: a string containing raw JSON data
        :raises: *ValueError*, *APIRequestError*

        """
        lat = str(params_dict['lat'])
        lon = str(params_dict['lon'])
        start = params_dict['start']
        interval = params_dict['interval']

        # build request URL
        if start is None:
            timeref = 'current'
        else:
            if interval is None:
                timeref = self._trim_to(formatting.to_date(start), 'year')
            else:
                timeref = self._trim_to(formatting.to_date(start), interval)

        uri = '%s/%s,%s/%s.json' % (NO2_INDEX_URL, lat, lon, timeref)
        _, json_data = self._client.get_json(uri)
        return json_data
Esempio n. 3
0
def min_difference_night_morn_temp():
    temp_differences = []
    for i in range(8):
        temp_differences.append(
            abs(weather_data.forecast_daily[i].temperature(
                'celsius')['night'] -
                weather_data.forecast_daily[i].temperature('celsius')['morn']))

    index_of_necessary_day = temp_differences.index(min(temp_differences))
    exact_date = to_date(
        weather_data.forecast_daily[index_of_necessary_day].ref_time)
    # date without hh:mm:ss+tz
    date = datetime.date(exact_date.year, exact_date.month, exact_date.day)

    return_data = {
        'date':
        date,
        'night_temp':
        weather_data.forecast_daily[index_of_necessary_day].temperature(
            'celsius')['night'],
        'morn_temp':
        weather_data.forecast_daily[index_of_necessary_day].temperature(
            'celsius')['morn']
    }
    return return_data
 def test_trim_to(self):
     ts = formatting.to_date(1463041620)  # 2016-05-12T08:27:00Z
     self.assertEqual(self.__instance._trim_to(ts, 'minute'),
                      '2016-05-12T08:27Z')
     self.assertEqual(self.__instance._trim_to(ts, 'hour'),
                      '2016-05-12T08Z')
     self.assertEqual(self.__instance._trim_to(ts, 'day'), '2016-05-12Z')
     self.assertEqual(self.__instance._trim_to(ts, 'month'), '2016-05Z')
     self.assertEqual(self.__instance._trim_to(ts, 'year'), '2016Z')
     self.assertRaises(ValueError, self.__instance._trim_to, ts, 'abcdef')
Esempio n. 5
0
    def test_creation_time(self):
        buf = Buffer(self.station_id)
        ts = buf.creation_time()
        iso_result = buf.creation_time(timeformat='iso')
        self.assertEqual(to_ISO8601(ts), iso_result)
        date_result = buf.creation_time(timeformat='date')
        self.assertEqual(to_date(ts), date_result)
        with self.assertRaises(ValueError):
            buf.creation_time(timeformat='unknown')

        buf.created_at = None
        self.assertIsNone(buf.creation_time())
Esempio n. 6
0
    def test_get_coi(self):

        # case: current CO 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.get_json = mock_func

        expected_url = 'co/43.75,8.25/current.json'
        result = self.__instance.get_coi(params)
        self.assertEqual(expected_url, result)

        ts = formatting.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 = 'co/43.75,8.25/2016Z.json'
        result = self.__instance.get_coi(params)
        self.assertEqual(expected_url, result)

        # case: 'minute' specified
        params = {'lon': 8.25, 'lat': 43.75, 'start': ts, 'interval': 'minute'}
        expected_url = 'co/43.75,8.25/2016-05-12T08:27Z.json'
        result = self.__instance.get_coi(params)
        self.assertEqual(expected_url, result)

        # case: 'hour' specified
        params = {'lon': 8.25, 'lat': 43.75, 'start': ts, 'interval': 'hour'}
        expected_url = 'co/43.75,8.25/2016-05-12T08Z.json'
        result = self.__instance.get_coi(params)
        self.assertEqual(expected_url, result)

        # case: 'day' specified
        params = {'lon': 8.25, 'lat': 43.75, 'start': ts, 'interval': 'day'}
        expected_url = 'co/43.75,8.25/2016-05-12Z.json'
        result = self.__instance.get_coi(params)
        self.assertEqual(expected_url, result)

        # case: 'month' specified
        params = {'lon': 8.25, 'lat': 43.75, 'start': ts, 'interval': 'month'}
        expected_url = 'co/43.75,8.25/2016-05Z.json'
        result = self.__instance.get_coi(params)
        self.assertEqual(expected_url, result)

        # case: 'year' specified
        params = {'lon': 8.25, 'lat': 43.75, 'start': ts, 'interval': 'year'}
        expected_url = 'co/43.75,8.25/2016Z.json'
        result = self.__instance.get_coi(params)
        self.assertEqual(expected_url, result)