Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
A structural pattern (composition) updated coordinates classes with t…
…wo sub classes
  • Loading branch information
shaynmorgan committed Aug 15, 2021
commit db80fef9ed9fcfdf1ee14ba73a587e951af5e2fc
32 changes: 28 additions & 4 deletions app/coordinates.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
"""app.coordinates.py"""

class Latitude:
"""
A numeric representation of a latitudinal position north or south of the Earth's equator, to track Coronavirus spread nation to nation latitudinally.
"""
def __init__(self, value: float):
self.data = value

def sum(other: Latitude):
self.data += other.data

def merge(other: Longitude):
return Coordinates(self, other)

class Longitude:
"""
A numeric representation of a longitudinal position measuring east to west, to track Coronavirus spread nation to nation longitudinally
"""
def __init__(self, value: float):
self.data = value

def sum(other: Longitude):
self.data += other.data

def merge(other: Latitude):
return Coordinates(self, other)

class Coordinates:
"""
A position on earth using decimal coordinates (latitude and longitude).
"""

def __init__(self, latitude, longitude):
def __init__(self, latitude: Latitude, longitude: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}
return {"latitude": self.latitude.data, "longitude": self.longitude.data}

def __str__(self):
return "lat: %s, long: %s" % (self.latitude, self.longitude)
return "lat: %s, long: %s" % (self.latitude.data, self.longitude.data)
2 changes: 1 addition & 1 deletion app/services/location/csbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions app/services/location/jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async def get_category(category):
"country_code": countries.country_code(country),
"province": item["Province/State"],
# Coordinates.
"coordinates": {"lat": item["Lat"], "long": item["Long"],},
"coordinates": {"lat": Latitude(item["Lat"]), "long": Longitude(item["Long"]),},
# History.
"history": history,
# Latest statistic.
Expand Down Expand Up @@ -178,7 +178,7 @@ async def get_locations():
location["country"],
location["province"],
# Coordinates.
Coordinates(latitude=coordinates["lat"], longitude=coordinates["long"]),
Coordinates(latitude=Latitude(coordinates["lat"]), longitude=Longitude(coordinates["long"]),
# Last update.
datetime.utcnow().isoformat() + "Z",
# Timelines (parse dates as ISO).
Expand Down