Skip to content

Commit 027b992

Browse files
committed
Aggregate pattern 2
1 parent 1c7e4ae commit 027b992

File tree

1 file changed

+75
-22
lines changed

1 file changed

+75
-22
lines changed

app/location/__init__.py

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,65 @@
33
from ..utils import countries
44
from ..utils.populations import country_population
55

6+
class Statistics:
7+
def __init__(self, confirmed=0, deaths=0, recovered=0):
8+
self.__confirmed = confirmed
9+
self.__deaths = deaths
10+
self.__recovered = recovered
11+
12+
@property()
13+
def confirmed(self):
14+
return self.__confirmed
15+
16+
@confirmed.setter
17+
def confirmed(self, value):
18+
self.__confirmed = value
19+
20+
@property()
21+
def deaths(self):
22+
return self.__deaths
23+
24+
@deaths.setter
25+
def deaths(self, value):
26+
self.__deaths = value
27+
28+
@property()
29+
def recovered(self):
30+
return self.__recovered
31+
32+
@recovered.setter
33+
def recovered(self, value):
34+
self.__recovered = value
35+
36+
class Geoinfo:
37+
def __init__(self, country, province, coordinates):
38+
self.__country = country.strip()
39+
self.__province = province.strip()
40+
self.__coordinates = coordinates
41+
42+
@property()
43+
def country(self):
44+
return self.__country
45+
46+
@country.setter
47+
def country(self, value):
48+
self.__country = value
49+
50+
@property()
51+
def province(self):
52+
return self.__province
53+
54+
@province.setter
55+
def province(self, value):
56+
self.__province = value
57+
58+
@property()
59+
def coordinates(self):
60+
return self.__coordinates
61+
62+
@coordinates.setter
63+
def coordinates(self, lat, longt):
64+
self.coordinates = Coordinates(lat, longt)
665

766
# pylint: disable=redefined-builtin,invalid-name
867
class Location: # pylint: disable=too-many-instance-attributes
@@ -11,21 +70,17 @@ class Location: # pylint: disable=too-many-instance-attributes
1170
"""
1271

1372
def __init__(
14-
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
73+
self, id, geoinfo, last_updated, statistics
1574
): # pylint: disable=too-many-arguments
1675
# General info.
1776
self.id = id
18-
self.country = country.strip()
19-
self.province = province.strip()
20-
self.coordinates = coordinates
77+
self.geoinfo = geoinfo
2178

2279
# Last update.
2380
self.last_updated = last_updated
2481

2582
# Statistics.
26-
self.confirmed = confirmed
27-
self.deaths = deaths
28-
self.recovered = recovered
83+
self.statistics = statistics
2984

3085
@property
3186
def country_code(self):
@@ -35,7 +90,7 @@ def country_code(self):
3590
:returns: The country code.
3691
:rtype: str
3792
"""
38-
return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper()
93+
return (countries.country_code(self.geoinfo.country) or countries.DEFAULT_COUNTRY_CODE).upper()
3994

4095
@property
4196
def country_population(self):
@@ -57,19 +112,19 @@ def serialize(self):
57112
return {
58113
# General info.
59114
"id": self.id,
60-
"country": self.country,
115+
"country": self.geoinfo.country,
61116
"country_code": self.country_code,
62117
"country_population": self.country_population,
63-
"province": self.province,
118+
"province": self.geoinfo.province,
64119
# Coordinates.
65-
"coordinates": self.coordinates.serialize(),
120+
"coordinates": self.geoinfo.coordinates.serialize(),
66121
# Last updated.
67122
"last_updated": self.last_updated,
68123
# Latest data (statistics).
69124
"latest": {
70-
"confirmed": self.confirmed,
71-
"deaths": self.deaths,
72-
"recovered": self.recovered,
125+
"confirmed": self.statistics.confirmed,
126+
"deaths": self.statistics.deaths,
127+
"recovered": self.statistics.recovered,
73128
},
74129
}
75130

@@ -80,20 +135,18 @@ class TimelinedLocation(Location):
80135
"""
81136

82137
# pylint: disable=too-many-arguments
83-
def __init__(self, id, country, province, coordinates, last_updated, timelines):
138+
def __init__(self, id, geoinfo, last_updated, statistics, timelines):
84139
super().__init__(
85140
# General info.
86141
id,
87-
country,
88-
province,
89-
coordinates,
142+
geoinfo,
90143
last_updated,
91144
# 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,
145+
statistics
95146
)
96-
147+
self.statistics.confirmed = timelines.get("confirmed").latest or 0
148+
self.statistics.deaths = timelines.get("deaths").latest or 0
149+
self.statistics.recovered = timelines.get("recovered").latest or 0
97150
# Set timelines.
98151
self.timelines = timelines
99152

0 commit comments

Comments
 (0)