diff --git a/app/location/__init__.py b/app/location/__init__.py index 1da5e9e5..f46cead3 100644 --- a/app/location/__init__.py +++ b/app/location/__init__.py @@ -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): @@ -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): @@ -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, }, } @@ -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, @@ -122,3 +253,4 @@ def serialize(self, timelines=False): # Return the serialized location. return serialized +''' \ No newline at end of file diff --git a/app/location/csbs.py b/app/location/csbs.py index 649e8b22..43710aa2 100644 --- a/app/location/csbs.py +++ b/app/location/csbs.py @@ -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 """ diff --git a/app/location/nyt.py b/app/location/nyt.py index ad92212e..e3b85eb7 100644 --- a/app/location/nyt.py +++ b/app/location/nyt.py @@ -1,5 +1,5 @@ """app.locations.nyt.py""" -from . import TimelinedLocation +from . import Director, TimelinedLocationBuilder, BaseInfo, GeoInfo, Statistic class NYTLocation(TimelinedLocation): @@ -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 """