示例#1
0
    def conversion_unit(self) -> TemperatureUnit:
        if self.__conversion_unit:
            return self.__conversion_unit

        if settings.AVERAGE_CONVERSION_UNIT == 'C':
            self.__conversion_unit = TemperatureUnit.make_celsius()

        if settings.AVERAGE_CONVERSION_UNIT == 'F':
            self.__conversion_unit = TemperatureUnit.make_fahrenheit()

        if not self.__conversion_unit:
            import pdb
            pdb.set_trace()
            raise Exception('Conversion unit not configured !!')

        return self.__conversion_unit
示例#2
0
 def some_services_test(self):
     message = FindAverageTemperatureTest(
         GeoLocation(12.1213123, -12.1231231), {'five', 'four'},
         TemperatureUnit.make_celsius())
     result = self.handler.handle(message)
     self.assertIsInstance(result, AverageTemperature)
     self.assertEqual(4.5, result.average.value)
示例#3
0
 def get_temperature(self, location: GeoLocation) -> Temperature:
     '''
     this last service retrive the result on fahrenheit temp unit, but the
     AverageTemperature primitive converts to a single unit begore to
     resolve the average:
     (5 celsius = 41 fahrenheit)
     '''
     return Temperature('five', 41, TemperatureUnit.make_fahrenheit())
    def get_temperature(self, location: GeoLocation) -> Temperature:
        values = {'latlon': f"{location.latitude},{location.longitude}"}
        url_params = parse.urlencode(values)
        url_endpoint = '%s/noaa' % self.__BASE_URL

        with request.urlopen(f"{url_endpoint}?{url_params}") as response:
            data = json.load(response)
            return Temperature(
                'noaa',
                float(data["today"]["current"]["celsius"]),
                TemperatureUnit.make_celsius(
                )  # TODO: change this implementation after pass the tests
            )
    def get_temperature(self, location: GeoLocation) -> Temperature:
        values = {
            'lat': location.latitude,
            'lon': location.longitude,
        }
        payload = json.dumps(values).encode('ascii')
        url_endpoint = '%s/weatherdotcom' % self.__BASE_URL
        weather_request = request.Request(url_endpoint, payload)
        weather_request.add_header('Content-Type', 'application/json')

        with request.urlopen(weather_request) as response:
            data = json.load(response)
            return Temperature(
                'weatherdotcom',
                float(
                    data['query']['results']['channel']['condition']['temp']),
                TemperatureUnit.make_fahrenheit())
示例#6
0
    def get_temperature(self, location: GeoLocation) -> Temperature:
        values = {
            'latitude': location.latitude,
            'longitude': location.longitude,
        }
        url_params = parse.urlencode(values)
        url_endpoint = '%s/accuweather' % self.__BASE_URL

        with request.urlopen(f"{url_endpoint}?{url_params}") as response:
            data = json.load(response)
            return Temperature(
                'accuweather',
                float(data["simpleforecast"]["forecastday"][0]["current"]
                      ["celsius"]),
                TemperatureUnit.make_celsius(
                )  # TODO: reimplement after that the tests pass
            )
示例#7
0
 def none_location_test(self):
     message = FindAverageTemperatureTest(None, {'five', 'four', 'three'},
                                          TemperatureUnit.make_celsius())
     self.assertRaises(NoneLocationProvided, self.handler.handle, (message))
示例#8
0
 def none_average_temperature_test(self):
     message = FindAverageTemperatureTest(
         GeoLocation(12.1213123, -12.1231231), {},
         TemperatureUnit.make_celsius())
     self.assertRaises(NoneWeatherServicesProvided, self.handler.handle,
                       (message))
示例#9
0
 def get_temperature(self, location: GeoLocation) -> Temperature:
     return Temperature('four', 4, TemperatureUnit.make_celsius())
示例#10
0
 def get_temperature(self, location: GeoLocation) -> Temperature:
     return Temperature('three', 3, TemperatureUnit.make_celsius())