Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 11 additions & 33 deletions app/location/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""app.location"""
from ..coordinates import Coordinates
from ..utils import countries
from ..utils.populations import country_population

from ..utils import Country

# pylint: disable=redefined-builtin,invalid-name
class Location: # pylint: disable=too-many-instance-attributes
Expand All @@ -11,12 +9,12 @@ class Location: # pylint: disable=too-many-instance-attributes
"""

def __init__(
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
self, id, country, coordinates, last_updated, confirmed, deaths, recovered,
): # pylint: disable=too-many-arguments
# General info.
self.id = id
self.country = country.strip()
self.province = province.strip()
self.country = country.name.strip()
self.province = country.province.strip()
self.coordinates = coordinates

# Last update.
Expand All @@ -27,26 +25,6 @@ def __init__(
self.deaths = deaths
self.recovered = recovered

@property
def country_code(self):
"""
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.

:returns: The country code.
:rtype: str
"""
return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper()

@property
def country_population(self):
"""
Gets the population of this location.

:returns: The population.
:rtype: int
"""
return country_population(self.country_code)

def serialize(self):
"""
Serializes the location into a dict.
Expand All @@ -57,10 +35,10 @@ def serialize(self):
return {
# General info.
"id": self.id,
"country": self.country,
"country_code": self.country_code,
"country_population": self.country_population,
"province": self.province,
"country": self.country.name,
"country_code": self.country.country_code,
"country_population": self.country.country_population,
"province": self.country.province,
# Coordinates.
"coordinates": self.coordinates.serialize(),
# Last updated.
Expand All @@ -80,12 +58,12 @@ class TimelinedLocation(Location):
"""

# pylint: disable=too-many-arguments
def __init__(self, id, country, province, coordinates, last_updated, timelines):
def __init__(self, id, country, coordinates, last_updated, timelines):
super().__init__(
# General info.
id,
country,
province,
country.name,
country.province,
coordinates,
last_updated,
# Statistics (retrieve latest from timelines).
Expand Down
13 changes: 6 additions & 7 deletions app/location/csbs.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
"""app.locations.csbs.py"""
from . import Location

from ..utils import Country

class CSBSLocation(Location):
"""
A CSBS (county) location.
"""

# pylint: disable=too-many-arguments,redefined-builtin
def __init__(self, id, state, county, coordinates, last_updated, confirmed, deaths):
def __init__(self, id, country, coordinates, last_updated, confirmed, deaths):
super().__init__(
# General info.
id,
"US",
state,
country,
coordinates,
last_updated,
# Statistics.
Expand All @@ -22,8 +21,8 @@ def __init__(self, id, state, county, coordinates, last_updated, confirmed, deat
recovered=0,
)

self.state = state
self.county = county
self.state = country.state
self.county = country.county

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

# Update with new fields.
serialized.update(
{"state": self.state, "county": self.county,}
{"state": self.country.state, "county": self.country.county,}
)

# Return the serialized location.
Expand Down
12 changes: 6 additions & 6 deletions app/location/nyt.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"""app.locations.nyt.py"""
from . import TimelinedLocation

from ..utils import Country

class NYTLocation(TimelinedLocation):
"""
A NYT (county) Timelinedlocation.
"""

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

self.state = state
self.county = county
self.state = country.state
self.county = country.county

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

# Update with new fields.
serialized.update(
{"state": self.state, "county": self.county,}
{"state": self.country.state, "county": self.country.county,}
)

# Return the serialized location.
Expand Down
4 changes: 2 additions & 2 deletions app/services/location/csbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ...coordinates import Coordinates
from ...location.csbs import CSBSLocation
from ...utils import httputils
from ...utils import Country
from . import LocationService

LOGGER = logging.getLogger("services.location.csbs")
Expand Down Expand Up @@ -79,8 +80,7 @@ async def get_locations():
CSBSLocation(
# General info.
i,
state,
county,
Country("","",state,county),
# Coordinates.
Coordinates(item["Latitude"], item["Longitude"]),
# Last update (parse as ISO).
Expand Down
4 changes: 2 additions & 2 deletions app/services/location/jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ...coordinates import Coordinates
from ...location import TimelinedLocation
from ...models import Timeline
from ...utils import countries
from ...utils import Country
from ...utils import date as date_util
from ...utils import httputils
from . import LocationService
Expand Down Expand Up @@ -98,7 +98,7 @@ async def get_category(category):
{
# General info.
"country": country,
"country_code": countries.country_code(country),
"country_code": Country().country_code(country),
"province": item["Province/State"],
# Coordinates.
"coordinates": {"lat": item["Lat"], "long": item["Long"],},
Expand Down
4 changes: 2 additions & 2 deletions app/services/location/nyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ...models import Timeline
from ...utils import httputils
from . import LocationService
from ...utils import Country

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

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