Skip to content

Commit 02527ae

Browse files
author
dengrf96
committed
Apply Structural Design Pattern
1 parent 1c7e4ae commit 02527ae

File tree

7 files changed

+49
-52
lines changed

7 files changed

+49
-52
lines changed

app/location/__init__.py

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Location: # pylint: disable=too-many-instance-attributes
1111
"""
1212

1313
def __init__(
14-
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
14+
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered, timelines
1515
): # pylint: disable=too-many-arguments
1616
# General info.
1717
self.id = id
@@ -26,6 +26,7 @@ def __init__(
2626
self.confirmed = confirmed
2727
self.deaths = deaths
2828
self.recovered = recovered
29+
self.timelines = timelines
2930

3031
@property
3132
def country_code(self):
@@ -47,14 +48,14 @@ def country_population(self):
4748
"""
4849
return country_population(self.country_code)
4950

50-
def serialize(self):
51+
def serialize(self, timelines=False):
5152
"""
5253
Serializes the location into a dict.
5354
5455
:returns: The serialized location.
5556
:rtype: dict
5657
"""
57-
return {
58+
serialized = {
5859
# General info.
5960
"id": self.id,
6061
"country": self.country,
@@ -72,43 +73,6 @@ def serialize(self):
7273
"recovered": self.recovered,
7374
},
7475
}
75-
76-
77-
class TimelinedLocation(Location):
78-
"""
79-
A location with timelines.
80-
"""
81-
82-
# pylint: disable=too-many-arguments
83-
def __init__(self, id, country, province, coordinates, last_updated, timelines):
84-
super().__init__(
85-
# General info.
86-
id,
87-
country,
88-
province,
89-
coordinates,
90-
last_updated,
91-
# Statistics (retrieve latest from timelines).
92-
confirmed=timelines.get("confirmed").latest or 0,
93-
deaths=timelines.get("deaths").latest or 0,
94-
recovered=timelines.get("recovered").latest or 0,
95-
)
96-
97-
# Set timelines.
98-
self.timelines = timelines
99-
100-
# pylint: disable=arguments-differ
101-
def serialize(self, timelines=False):
102-
"""
103-
Serializes the location into a dict.
104-
105-
:param timelines: Whether to include the timelines.
106-
:returns: The serialized location.
107-
:rtype: dict
108-
"""
109-
serialized = super().serialize()
110-
111-
# Whether to include the timelines or not.
11276
if timelines:
11377
serialized.update(
11478
{
@@ -119,6 +83,4 @@ def serialize(self, timelines=False):
11983
}
12084
}
12185
)
122-
123-
# Return the serialized location.
12486
return serialized

app/location/csbs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(self, id, state, county, coordinates, last_updated, confirmed, deat
2020
confirmed=confirmed,
2121
deaths=deaths,
2222
recovered=0,
23+
timelines=None
2324
)
2425

2526
self.state = state
@@ -32,7 +33,7 @@ def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused
3233
:returns: The serialized location.
3334
:rtype: dict
3435
"""
35-
serialized = super().serialize()
36+
serialized = super().serialize(timelines)
3637

3738
# Update with new fields.
3839
serialized.update(

app/location/jhu.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""app.locations.nyt.py"""
2+
from app.location import Location
3+
4+
5+
class JHULocation(Location):
6+
"""
7+
A JHU (county) Location.
8+
"""
9+
10+
# pylint: disable=too-many-arguments,redefined-builtin
11+
def __init__(self, id, country, state, coordinates, last_updated, timelines):
12+
super().__init__(
13+
# General info.
14+
id,
15+
country,
16+
state,
17+
coordinates,
18+
last_updated,
19+
confirmed=timelines.get("confirmed").latest or 0,
20+
deaths=timelines.get("deaths").latest or 0,
21+
recovered=timelines.get("recovered").latest or 0,
22+
timelines=timelines
23+
)

app/location/nyt.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
"""app.locations.nyt.py"""
2-
from . import TimelinedLocation
2+
from app.location import Location
33

44

5-
class NYTLocation(TimelinedLocation):
5+
class NYTLocation(Location):
66
"""
7-
A NYT (county) Timelinedlocation.
7+
A NYT (county) Location.
88
"""
99

1010
# pylint: disable=too-many-arguments,redefined-builtin
1111
def __init__(self, id, state, county, coordinates, last_updated, timelines):
12-
super().__init__(id, "US", state, coordinates, last_updated, timelines)
12+
super().__init__(
13+
# General info.
14+
id,
15+
"US",
16+
state,
17+
coordinates,
18+
last_updated,
19+
confirmed=timelines.get("confirmed").latest or 0,
20+
deaths=timelines.get("deaths").latest or 0,
21+
recovered=timelines.get("recovered").latest or 0,
22+
timelines=timelines
23+
)
1324

1425
self.state = state
1526
self.county = county

app/services/location/jhu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from ...caches import check_cache, load_cache
1212
from ...coordinates import Coordinates
13-
from ...location import TimelinedLocation
13+
from ...location.jhu import JHULocation
1414
from ...models import Timeline
1515
from ...utils import countries
1616
from ...utils import date as date_util
@@ -172,7 +172,7 @@ async def get_locations():
172172

173173
# Create location (supporting timelines) and append.
174174
locations.append(
175-
TimelinedLocation(
175+
JHULocation(
176176
# General info.
177177
index,
178178
location["country"],

tests/test_location.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_location_class(
4747
now = datetime.utcnow().isoformat() + "Z"
4848

4949
# Location.
50-
location_obj = location.TimelinedLocation(
50+
location_obj = location.Location(
5151
test_id,
5252
country,
5353
province,

tests/test_nyt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from app.location import TimelinedLocation
6+
from app.location import Location
77
from app.location.nyt import NYTLocation
88
from app.services.location import nyt
99
from tests.conftest import mocked_strptime_isoformat
@@ -23,7 +23,7 @@ async def test_get_locations(mock_client_session):
2323
serialized_locations = []
2424
for location in locations:
2525
assert isinstance(location, NYTLocation)
26-
assert isinstance(location, TimelinedLocation)
26+
assert isinstance(location, Location)
2727

2828
# Making sure country population is a non-zero value
2929
assert location.country_population != 0

0 commit comments

Comments
 (0)