Skip to content

Commit 976b8d4

Browse files
authored
Update __init__.py
1 parent c3d8c6e commit 976b8d4

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

app/location/__init__.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,81 @@ def serialize(self):
8282
},
8383
}
8484

85+
# pylint: disable=redefined-builtin,invalid-name
86+
class LocationAdapter(Location): # pylint: disable=too-many-instance-attributes
87+
"""
88+
A location in the world affected by the coronavirus.
89+
"""
90+
91+
def __init__(
92+
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
93+
): # pylint: disable=too-many-arguments
94+
# General info.
95+
self.id = id
96+
self.country = country.strip()
97+
self.province = province.strip()
98+
self.coordinates = coordinates
8599

100+
# Last update.
101+
self.last_updated = last_updated
102+
103+
# Statistics.
104+
self.confirmed = confirmed
105+
self.deaths = deaths
106+
self.recovered = recovered
107+
108+
@property
109+
def country_code(self):
110+
"""
111+
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
112+
113+
:returns: The country code.
114+
:rtype: str
115+
"""
116+
return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper()
117+
118+
@property
119+
def country_population(self):
120+
"""
121+
Gets the population of this location.
122+
123+
:returns: The population.
124+
:rtype: int
125+
"""
126+
return country_population(self.country_code)
127+
128+
def serialize(self):
129+
"""
130+
Serializes the location into a dict.
131+
132+
:returns: The serialized location.
133+
:rtype: dict
134+
"""
135+
serialized = {
136+
# General info.
137+
"id": self.id,
138+
"country": self.country,
139+
"country_code": self.country_code,
140+
"country_population": self.country_population,
141+
"province": self.province,
142+
# Coordinates.
143+
"coordinates": self.coordinates.serialize(),
144+
# Last updated.
145+
"last_updated": self.last_updated,
146+
# Latest data (statistics).
147+
"latest": {
148+
"confirmed": self.confirmed,
149+
"deaths": self.deaths,
150+
"recovered": self.recovered,
151+
},
152+
}
153+
154+
# Update with new fields.
155+
serialized.update(
156+
{"state": self.province, "county": self.county,}
157+
)
158+
159+
return serialized
86160

87161
class TimelinedLocation(Location):
88162
"""

0 commit comments

Comments
 (0)