diff --git a/app/location/__init__.py b/app/location/__init__.py index 1da5e9e5..eb40441c 100644 --- a/app/location/__init__.py +++ b/app/location/__init__.py @@ -4,8 +4,88 @@ from ..utils.populations import country_population +class Location: + def __init__( + self, id, country, province, 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.coordinates = coordinates + + # Last update. + self.last_updated = last_updated + + # Statistics. + self.confirmed = confirmed + self.deaths = deaths + self.recovered = recovered + + def add(self, location): + pass + + def remove(self, location): + pass + + def country_code(self): + pass + + def country_population(self): + pass + + def serialize(self): + pass + + +class Province(Location): + countries = None + + def __init__( + self, id, country, province, coordinates, last_updated, confirmed, deaths, recovered, + ): # pylint: disable=too-many-arguments + # General info. + self.id = id + self.country = country.strip() + self.province = None + self.coordinates = coordinates + + # Last update. + self.last_updated = last_updated + + # Statistics. + self.confirmed = confirmed + self.deaths = deaths + self.recovered = recovered + self.countries = [] + + def add(self, location): + self.countries.append(location) + + def remove(self, location): + self.countries.remove(location) + + def country_code(self): + countries_code = [] + for i in countries + countries_code.append(countries.country_code(i)) + return countries_code + + def country_population(self): + countries_population = 0 + for i in countries + countries_population=countries_population+ country_population(i) + return countries_population + + def serialize(self): + for i in countries + i.serialize() + + + + # pylint: disable=redefined-builtin,invalid-name -class Location: # pylint: disable=too-many-instance-attributes +class Country(Location): # pylint: disable=too-many-instance-attributes """ A location in the world affected by the coronavirus. """ diff --git a/app/services/location/__init__.py b/app/services/location/__init__.py index 6d292b54..123552a5 100644 --- a/app/services/location/__init__.py +++ b/app/services/location/__init__.py @@ -1,7 +1,17 @@ """app.services.location""" from abc import ABC, abstractmethod +def singleton(cls): + instances = {} + def wrapper(*args, **kwargs): + if cls not in instances: + instances[cls] = cls(*args, **kwargs) + return instances[cls] + + return wrapper + +@singleton class LocationService(ABC): """ Service for retrieving locations. diff --git a/app/services/location/csbs.py b/app/services/location/csbs.py index 444ebad6..8d97e5ac 100644 --- a/app/services/location/csbs.py +++ b/app/services/location/csbs.py @@ -14,7 +14,7 @@ LOGGER = logging.getLogger("services.location.csbs") - +@singleton class CSBSLocationService(LocationService): """ Service for retrieving locations from csbs diff --git a/app/services/location/jhu.py b/app/services/location/jhu.py index ebed3960..ce58f1dd 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -21,6 +21,7 @@ PID = os.getpid() +@singleton class JhuLocationService(LocationService): """ Service for retrieving locations from Johns Hopkins CSSE (https://github.com/CSSEGISandData/COVID-19). diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py index 1f25ec34..54b01fef 100644 --- a/app/services/location/nyt.py +++ b/app/services/location/nyt.py @@ -16,6 +16,7 @@ LOGGER = logging.getLogger("services.location.nyt") +@singleton class NYTLocationService(LocationService): """ Service for retrieving locations from New York Times (https://github.com/nytimes/covid-19-data).