Skip to content

Commit 90a62f6

Browse files
committed
apply aggregate for country
1 parent 1c7e4ae commit 90a62f6

File tree

8 files changed

+469
-467
lines changed

8 files changed

+469
-467
lines changed

app/location/__init__.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""app.location"""
22
from ..coordinates import Coordinates
3-
from ..utils import countries
4-
from ..utils.populations import country_population
5-
3+
from ..utils import Country
64

75
# pylint: disable=redefined-builtin,invalid-name
86
class Location: # pylint: disable=too-many-instance-attributes
@@ -11,12 +9,12 @@ class Location: # pylint: disable=too-many-instance-attributes
119
"""
1210

1311
def __init__(
14-
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
12+
self, id, country, coordinates, last_updated, confirmed, deaths, recovered,
1513
): # pylint: disable=too-many-arguments
1614
# General info.
1715
self.id = id
18-
self.country = country.strip()
19-
self.province = province.strip()
16+
self.country = country.name.strip()
17+
self.province = country.province.strip()
2018
self.coordinates = coordinates
2119

2220
# Last update.
@@ -27,26 +25,6 @@ def __init__(
2725
self.deaths = deaths
2826
self.recovered = recovered
2927

30-
@property
31-
def country_code(self):
32-
"""
33-
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
34-
35-
:returns: The country code.
36-
:rtype: str
37-
"""
38-
return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper()
39-
40-
@property
41-
def country_population(self):
42-
"""
43-
Gets the population of this location.
44-
45-
:returns: The population.
46-
:rtype: int
47-
"""
48-
return country_population(self.country_code)
49-
5028
def serialize(self):
5129
"""
5230
Serializes the location into a dict.
@@ -57,10 +35,10 @@ def serialize(self):
5735
return {
5836
# General info.
5937
"id": self.id,
60-
"country": self.country,
61-
"country_code": self.country_code,
62-
"country_population": self.country_population,
63-
"province": self.province,
38+
"country": self.country.name,
39+
"country_code": self.country.country_code,
40+
"country_population": self.country.country_population,
41+
"province": self.country.province,
6442
# Coordinates.
6543
"coordinates": self.coordinates.serialize(),
6644
# Last updated.
@@ -80,12 +58,12 @@ class TimelinedLocation(Location):
8058
"""
8159

8260
# pylint: disable=too-many-arguments
83-
def __init__(self, id, country, province, coordinates, last_updated, timelines):
61+
def __init__(self, id, country, coordinates, last_updated, timelines):
8462
super().__init__(
8563
# General info.
8664
id,
87-
country,
88-
province,
65+
country.name,
66+
country.province,
8967
coordinates,
9068
last_updated,
9169
# Statistics (retrieve latest from timelines).

app/location/csbs.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
"""app.locations.csbs.py"""
22
from . import Location
3-
3+
from ..utils import Country
44

55
class CSBSLocation(Location):
66
"""
77
A CSBS (county) location.
88
"""
99

1010
# pylint: disable=too-many-arguments,redefined-builtin
11-
def __init__(self, id, state, county, coordinates, last_updated, confirmed, deaths):
11+
def __init__(self, id, country, coordinates, last_updated, confirmed, deaths):
1212
super().__init__(
1313
# General info.
1414
id,
15-
"US",
16-
state,
15+
country,
1716
coordinates,
1817
last_updated,
1918
# Statistics.
@@ -22,8 +21,8 @@ def __init__(self, id, state, county, coordinates, last_updated, confirmed, deat
2221
recovered=0,
2322
)
2423

25-
self.state = state
26-
self.county = county
24+
self.state = country.state
25+
self.county = country.county
2726

2827
def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
2928
"""
@@ -36,7 +35,7 @@ def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused
3635

3736
# Update with new fields.
3837
serialized.update(
39-
{"state": self.state, "county": self.county,}
38+
{"state": self.country.state, "county": self.country.county,}
4039
)
4140

4241
# Return the serialized location.

app/location/nyt.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"""app.locations.nyt.py"""
22
from . import TimelinedLocation
3-
3+
from ..utils import Country
44

55
class NYTLocation(TimelinedLocation):
66
"""
77
A NYT (county) Timelinedlocation.
88
"""
99

1010
# pylint: disable=too-many-arguments,redefined-builtin
11-
def __init__(self, id, state, county, coordinates, last_updated, timelines):
12-
super().__init__(id, "US", state, coordinates, last_updated, timelines)
11+
def __init__(self, id, country, coordinates, last_updated, timelines):
12+
super().__init__(id, country, coordinates, last_updated, timelines)
1313

14-
self.state = state
15-
self.county = county
14+
self.state = country.state
15+
self.county = country.county
1616

1717
def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
1818
"""
@@ -25,7 +25,7 @@ def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused
2525

2626
# Update with new fields.
2727
serialized.update(
28-
{"state": self.state, "county": self.county,}
28+
{"state": self.country.state, "county": self.country.county,}
2929
)
3030

3131
# Return the serialized location.

app/services/location/csbs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from ...coordinates import Coordinates
1111
from ...location.csbs import CSBSLocation
1212
from ...utils import httputils
13+
from ...utils import Country
1314
from . import LocationService
1415

1516
LOGGER = logging.getLogger("services.location.csbs")
@@ -79,8 +80,7 @@ async def get_locations():
7980
CSBSLocation(
8081
# General info.
8182
i,
82-
state,
83-
county,
83+
Country("","",state,county),
8484
# Coordinates.
8585
Coordinates(item["Latitude"], item["Longitude"]),
8686
# Last update (parse as ISO).

app/services/location/jhu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ...coordinates import Coordinates
1313
from ...location import TimelinedLocation
1414
from ...models import Timeline
15-
from ...utils import countries
15+
from ...utils import Country
1616
from ...utils import date as date_util
1717
from ...utils import httputils
1818
from . import LocationService
@@ -98,7 +98,7 @@ async def get_category(category):
9898
{
9999
# General info.
100100
"country": country,
101-
"country_code": countries.country_code(country),
101+
"country_code": Country().country_code(country),
102102
"province": item["Province/State"],
103103
# Coordinates.
104104
"coordinates": {"lat": item["Lat"], "long": item["Long"],},

app/services/location/nyt.py

Lines changed: 2 additions & 2 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 ...utils import Country
1516

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

@@ -113,8 +114,7 @@ async def get_locations():
113114
locations.append(
114115
NYTLocation(
115116
id=idx,
116-
state=county_state[1],
117-
county=county_state[0],
117+
country=Country("","",county_state[1],county=county_state[0]),
118118
coordinates=Coordinates(None, None), # NYT does not provide coordinates
119119
last_updated=datetime.utcnow().isoformat() + "Z", # since last request
120120
timelines={

0 commit comments

Comments
 (0)