Skip to content

Commit 0bba0b8

Browse files
committed
Convert the Coordinates class to a pydantic model
As requested by Kilo59. It still passed all the unit test, which is good. :)
1 parent 7b4d58b commit 0bba0b8

File tree

6 files changed

+14
-20
lines changed

6 files changed

+14
-20
lines changed

app/coordinates.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
"""app.coordinates.py"""
2+
from pydantic import BaseModel
23

34

4-
class Coordinates:
5+
class Coordinates(BaseModel):
56
"""
67
A position on earth using decimal coordinates (latitude and longitude).
78
"""
89

9-
def __init__(self, latitude, longitude):
10-
self._latitude = latitude
11-
self._longitude = longitude
12-
13-
def serialize(self):
14-
"""
15-
Serializes the coordinates into a dict.
16-
17-
:returns: The serialized coordinates.
18-
:rtype: dict
19-
"""
20-
return {"latitude": self._latitude, "longitude": self._longitude}
10+
latitude: float = None
11+
longitude: float = None
2112

2213
def __str__(self):
23-
return "lat: %s, long: %s" % (self._latitude, self._longitude)
14+
return "lat: %s, long: %s" % (self.latitude, self.longitude)

app/location/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def serialize(self):
6262
"country_population": self.country_population,
6363
"province": self.province,
6464
# Coordinates.
65-
"coordinates": self.coordinates.serialize(),
65+
"coordinates": self.coordinates.dict(),
6666
# Last updated.
6767
"last_updated": self.last_updated,
6868
# Latest data (statistics).

app/services/location/csbs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async def get_locations():
8282
state,
8383
county,
8484
# Coordinates.
85-
Coordinates(item["Latitude"], item["Longitude"]),
85+
Coordinates(latitude=item["Latitude"], longitude=item["Longitude"]),
8686
# Last update (parse as ISO).
8787
datetime.strptime(last_update, "%Y-%m-%d %H:%M").isoformat() + "Z",
8888
# Statistics.

app/services/location/jhu.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ async def get_locations():
169169

170170
# Grab coordinates.
171171
coordinates = location["coordinates"]
172+
# print(coordinates)
172173

173174
# Create location (supporting timelines) and append.
174175
locations.append(
@@ -178,7 +179,9 @@ async def get_locations():
178179
location["country"],
179180
location["province"],
180181
# Coordinates.
181-
Coordinates(latitude=coordinates["lat"], longitude=coordinates["long"]),
182+
Coordinates(
183+
latitude=coordinates["lat"] or None, longitude=coordinates["long"] or None
184+
),
182185
# Last update.
183186
datetime.utcnow().isoformat() + "Z",
184187
# Timelines (parse dates as ISO).

app/services/location/nyt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async def get_locations():
115115
id=idx,
116116
state=county_state[1],
117117
county=county_state[0],
118-
coordinates=Coordinates(None, None), # NYT does not provide coordinates
118+
coordinates=Coordinates(), # NYT does not provide coordinates
119119
last_updated=datetime.utcnow().isoformat() + "Z", # since last request
120120
timelines={
121121
"confirmed": Timeline(

tests/test_coordinates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ def test_coordinates_class(latitude, longitude):
1010
coord_obj = coordinates.Coordinates(latitude=latitude, longitude=longitude)
1111

1212
# validate serialize
13-
check_obj = {"latitude": latitude, "longitude": longitude}
13+
check_obj = {"latitude": float(latitude), "longitude": float(longitude)}
1414

15-
assert coord_obj.serialize() == check_obj
15+
assert coord_obj.dict() == check_obj

0 commit comments

Comments
 (0)