Skip to content

Commit 855f548

Browse files
committed
Apply factory pattern. Create a new factory class ModelFactory under app/services/location directory. Change code in jhu.py and nyt.py to use ModelFactory's create_factory() method
1 parent 1c7e4ae commit 855f548

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

app/services/location/jhu.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ...utils import date as date_util
1717
from ...utils import httputils
1818
from . import LocationService
19+
from . import ModelFactory
1920

2021
LOGGER = logging.getLogger("services.location.jhu")
2122
PID = os.getpid()
@@ -155,6 +156,9 @@ async def get_locations():
155156
# incorrect data to consumers.
156157
# ***************************************************************************
157158
# Go through locations.
159+
modelFactory = ModelFactory()
160+
model_type = "timeline"
161+
158162
for index, location in enumerate(locations_confirmed):
159163
# Get the timelines.
160164

@@ -183,19 +187,19 @@ async def get_locations():
183187
datetime.utcnow().isoformat() + "Z",
184188
# Timelines (parse dates as ISO).
185189
{
186-
"confirmed": Timeline(
187-
timeline={
190+
"confirmed": modelFactory.create_model(model_type,
191+
{
188192
datetime.strptime(date, "%m/%d/%y").isoformat() + "Z": amount
189193
for date, amount in timelines["confirmed"].items()
190194
}
191195
),
192-
"deaths": Timeline(
193-
timeline={
196+
"deaths": modelFactory.create_model(model_type,
197+
{
194198
datetime.strptime(date, "%m/%d/%y").isoformat() + "Z": amount
195199
for date, amount in timelines["deaths"].items()
196200
}
197201
),
198-
"recovered": Timeline(
202+
"recovered": modelFactory.create_model(model_type,
199203
timeline={
200204
datetime.strptime(date, "%m/%d/%y").isoformat() + "Z": amount
201205
for date, amount in timelines["recovered"].items()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from ...models import *
2+
class ModelFactory():
3+
def create_model(self, type, data):
4+
"""Factory Method"""
5+
localizers = {
6+
"timeline": Timeline(timeline = data),
7+
"latest": Latest(confirmed = data.confirmed, deaths = data.deaths, recovered = data.recovered),
8+
"timelines": Timelines(confirmed = data.confirmed, deaths = data.deaths, recovered = data.recovered),
9+
"location": Location(data)
10+
}
11+
12+
return localizers[type]

app/services/location/nyt.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ...models import Timeline
1313
from ...utils import httputils
1414
from . import LocationService
15+
from . import ModelFactory
1516

1617
LOGGER = logging.getLogger("services.location.nyt")
1718

@@ -99,6 +100,8 @@ async def get_locations():
99100

100101
# The normalized locations.
101102
locations = []
103+
modelFactory = ModelFactory()
104+
model_type = "timeline"
102105

103106
for idx, (county_state, histories) in enumerate(grouped_locations.items()):
104107
# Make location history for confirmed and deaths from dates.
@@ -118,19 +121,19 @@ async def get_locations():
118121
coordinates=Coordinates(None, None), # NYT does not provide coordinates
119122
last_updated=datetime.utcnow().isoformat() + "Z", # since last request
120123
timelines={
121-
"confirmed": Timeline(
122-
timeline={
124+
"confirmed": modelFactory.create_model(model_type,
125+
{
123126
datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount
124127
for date, amount in confirmed_history.items()
125128
}
126129
),
127-
"deaths": Timeline(
128-
timeline={
130+
"deaths": modelFactory.create_model(model_type,
131+
{
129132
datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount
130133
for date, amount in deaths_history.items()
131134
}
132135
),
133-
"recovered": Timeline(),
136+
"recovered": modelFactory.create_model(model_type,{}),
134137
},
135138
)
136139
)

0 commit comments

Comments
 (0)