From 7274f963f0d3d521d1e6533d8ce06db28c42da94 Mon Sep 17 00:00:00 2001 From: Billa32 <87912290+Billa32@users.noreply.github.com> Date: Sun, 25 Jul 2021 23:24:43 -0400 Subject: [PATCH 1/4] Create baseurls.py reason of creating a base_url cause it was assigned hardcoded in scripts and it is better to set hardcoded variables seperately in a class thus created a new baseurl --- app/utils/baseurls.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 app/utils/baseurls.py diff --git a/app/utils/baseurls.py b/app/utils/baseurls.py new file mode 100644 index 00000000..ccebf743 --- /dev/null +++ b/app/utils/baseurls.py @@ -0,0 +1,10 @@ +import enum + +class BaseUrl(str, enum.Enum): + """ + A base url available for retrieving data. + """ + + JHU = "https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/" + CSBS = "https://facts.csbs.org/covid-19/covid19_county.csv" + NYT = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv" From c3d8c6ec538e8d9c75a917e7665aeb39e276a19d Mon Sep 17 00:00:00 2001 From: Billa32 <87912290+Billa32@users.noreply.github.com> Date: Sat, 14 Aug 2021 16:47:17 -0400 Subject: [PATCH 2/4] Creational Pattern Pull Request) Why I did it? I have used the factory design pattern to do my creational pattern where i had to provide an interface for creating objects in superclass, but allowing subclasses to alter the type of objects that will be created. How i did it? I created a new interface class for location to implement creational pattern and the interface for location class is missing so i just added an interface class and serialized the location into a dict. --- app/location/__init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/location/__init__.py b/app/location/__init__.py index 1da5e9e5..af29e390 100644 --- a/app/location/__init__.py +++ b/app/location/__init__.py @@ -3,9 +3,18 @@ from ..utils import countries from ..utils.populations import country_population +from abc import abstractmethod, ABCMeta + +class ILocation(metaclass=ABCMeta): + + @abstractmethod + def serialize(): + """ + Serializes the location into a dict. + """ # pylint: disable=redefined-builtin,invalid-name -class Location: # pylint: disable=too-many-instance-attributes +class Location(ILocation): # pylint: disable=too-many-instance-attributes """ A location in the world affected by the coronavirus. """ @@ -74,6 +83,7 @@ def serialize(self): } + class TimelinedLocation(Location): """ A location with timelines. From 976b8d41db905f8e0a84ce5a2adcf565c1f9e2e4 Mon Sep 17 00:00:00 2001 From: Billa32 <87912290+Billa32@users.noreply.github.com> Date: Sun, 15 Aug 2021 21:09:00 -0400 Subject: [PATCH 3/4] Update __init__.py --- app/location/__init__.py | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/app/location/__init__.py b/app/location/__init__.py index af29e390..9e505631 100644 --- a/app/location/__init__.py +++ b/app/location/__init__.py @@ -82,7 +82,81 @@ def serialize(self): }, } +# pylint: disable=redefined-builtin,invalid-name +class LocationAdapter(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, + ): # 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 + + @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. + + :returns: The serialized location. + :rtype: dict + """ + serialized = { + # General info. + "id": self.id, + "country": self.country, + "country_code": self.country_code, + "country_population": self.country_population, + "province": self.province, + # Coordinates. + "coordinates": self.coordinates.serialize(), + # Last updated. + "last_updated": self.last_updated, + # Latest data (statistics). + "latest": { + "confirmed": self.confirmed, + "deaths": self.deaths, + "recovered": self.recovered, + }, + } + + # Update with new fields. + serialized.update( + {"state": self.province, "county": self.county,} + ) + + return serialized class TimelinedLocation(Location): """ From fbcbb6a374ae30aa20bfc3c5a045f72a8ff90611 Mon Sep 17 00:00:00 2001 From: Billa32 <87912290+Billa32@users.noreply.github.com> Date: Sun, 15 Aug 2021 21:10:55 -0400 Subject: [PATCH 4/4] Update csbs.py --- app/location/csbs.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/app/location/csbs.py b/app/location/csbs.py index 649e8b22..d73dfe12 100644 --- a/app/location/csbs.py +++ b/app/location/csbs.py @@ -1,8 +1,8 @@ """app.locations.csbs.py""" -from . import Location +from . import LocationAdapter -class CSBSLocation(Location): +class CSBSLocation(LocationAdapter): """ A CSBS (county) location. """ @@ -34,10 +34,5 @@ def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused """ serialized = super().serialize() - # Update with new fields. - serialized.update( - {"state": self.state, "county": self.county,} - ) - # Return the serialized location. return serialized