Skip to content
Closed
Changes from 1 commit
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
Next Next commit
The first aggregate pattern
  • Loading branch information
ZhiXuZhao committed Jul 24, 2021
commit 0e3d924c90a353f025662b2a8f79448ae2f6c69d
49 changes: 33 additions & 16 deletions app/location/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,42 @@
from ..utils import countries
from ..utils.populations import country_population

# put confirmed cases, deaths cases, recovered cases into one class
# Inseated of using confirmed cases, deaths cases, recovered cases as attributes, we can use CaseNumbers class instance as attribute
class CaseNumbers:
def __init__(self, confirmed = 0, deaths = 0, recovered = 0):
self.confirmed = confirmed
self.deaths = deaths
self.recovered = recovered

class Locationinfo:
def __init__(self, id, country, province, coordinates):
self.id = id
self.country = country.strip()
self.province = province.strip()
self.coordinates = coordinates




# pylint: disable=redefined-builtin,invalid-name
class Location: # pylint: disable=too-many-instance-attributes
"""
A location in the world affected by the coronavirus.
"""

# Use instance of class CaseNumbers as attribute
def __init__(
self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered,
self, locationinfo, last_updated, casenumbers
): # pylint: disable=too-many-arguments
# General info.
self.id = id
self.country = country.strip()
self.province = province.strip()
self.coordinates = coordinates
self.locationinfo = locationinfo

# Last update.
self.last_updated = last_updated

# Statistics.
self.confirmed = confirmed
self.deaths = deaths
self.recovered = recovered
self.casenumbers = casenumbers

@property
def country_code(self):
Expand Down Expand Up @@ -67,9 +80,9 @@ def serialize(self):
"last_updated": self.last_updated,
# Latest data (statistics).
"latest": {
"confirmed": self.confirmed,
"deaths": self.deaths,
"recovered": self.recovered,
"confirmed": self.casenumbers.confirmed,
"deaths": self.casenumbers.deaths,
"recovered": self.casenumbers.recovered,
},
}

Expand All @@ -80,7 +93,7 @@ class TimelinedLocation(Location):
"""

# pylint: disable=too-many-arguments
def __init__(self, id, country, province, coordinates, last_updated, timelines):
def __init__(self, id, country, province, coordinates, last_updated, timelines, casenumbers):
super().__init__(
# General info.
id,
Expand All @@ -89,11 +102,15 @@ def __init__(self, id, country, province, coordinates, last_updated, timelines):
coordinates,
last_updated,
# Statistics (retrieve latest from timelines).
confirmed=timelines.get("confirmed").latest or 0,
deaths=timelines.get("deaths").latest or 0,
recovered=timelines.get("recovered").latest or 0,
casenumbers
#confirmed=timelines.get("confirmed").latest or 0,
#deaths=timelines.get("deaths").latest or 0,
#recovered=timelines.get("recovered").latest or 0,
)

#set case numbers
self.casenumbers.confirmed=timelines.get("confirmed").latest or 0
self.casenumbers.deaths=timelines.get("deaths").latest or 0
self.casenumbers.recovered=timelines.get("recovered").latest or 0
# Set timelines.
self.timelines = timelines

Expand Down