Skip to content

Commit 53fc9d3

Browse files
author
Hankun Li
committed
Creation Pattern applied
1 parent 1c7e4ae commit 53fc9d3

File tree

3 files changed

+168
-42
lines changed

3 files changed

+168
-42
lines changed

app/location/__init__.py

Lines changed: 152 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,163 @@
22
from ..coordinates import Coordinates
33
from ..utils import countries
44
from ..utils.populations import country_population
5+
from abc import abstractmethod
6+
class Builder:
7+
def build_base(self) -> None:
8+
pass
9+
def build_stat(self) -> None:
10+
pass
11+
def build_geo(self) -> None:
12+
pass
13+
def build_timelines(self) -> None:
14+
pass
515

16+
class Location_Builder(Builder):
17+
def __init__(self, basinfo, statistic, geoinfo) -> None:
18+
self.reset()
19+
20+
def reset(self) -> None:
21+
self._location = Location()
22+
23+
def locate(self) -> Location:
24+
location = self._location
25+
self.reset()
26+
return location
27+
28+
def build_base(self, baseinfo) -> None:
29+
self.baseinfo = baseinfo
30+
def build_stat(self, statistic) -> None:
31+
self.statistic = statistic
32+
def build_geo(self, geoinfo) -> None:
33+
self.geoinfo = geoinfo
34+
35+
class TimelinedLocation_Builder(Builder):
36+
def __init__(self, baseinfo, statistic, geoinfo, timelines) -> None:
37+
self.reset()
38+
39+
def reset(self) -> None:
40+
self._location = TimelinedLocation()
41+
42+
def locate(self) -> TimelinedLocation:
43+
timelined_location = self._timelined_location
44+
self.reset()
45+
return timelined_location
646

47+
def build_base(self, baseinfo) -> None:
48+
self.baseinfo = baseinfo
49+
def build_stat(self, statistic) -> None:
50+
self.statistic = statistic
51+
def build_geo(self, geoinfo) -> None:
52+
self.geoinfo = geoinfo
53+
def build_timelines(self, timelines) -> None:
54+
self.timelines = timelines
55+
56+
class Location:
57+
58+
def __init__(self):
59+
self.id = None
60+
self.last_updated = None
61+
self.country_code = None
62+
self.country_population = None
63+
self.serialize = None
64+
self.geoinfo = None
65+
self.last_updated = None
66+
self.statistic = None
67+
68+
def set_base(self, id, last_updated, serialize) -> None:
69+
self.id = id
70+
self.last_updated = last_updated
71+
self.serialize = serialize
72+
def set_geoinfo(self, geoinfo):
73+
self._geoinfo = geoinfo
74+
def set_statistic(self, statistic):
75+
self._statistic = statistic
76+
77+
class TimelinedLocation:
78+
79+
def set_base(self, id, last_updated, serialize) -> None:
80+
self.id = id
81+
self.last_updated = last_updated
82+
self.serialize = serialize
83+
def set_geoinfo(self, geoinfo):
84+
self._geoinfo = geoinfo
85+
def set_timelines(self, timelines):
86+
self._timelines = timelines
87+
88+
class Director:
89+
def __init__(self) -> None:
90+
self._builder = None
91+
92+
def set_builder(self, builder: Builder) -> None:
93+
self._builder = builder
94+
95+
def build_location(self) -> None:
96+
location = Location()
97+
self.builder.build_location()
98+
self.builder.build_base()
99+
self.builder.build_stat()
100+
self.builder.build_geo()
101+
102+
def build_timelinedlocation(self) -> None:
103+
self.builder.build_location()
104+
self.builder.build_base()
105+
self.builder.build_stat()
106+
self.builder.build_geo()
107+
self.builder.build_timelines()
108+
109+
class Stastistic:
110+
def __init__(self, confirmed, deaths, recovered):
111+
self.confirmed = confirmed
112+
self.deaths = deaths
113+
self.recovered = recovered
114+
115+
class GeoInfo:
116+
def __init__(self, country, province, coorinates):
117+
self.country = country
118+
self.province = province
119+
self.coordinates = coorinates
120+
self.country_code = (countries.country_code(self.geoinfo.country) or countries.DEFAULT_COUNTRY_CODE).upper()
121+
self.country_population = country_population(self.country_code)
122+
123+
class BaseInfo:
124+
def __init__(self, id, last_updated, serialize):
125+
self.id = id
126+
self.last_updated = last_updated
127+
self.serialize = {
128+
# General info.
129+
"id": self.id,
130+
"country": self.geoinfo.country,
131+
"country_code": self.country_code,
132+
"country_population": self.country_population,
133+
"province": self.geoinfo.province,
134+
# Coordinates.
135+
"coordinates": self.geoinfo.coordinates.serialize(),
136+
# Last updated.
137+
"last_updated": self.last_updated,
138+
# Latest data (statistics).
139+
"latest": {
140+
"confirmed": self.statistic.confirmed,
141+
"deaths": self.statistic.deaths,
142+
"recovered": self.statistic.recovered,
143+
},
144+
}
7145
# pylint: disable=redefined-builtin,invalid-name
8-
class Location: # pylint: disable=too-many-instance-attributes
146+
'''class Location: # pylint: disable=too-many-instance-attributes
9147
"""
10148
A location in the world affected by the coronavirus.
11149
"""
12150
13151
def __init__(
14-
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
152+
self, id, geoinfo, last_updated, statistic
15153
): # pylint: disable=too-many-arguments
16154
# General info.
17155
self.id = id
18-
self.country = country.strip()
19-
self.province = province.strip()
20-
self.coordinates = coordinates
21-
156+
self.geoinfo = geoinfo
22157
# Last update.
23158
self.last_updated = last_updated
24159
25160
# Statistics.
26-
self.confirmed = confirmed
27-
self.deaths = deaths
28-
self.recovered = recovered
161+
self.statistic = statistic
29162
30163
@property
31164
def country_code(self):
@@ -35,7 +168,7 @@ def country_code(self):
35168
:returns: The country code.
36169
:rtype: str
37170
"""
38-
return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper()
171+
return (countries.country_code(self.geoinfo.country) or countries.DEFAULT_COUNTRY_CODE).upper()
39172
40173
@property
41174
def country_population(self):
@@ -57,19 +190,19 @@ def serialize(self):
57190
return {
58191
# General info.
59192
"id": self.id,
60-
"country": self.country,
193+
"country": self.geoinfo.country,
61194
"country_code": self.country_code,
62195
"country_population": self.country_population,
63-
"province": self.province,
196+
"province": self.geoinfo.province,
64197
# Coordinates.
65-
"coordinates": self.coordinates.serialize(),
198+
"coordinates": self.geoinfo.coordinates.serialize(),
66199
# Last updated.
67200
"last_updated": self.last_updated,
68201
# Latest data (statistics).
69202
"latest": {
70-
"confirmed": self.confirmed,
71-
"deaths": self.deaths,
72-
"recovered": self.recovered,
203+
"confirmed": self.statistic.confirmed,
204+
"deaths": self.statistic.deaths,
205+
"recovered": self.statistic.recovered,
73206
},
74207
}
75208
@@ -80,13 +213,11 @@ class TimelinedLocation(Location):
80213
"""
81214
82215
# pylint: disable=too-many-arguments
83-
def __init__(self, id, country, province, coordinates, last_updated, timelines):
216+
def __init__(self, id, geoinfo, last_updated, timelines):
84217
super().__init__(
85218
# General info.
86219
id,
87-
country,
88-
province,
89-
coordinates,
220+
geoinfo,
90221
last_updated,
91222
# Statistics (retrieve latest from timelines).
92223
confirmed=timelines.get("confirmed").latest or 0,
@@ -122,3 +253,4 @@ def serialize(self, timelines=False):
122253
123254
# Return the serialized location.
124255
return serialized
256+
'''

app/location/csbs.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
"""app.locations.csbs.py"""
2-
from . import Location
2+
from . import Director, LocationBuilder, BaseInfo, GeoInfo, Statistic
33

44

5-
class CSBSLocation(Location):
5+
class CSBSLocation:
66
"""
77
A CSBS (county) location.
88
"""
9-
109
# pylint: disable=too-many-arguments,redefined-builtin
1110
def __init__(self, id, state, county, coordinates, last_updated, confirmed, deaths):
12-
super().__init__(
13-
# General info.
14-
id,
15-
"US",
16-
state,
17-
coordinates,
18-
last_updated,
19-
# Statistics.
20-
confirmed=confirmed,
21-
deaths=deaths,
22-
recovered=0,
23-
)
24-
25-
self.state = state
26-
self.county = county
11+
director = Director()
12+
baseinfo = BaseInfo(id=id,last_updated=last_updated)
13+
geoinfo = GeoInfo(county="US", province=state, coordinates=coordinates)
14+
statistic = Statistic(confirmed=confirmed, deaths=deaths, recovered=0)
15+
locationBuilder = LocationBuilder(baseinfo=baseinfo, statistic=statistic, geoinfo=geoinfo)
16+
director.set_builder(LocationBuilder)
17+
csbs = director.build_location()
2718

2819
def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
2920
"""

app/location/nyt.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""app.locations.nyt.py"""
2-
from . import TimelinedLocation
2+
from . import Director, TimelinedLocationBuilder, BaseInfo, GeoInfo, Statistic
33

44

55
class NYTLocation(TimelinedLocation):
@@ -9,10 +9,13 @@ class NYTLocation(TimelinedLocation):
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)
1312

14-
self.state = state
15-
self.county = county
13+
director = Director()
14+
baseinfo = BaseInfo(id=id,last_updated=last_updated)
15+
geoinfo = GeoInfo(county="US", province=state, coordinates=coordinates)
16+
locationBuilder = TimelinedLocationBuilder(baseinfo=baseinfo, geoinfo=geoinfo, timelines=timelines)
17+
director.set_builder(TimelinedLocationBuilder)
18+
nyt = director.build_location()
1619

1720
def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
1821
"""

0 commit comments

Comments
 (0)