Exemple #1
0
 def test_from_dictionary(self):
     dict1 = {"coord": {"lon": -0.12574, "lat": 51.50853}, "id": 2643743,
              "name": "London", "cnt": 9}
     dict2 = {"city": {"coord": {"lat": 51.50853, "lon": -0.125739},
              "country": "GB", "id": 2643743, "name": "London",
              "population": 1000000}
             }
     dict3 = {"station":{"coord":{"lon":-90.47,"lat":39.38}}}
     result1 = location_from_dictionary(dict1)
     result2 = location_from_dictionary(dict2)
     result3 = location_from_dictionary(dict3)
     self.assertTrue(isinstance(result1, Location))
     self.assertTrue(isinstance(result2, Location))
     self.assertFalse(result1.get_country() is not None)
     self.assertTrue(result1.get_ID() is not None)
     self.assertTrue(result1.get_lat() is not None)
     self.assertTrue(result1.get_lon() is not None)
     self.assertTrue(result1.get_name() is not None)
     self.assertTrue(result2.get_country() is not None)
     self.assertTrue(result2.get_ID() is not None)
     self.assertTrue(result2.get_lat() is not None)
     self.assertTrue(result2.get_lon() is not None)
     self.assertTrue(result2.get_name() is not None)
     self.assertTrue(result3.get_lat() is not None)
     self.assertTrue(result3.get_lon() is not None)
     self.assertTrue(result3.get_country() is None)
     self.assertTrue(result3.get_name() is None)
     self.assertTrue(result3.get_ID() is None)
Exemple #2
0
 def test_from_dictionary(self):
     dict1 = {"coord": {"lon": -0.12574, "lat": 51.50853}, "id": 2643743,
              "name": "London", "cnt": 9}
     dict2 = {"city": {"coord": {"lat": 51.50853, "lon": -0.125739},
              "country": "GB", "id": 2643743, "name": "London",
              "population": 1000000}
             }
     dict3 = {"station":{"coord":{"lon":-90.47,"lat":39.38}}}
     result1 = location_from_dictionary(dict1)
     result2 = location_from_dictionary(dict2)
     result3 = location_from_dictionary(dict3)
     self.assertTrue(isinstance(result1, Location))
     self.assertTrue(isinstance(result2, Location))
     self.assertFalse(result1.get_country() is not None)
     self.assertTrue(result1.get_ID() is not None)
     self.assertTrue(result1.get_lat() is not None)
     self.assertTrue(result1.get_lon() is not None)
     self.assertTrue(result1.get_name() is not None)
     self.assertTrue(result2.get_country() is not None)
     self.assertTrue(result2.get_ID() is not None)
     self.assertTrue(result2.get_lat() is not None)
     self.assertTrue(result2.get_lon() is not None)
     self.assertTrue(result2.get_name() is not None)
     self.assertTrue(result3.get_lat() is not None)
     self.assertTrue(result3.get_lon() is not None)
     self.assertTrue(result3.get_country() is None)
     self.assertTrue(result3.get_name() is None)
     self.assertTrue(result3.get_ID() is None)
Exemple #3
0
    def parse_JSON(self, JSON_string):
        """
        Parses a *Forecast* instance out of raw JSON data. Only certain
        properties of the data are used: if these properties are not found or
        cannot be parsed, an error is issued.

        :param JSON_string: a raw JSON string
        :type JSON_string: str
        :returns: a *Forecast* 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, *APIResponseError* if the JSON
            string embeds an HTTP status error (this is an OWM web API 2.5 bug)

        """
        if JSON_string is None:
            raise parse_response_error.ParseResponseError('JSON data is None')
        d = json.loads(JSON_string)
        # Check if server returned errors: this check overcomes the lack of use
        # of HTTP error status codes by the OWM API 2.5. This mechanism is
        # supposed to be deprecated as soon as the API fully adopts HTTP for
        # conveying errors to the clients
        if 'message' in d and 'cod' in d:
            if d['cod'] == "404":
                print(
                    "OWM API: data not found - response payload: " +
                    json.dumps(d), d['cod'])
                return None
            elif d['cod'] != "200":
                raise api_response_error.APIResponseError(
                    "OWM API: error - response payload: " + json.dumps(d),
                    d['cod'])
        try:
            place = location.location_from_dictionary(d)
        except KeyError:
            raise parse_response_error.ParseResponseError(''.join([
                __name__, ': impossible to read location info from JSON data'
            ]))
        # Handle the case when no results are found
        if 'count' in d and d['count'] == "0":
            weathers = []
        elif 'cnt' in d and d['cnt'] == 0:
            weathers = []
        else:
            if 'list' in d:
                try:
                    weathers = [weather.weather_from_dictionary(item) \
                                for item in d['list']]
                except KeyError:
                    raise parse_response_error.ParseResponseError(
                          ''.join([__name__, ': impossible to read weather ' \
                                   'info from JSON data'])
                                  )
            else:
                raise parse_response_error.ParseResponseError(
                          ''.join([__name__, ': impossible to read weather ' \
                                   'list from JSON data'])
                          )
        current_time = int(round(time.time()))
        return forecast.Forecast(None, current_time, place, weathers)
Exemple #4
0
 def test_from_dictionary_holds_the_lack_of_geocoords(self):
     dict1 = {"station": {"coord": {}}}
     dict2 = {"coord": {}}
     result1 = location_from_dictionary(dict1)
     self.assertTrue(isinstance(result1, Location))
     self.assertEqual(result1.get_lat(), 0.0)
     self.assertEqual(result1.get_lon(), 0.0)
     self.assertTrue(result1.get_country() is None)
     self.assertTrue(result1.get_name() is None)
     self.assertTrue(result1.get_ID() is None)
     result2 = location_from_dictionary(dict2)
     self.assertTrue(isinstance(result2, Location))
     self.assertEqual(result2.get_lat(), 0.0)
     self.assertEqual(result2.get_lon(), 0.0)
     self.assertTrue(result2.get_country() is None)
     self.assertTrue(result2.get_name() is None)
     self.assertTrue(result2.get_ID() is None)
    def parse_JSON(self, JSON_string):
        """
        Parses a *Forecast* instance out of raw JSON data. Only certain
        properties of the data are used: if these properties are not found or
        cannot be parsed, an error is issued.

        :param JSON_string: a raw JSON string
        :type JSON_string: str
        :returns: a *Forecast* 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, *APIResponseError* if the JSON
            string embeds an HTTP status error (this is an OWM web API 2.5 bug)

        """
        if JSON_string is None:
            raise parse_response_error.ParseResponseError('JSON data is None')
        d = json.loads(JSON_string)
        # Check if server returned errors: this check overcomes the lack of use
        # of HTTP error status codes by the OWM API 2.5. This mechanism is
        # supposed to be deprecated as soon as the API fully adopts HTTP for
        # conveying errors to the clients
        if 'message' in d and 'cod' in d:
            if d['cod'] == "404":
                print("OWM API: data not found - response payload: " + \
                    json.dumps(d))
                return None
            elif d['cod'] != "200":
                raise api_response_error.APIResponseError("OWM API: error " \
                                    " - response payload: " + json.dumps(d))
        try:
            place = location.location_from_dictionary(d)
        except KeyError:
            raise parse_response_error.ParseResponseError(''.join([__name__,
                      ': impossible to read location info from JSON data']))
        # Handle the case when no results are found
        if 'count' in d and d['count'] == "0":
            weathers = []
        elif 'cnt' in d and d['cnt'] == 0:
            weathers = []
        else:
            if 'list' in d:
                try:
                    weathers = [weather.weather_from_dictionary(item) \
                                for item in d['list']]
                except KeyError:
                    raise parse_response_error.ParseResponseError(
                          ''.join([__name__, ': impossible to read weather ' \
                                   'info from JSON data'])
                                  )
            else:
                raise parse_response_error.ParseResponseError(
                          ''.join([__name__, ': impossible to read weather ' \
                                   'list from JSON data'])
                          )
        current_time = int(round(time.time()))
        return forecast.Forecast(None, current_time, place, weathers)
    def parse_JSON(self, JSON_string):
        """
        Parses an *Observation* instance out of raw JSON data. Only certain
        properties of the data are used: if these properties are not found or
        cannot be parsed, an error is issued.

        :param JSON_string: a raw JSON string
        :type JSON_string: str
        :returns: an *Observation* 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, *APIResponseError* if the JSON
            string embeds an HTTP status error (this is an OWM web API 2.5 bug)

        """
        if JSON_string is None:
            raise parse_response_error.ParseResponseError('JSON data is None')
        d = loads(JSON_string)
        # Check if server returned errors: this check overcomes the lack of use
        # of HTTP error status codes by the OWM API 2.5. This mechanism is
        # supposed to be deprecated as soon as the API fully adopts HTTP for
        # conveying errors to the clients
        if 'message' in d and 'cod' in d:
            if d['cod'] == "404":
                print("OWM API: observation data not available - response " \
                    "payload: " + dumps(d))
                return None
            else:
                raise api_response_error.APIResponseError(
                                      "OWM API: error - response payload: " + \
                                       dumps(d))
        try:
            place = location.location_from_dictionary(d)
        except KeyError:
            raise parse_response_error.ParseResponseError(
                                      ''.join([__name__, ': impossible to ' \
                                       'read location info from JSON data']))
        try:
            w = weather.weather_from_dictionary(d)
        except KeyError:
            raise parse_response_error.ParseResponseError(
                                      ''.join([__name__, ': impossible to ' \
                                       'read weather info from JSON data']))
        current_time = int(round(time()))
        return observation.Observation(current_time, place, w)
Exemple #7
0
    def parse_JSON(self, JSON_string):
        """
        Parses an *Observation* instance out of raw JSON data. Only certain
        properties of the data are used: if these properties are not found or
        cannot be parsed, an error is issued.

        :param JSON_string: a raw JSON string
        :type JSON_string: str
        :returns: an *Observation* 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, *APIResponseError* if the JSON
            string embeds an HTTP status error (this is an OWM web API 2.5 bug)

        """
        if JSON_string is None:
            raise parse_response_error.ParseResponseError('JSON data is None')
        d = loads(JSON_string)
        # Check if server returned errors: this check overcomes the lack of use
        # of HTTP error status codes by the OWM API 2.5. This mechanism is
        # supposed to be deprecated as soon as the API fully adopts HTTP for
        # conveying errors to the clients
        if 'message' in d and 'cod' in d:
            if d['cod'] == "404":
                print("OWM API: observation data not available - response " \
                    "payload: " + dumps(d))
                return None
            else:
                raise api_response_error.APIResponseError(
                                      "OWM API: error - response payload: " + \
                                       dumps(d))
        try:
            place = location.location_from_dictionary(d)
        except KeyError:
            raise parse_response_error.ParseResponseError(
                                      ''.join([__name__, ': impossible to ' \
                                       'read location info from JSON data']))
        try:
            w = weather.weather_from_dictionary(d)
        except KeyError:
            raise parse_response_error.ParseResponseError(
                                      ''.join([__name__, ': impossible to ' \
                                       'read weather info from JSON data']))
        current_time = int(round(time()))
        return observation.Observation(current_time, place, w)