From 23a5136c8bcd6669d29acdb51b90cdb1f28be250 Mon Sep 17 00:00:00 2001 From: Shayn Morgan Date: Sat, 24 Jul 2021 20:53:52 -0400 Subject: [PATCH 1/4] A stats class was created to implement aggregation --- app/models.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models.py b/app/models.py index 497a4b83..edc858ea 100644 --- a/app/models.py +++ b/app/models.py @@ -3,15 +3,17 @@ from pydantic import BaseModel, validator - +class Stats: + confirmed: int + deaths: int + recovered: int + class Latest(BaseModel): """ Latest model. """ - confirmed: int - deaths: int - recovered: int + stats: Stats class LatestResponse(BaseModel): From db80fef9ed9fcfdf1ee14ba73a587e951af5e2fc Mon Sep 17 00:00:00 2001 From: Shayn Morgan Date: Sun, 15 Aug 2021 19:55:38 -0400 Subject: [PATCH 2/4] A structural pattern (composition) updated coordinates classes with two sub classes --- app/coordinates.py | 32 ++++++++++++++++++++++++++++---- app/services/location/csbs.py | 2 +- app/services/location/jhu.py | 4 ++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/coordinates.py b/app/coordinates.py index be972c6e..ac3a4457 100644 --- a/app/coordinates.py +++ b/app/coordinates.py @@ -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) diff --git a/app/services/location/csbs.py b/app/services/location/csbs.py index 444ebad6..23748e08 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..06e82e13 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -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. @@ -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). From 7494a4fa4e1c9c20bbe0c11f7baebd83f3d5009d Mon Sep 17 00:00:00 2001 From: Shayn Morgan Date: Sun, 15 Aug 2021 21:38:31 -0400 Subject: [PATCH 3/4] A creational pattern (factory) updated coordinates class --- app/coordinates.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/coordinates.py b/app/coordinates.py index ac3a4457..d895d230 100644 --- a/app/coordinates.py +++ b/app/coordinates.py @@ -1,6 +1,11 @@ """app.coordinates.py""" - -class Latitude: +class CoordinateFactory: + def create_coordinate(creation_type:int, data:float): + if creation_type == 1: new_coordinate = Latitude(data) + if creation_type == 2: new_coordinate = Longitude(data) +class Coordinate: + pass +class Latitude(Coordinate): """ A numeric representation of a latitudinal position north or south of the Earth's equator, to track Coronavirus spread nation to nation latitudinally. """ @@ -13,7 +18,7 @@ def sum(other: Latitude): def merge(other: Longitude): return Coordinates(self, other) -class Longitude: +class Longitude(Coordinate): """ A numeric representation of a longitudinal position measuring east to west, to track Coronavirus spread nation to nation longitudinally """ From 262fc8d741045eef67a5bfe42aedac70beacc0dc Mon Sep 17 00:00:00 2001 From: Shayn Morgan Date: Sun, 15 Aug 2021 23:47:53 -0400 Subject: [PATCH 4/4] Update to coordinates.py to implement creational design pattern --- app/coordinates.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/coordinates.py b/app/coordinates.py index d895d230..33d69168 100644 --- a/app/coordinates.py +++ b/app/coordinates.py @@ -3,6 +3,7 @@ class CoordinateFactory: def create_coordinate(creation_type:int, data:float): if creation_type == 1: new_coordinate = Latitude(data) if creation_type == 2: new_coordinate = Longitude(data) + return new_coordinate class Coordinate: pass class Latitude(Coordinate):