From 7b4d58b028101e48ab581683c4c9ebf720dee598 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 20 Jul 2021 21:39:43 -0400 Subject: [PATCH 1/2] Apply aggregate to the Coordinates class making the latitude and longitude into protected, because there is no point making them public --- app/coordinates.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/coordinates.py b/app/coordinates.py index be972c6e..8c481a40 100644 --- a/app/coordinates.py +++ b/app/coordinates.py @@ -7,8 +7,8 @@ class Coordinates: """ def __init__(self, latitude, longitude): - self.latitude = latitude - self.longitude = longitude + self._latitude = latitude + self._longitude = longitude def serialize(self): """ @@ -17,7 +17,7 @@ def serialize(self): :returns: The serialized coordinates. :rtype: dict """ - return {"latitude": self.latitude, "longitude": self.longitude} + return {"latitude": self._latitude, "longitude": self._longitude} def __str__(self): - return "lat: %s, long: %s" % (self.latitude, self.longitude) + return "lat: %s, long: %s" % (self._latitude, self._longitude) From 0bba0b85d9511fee824e932d602ad742923142b0 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 3 Aug 2021 13:30:41 -0400 Subject: [PATCH 2/2] Convert the Coordinates class to a pydantic model As requested by Kilo59. It still passed all the unit test, which is good. :) --- app/coordinates.py | 19 +++++-------------- app/location/__init__.py | 2 +- app/services/location/csbs.py | 2 +- app/services/location/jhu.py | 5 ++++- app/services/location/nyt.py | 2 +- tests/test_coordinates.py | 4 ++-- 6 files changed, 14 insertions(+), 20 deletions(-) diff --git a/app/coordinates.py b/app/coordinates.py index 8c481a40..1f397395 100644 --- a/app/coordinates.py +++ b/app/coordinates.py @@ -1,23 +1,14 @@ """app.coordinates.py""" +from pydantic import BaseModel -class Coordinates: +class Coordinates(BaseModel): """ A position on earth using decimal coordinates (latitude and longitude). """ - def __init__(self, latitude, longitude): - self._latitude = latitude - self._longitude = longitude - - def serialize(self): - """ - Serializes the coordinates into a dict. - - :returns: The serialized coordinates. - :rtype: dict - """ - return {"latitude": self._latitude, "longitude": self._longitude} + latitude: float = None + longitude: float = None def __str__(self): - return "lat: %s, long: %s" % (self._latitude, self._longitude) + return "lat: %s, long: %s" % (self.latitude, self.longitude) diff --git a/app/location/__init__.py b/app/location/__init__.py index 1da5e9e5..33ee1b55 100644 --- a/app/location/__init__.py +++ b/app/location/__init__.py @@ -62,7 +62,7 @@ def serialize(self): "country_population": self.country_population, "province": self.province, # Coordinates. - "coordinates": self.coordinates.serialize(), + "coordinates": self.coordinates.dict(), # Last updated. "last_updated": self.last_updated, # Latest data (statistics). diff --git a/app/services/location/csbs.py b/app/services/location/csbs.py index 444ebad6..89805a61 100644 --- a/app/services/location/csbs.py +++ b/app/services/location/csbs.py @@ -82,7 +82,7 @@ async def get_locations(): state, county, # Coordinates. - Coordinates(item["Latitude"], item["Longitude"]), + Coordinates(latitude=item["Latitude"], longitude=item["Longitude"]), # Last update (parse as ISO). datetime.strptime(last_update, "%Y-%m-%d %H:%M").isoformat() + "Z", # Statistics. diff --git a/app/services/location/jhu.py b/app/services/location/jhu.py index ebed3960..64bb543f 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -169,6 +169,7 @@ async def get_locations(): # Grab coordinates. coordinates = location["coordinates"] + # print(coordinates) # Create location (supporting timelines) and append. locations.append( @@ -178,7 +179,9 @@ async def get_locations(): location["country"], location["province"], # Coordinates. - Coordinates(latitude=coordinates["lat"], longitude=coordinates["long"]), + Coordinates( + latitude=coordinates["lat"] or None, longitude=coordinates["long"] or None + ), # Last update. datetime.utcnow().isoformat() + "Z", # Timelines (parse dates as ISO). diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py index 1f25ec34..2d2ae1e0 100644 --- a/app/services/location/nyt.py +++ b/app/services/location/nyt.py @@ -115,7 +115,7 @@ async def get_locations(): id=idx, state=county_state[1], county=county_state[0], - coordinates=Coordinates(None, None), # NYT does not provide coordinates + coordinates=Coordinates(), # NYT does not provide coordinates last_updated=datetime.utcnow().isoformat() + "Z", # since last request timelines={ "confirmed": Timeline( diff --git a/tests/test_coordinates.py b/tests/test_coordinates.py index 2ec0290a..952d573f 100644 --- a/tests/test_coordinates.py +++ b/tests/test_coordinates.py @@ -10,6 +10,6 @@ def test_coordinates_class(latitude, longitude): coord_obj = coordinates.Coordinates(latitude=latitude, longitude=longitude) # validate serialize - check_obj = {"latitude": latitude, "longitude": longitude} + check_obj = {"latitude": float(latitude), "longitude": float(longitude)} - assert coord_obj.serialize() == check_obj + assert coord_obj.dict() == check_obj