Skip to content

Commit db80fef

Browse files
committed
A structural pattern (composition) updated coordinates classes with two sub classes
1 parent 23a5136 commit db80fef

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

app/coordinates.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
"""app.coordinates.py"""
22

3+
class Latitude:
4+
"""
5+
A numeric representation of a latitudinal position north or south of the Earth's equator, to track Coronavirus spread nation to nation latitudinally.
6+
"""
7+
def __init__(self, value: float):
8+
self.data = value
9+
10+
def sum(other: Latitude):
11+
self.data += other.data
12+
13+
def merge(other: Longitude):
14+
return Coordinates(self, other)
15+
16+
class Longitude:
17+
"""
18+
A numeric representation of a longitudinal position measuring east to west, to track Coronavirus spread nation to nation longitudinally
19+
"""
20+
def __init__(self, value: float):
21+
self.data = value
22+
23+
def sum(other: Longitude):
24+
self.data += other.data
25+
26+
def merge(other: Latitude):
27+
return Coordinates(self, other)
328

429
class Coordinates:
530
"""
631
A position on earth using decimal coordinates (latitude and longitude).
732
"""
833

9-
def __init__(self, latitude, longitude):
34+
def __init__(self, latitude: Latitude, longitude:Longitude):
1035
self.latitude = latitude
1136
self.longitude = longitude
1237

1338
def serialize(self):
1439
"""
1540
Serializes the coordinates into a dict.
16-
1741
:returns: The serialized coordinates.
1842
:rtype: dict
1943
"""
20-
return {"latitude": self.latitude, "longitude": self.longitude}
44+
return {"latitude": self.latitude.data, "longitude": self.longitude.data}
2145

2246
def __str__(self):
23-
return "lat: %s, long: %s" % (self.latitude, self.longitude)
47+
return "lat: %s, long: %s" % (self.latitude.data, self.longitude.data)

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async def get_category(category):
101101
"country_code": countries.country_code(country),
102102
"province": item["Province/State"],
103103
# Coordinates.
104-
"coordinates": {"lat": item["Lat"], "long": item["Long"],},
104+
"coordinates": {"lat": Latitude(item["Lat"]), "long": Longitude(item["Long"]),},
105105
# History.
106106
"history": history,
107107
# Latest statistic.
@@ -178,7 +178,7 @@ async def get_locations():
178178
location["country"],
179179
location["province"],
180180
# Coordinates.
181-
Coordinates(latitude=coordinates["lat"], longitude=coordinates["long"]),
181+
Coordinates(latitude=Latitude(coordinates["lat"]), longitude=Longitude(coordinates["long"]),
182182
# Last update.
183183
datetime.utcnow().isoformat() + "Z",
184184
# Timelines (parse dates as ISO).

0 commit comments

Comments
 (0)