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
172 changes: 152 additions & 20 deletions app/location/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,163 @@
from ..coordinates import Coordinates
from ..utils import countries
from ..utils.populations import country_population
from abc import abstractmethod
class Builder:
def build_base(self) -> None:
pass
def build_stat(self) -> None:
pass
def build_geo(self) -> None:
pass
def build_timelines(self) -> None:
pass

class Location_Builder(Builder):
def __init__(self, basinfo, statistic, geoinfo) -> None:
self.reset()

def reset(self) -> None:
self._location = Location()

def locate(self) -> Location:
location = self._location
self.reset()
return location

def build_base(self, baseinfo) -> None:
self.baseinfo = baseinfo
def build_stat(self, statistic) -> None:
self.statistic = statistic
def build_geo(self, geoinfo) -> None:
self.geoinfo = geoinfo

class TimelinedLocation_Builder(Builder):
def __init__(self, baseinfo, statistic, geoinfo, timelines) -> None:
self.reset()

def reset(self) -> None:
self._location = TimelinedLocation()

def locate(self) -> TimelinedLocation:
timelined_location = self._timelined_location
self.reset()
return timelined_location

def build_base(self, baseinfo) -> None:
self.baseinfo = baseinfo
def build_stat(self, statistic) -> None:
self.statistic = statistic
def build_geo(self, geoinfo) -> None:
self.geoinfo = geoinfo
def build_timelines(self, timelines) -> None:
self.timelines = timelines

class Location:

def __init__(self):
self.id = None
self.last_updated = None
self.country_code = None
self.country_population = None
self.serialize = None
self.geoinfo = None
self.last_updated = None
self.statistic = None

def set_base(self, id, last_updated, serialize) -> None:
self.id = id
self.last_updated = last_updated
self.serialize = serialize
def set_geoinfo(self, geoinfo):
self._geoinfo = geoinfo
def set_statistic(self, statistic):
self._statistic = statistic

class TimelinedLocation:

def set_base(self, id, last_updated, serialize) -> None:
self.id = id
self.last_updated = last_updated
self.serialize = serialize
def set_geoinfo(self, geoinfo):
self._geoinfo = geoinfo
def set_timelines(self, timelines):
self._timelines = timelines

class Director:
def __init__(self) -> None:
self._builder = None

def set_builder(self, builder: Builder) -> None:
self._builder = builder

def build_location(self) -> None:
location = Location()
self.builder.build_location()
self.builder.build_base()
self.builder.build_stat()
self.builder.build_geo()

def build_timelinedlocation(self) -> None:
self.builder.build_location()
self.builder.build_base()
self.builder.build_stat()
self.builder.build_geo()
self.builder.build_timelines()

class Stastistic:
def __init__(self, confirmed, deaths, recovered):
self.confirmed = confirmed
self.deaths = deaths
self.recovered = recovered

class GeoInfo:
def __init__(self, country, province, coorinates):
self.country = country
self.province = province
self.coordinates = coorinates
self.country_code = (countries.country_code(self.geoinfo.country) or countries.DEFAULT_COUNTRY_CODE).upper()
self.country_population = country_population(self.country_code)

class BaseInfo:
def __init__(self, id, last_updated, serialize):
self.id = id
self.last_updated = last_updated
self.serialize = {
# General info.
"id": self.id,
"country": self.geoinfo.country,
"country_code": self.country_code,
"country_population": self.country_population,
"province": self.geoinfo.province,
# Coordinates.
"coordinates": self.geoinfo.coordinates.serialize(),
# Last updated.
"last_updated": self.last_updated,
# Latest data (statistics).
"latest": {
"confirmed": self.statistic.confirmed,
"deaths": self.statistic.deaths,
"recovered": self.statistic.recovered,
},
}
# pylint: disable=redefined-builtin,invalid-name
class Location: # pylint: disable=too-many-instance-attributes
'''class Location: # pylint: disable=too-many-instance-attributes
"""
A location in the world affected by the coronavirus.
"""

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

self.geoinfo = geoinfo
# Last update.
self.last_updated = last_updated

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

@property
def country_code(self):
Expand All @@ -35,7 +168,7 @@ def country_code(self):
:returns: The country code.
:rtype: str
"""
return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper()
return (countries.country_code(self.geoinfo.country) or countries.DEFAULT_COUNTRY_CODE).upper()

@property
def country_population(self):
Expand All @@ -57,19 +190,19 @@ def serialize(self):
return {
# General info.
"id": self.id,
"country": self.country,
"country": self.geoinfo.country,
"country_code": self.country_code,
"country_population": self.country_population,
"province": self.province,
"province": self.geoinfo.province,
# Coordinates.
"coordinates": self.coordinates.serialize(),
"coordinates": self.geoinfo.coordinates.serialize(),
# Last updated.
"last_updated": self.last_updated,
# Latest data (statistics).
"latest": {
"confirmed": self.confirmed,
"deaths": self.deaths,
"recovered": self.recovered,
"confirmed": self.statistic.confirmed,
"deaths": self.statistic.deaths,
"recovered": self.statistic.recovered,
},
}

Expand All @@ -80,13 +213,11 @@ class TimelinedLocation(Location):
"""

# pylint: disable=too-many-arguments
def __init__(self, id, country, province, coordinates, last_updated, timelines):
def __init__(self, id, geoinfo, last_updated, timelines):
super().__init__(
# General info.
id,
country,
province,
coordinates,
geoinfo,
last_updated,
# Statistics (retrieve latest from timelines).
confirmed=timelines.get("confirmed").latest or 0,
Expand Down Expand Up @@ -122,3 +253,4 @@ def serialize(self, timelines=False):

# Return the serialized location.
return serialized
'''
27 changes: 9 additions & 18 deletions app/location/csbs.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
"""app.locations.csbs.py"""
from . import Location
from . import Director, LocationBuilder, BaseInfo, GeoInfo, Statistic


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

# pylint: disable=too-many-arguments,redefined-builtin
def __init__(self, id, state, county, coordinates, last_updated, confirmed, deaths):
super().__init__(
# General info.
id,
"US",
state,
coordinates,
last_updated,
# Statistics.
confirmed=confirmed,
deaths=deaths,
recovered=0,
)

self.state = state
self.county = county
director = Director()
baseinfo = BaseInfo(id=id,last_updated=last_updated)
geoinfo = GeoInfo(county="US", province=state, coordinates=coordinates)
statistic = Statistic(confirmed=confirmed, deaths=deaths, recovered=0)
locationBuilder = LocationBuilder(baseinfo=baseinfo, statistic=statistic, geoinfo=geoinfo)
director.set_builder(LocationBuilder)
csbs = director.build_location()

def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
"""
Expand Down
11 changes: 7 additions & 4 deletions app/location/nyt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""app.locations.nyt.py"""
from . import TimelinedLocation
from . import Director, TimelinedLocationBuilder, BaseInfo, GeoInfo, Statistic


class NYTLocation(TimelinedLocation):
Expand All @@ -9,10 +9,13 @@ class NYTLocation(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)

self.state = state
self.county = county
director = Director()
baseinfo = BaseInfo(id=id,last_updated=last_updated)
geoinfo = GeoInfo(county="US", province=state, coordinates=coordinates)
locationBuilder = TimelinedLocationBuilder(baseinfo=baseinfo, geoinfo=geoinfo, timelines=timelines)
director.set_builder(TimelinedLocationBuilder)
nyt = director.build_location()

def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument
"""
Expand Down
28 changes: 10 additions & 18 deletions app/services/location/__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
"""app.services.location"""
from abc import ABC, abstractmethod


class LocationService(ABC):
class LocationService:
"""
Service for retrieving locations.
"""

@abstractmethod
async def get_all(self):
"""
Gets and returns all of the locations.
# Get the locations.
locations = await get_locations()
return locations

:returns: The locations.
:rtype: List[Location]
"""
raise NotImplementedError
async def get(self, loc_id): # pylint: disable=arguments-differ
# Get location at the index equal to the provided id.
locations = await self.get_all()
return locations[loc_id]

@abstractmethod
async def get(self, id): # pylint: disable=redefined-builtin,invalid-name
"""
Gets and returns location with the provided id.

:returns: The location.
:rtype: Location
"""
raise NotImplementedError
async def get_locations():
raise NotImplementedError
Loading