diff --git a/app/location/__init__.py b/app/location/__init__.py index 1da5e9e5..3d189e12 100644 --- a/app/location/__init__.py +++ b/app/location/__init__.py @@ -1,8 +1,6 @@ """app.location""" from ..coordinates import Coordinates -from ..utils import countries -from ..utils.populations import country_population - +from ..utils import lookup # pylint: disable=redefined-builtin,invalid-name class Location: # pylint: disable=too-many-instance-attributes @@ -35,7 +33,7 @@ def country_code(self): :returns: The country code. :rtype: str """ - return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper() + return lookup.get('country_code',self.country).upper() @property def country_population(self): @@ -45,7 +43,7 @@ def country_population(self): :returns: The population. :rtype: int """ - return country_population(self.country_code) + return lookup.get('country_population',self.country_code) def serialize(self): """ diff --git a/app/services/location/jhu.py b/app/services/location/jhu.py index ebed3960..02cfc5ae 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -12,7 +12,7 @@ from ...coordinates import Coordinates from ...location import TimelinedLocation from ...models import Timeline -from ...utils import countries +from ...utils import lookup from ...utils import date as date_util from ...utils import httputils from . import LocationService @@ -98,7 +98,7 @@ async def get_category(category): { # General info. "country": country, - "country_code": countries.country_code(country), + "country_code": lookup.get('country_code',country), "province": item["Province/State"], # Coordinates. "coordinates": {"lat": item["Lat"], "long": item["Long"],}, diff --git a/app/utils/__init__.py b/app/utils/__init__.py index e69de29b..d9a3d6ac 100644 --- a/app/utils/__init__.py +++ b/app/utils/__init__.py @@ -0,0 +1,3 @@ +import .lookup +import .countries +import .populations diff --git a/app/utils/countries.py b/app/utils/countries.py index 9fb4f98a..87d58fa2 100644 --- a/app/utils/countries.py +++ b/app/utils/countries.py @@ -1,5 +1,7 @@ """app.utils.countries.py""" import logging +import lookup +from lookup import Lookup LOGGER = logging.getLogger(__name__) @@ -367,14 +369,25 @@ } # fmt: on -def country_code(value): + +class CountryCodeLookup(Lookup): """ - Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1 - Defaults to "XX". + This is a Lookup data class for returning the country code for county name """ - code = COUNTRY_NAME__COUNTRY_CODE.get(value, DEFAULT_COUNTRY_CODE) - if code == DEFAULT_COUNTRY_CODE: - # log at sub DEBUG level - LOGGER.log(5, f"No country code found for '{value}'. Using '{code}'!") + def __init__(self): + self.type = "country_code" + + def get(self, value, default=DEFAULT_COUNTRY_CODE): + """ + Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1 + Defaults to "XX". + """ + code = COUNTRY_NAME__COUNTRY_CODE.get(value, default) + if code == default: + # log at sub DEBUG level + LOGGER.log(5, f"No country code found for '{value}'. Using '{code}'!") + + return code - return code +# register an instance of CountryCodeLookup in lookup registry +lookup.register_lookup(CountryCodeLookup()) \ No newline at end of file diff --git a/app/utils/lookup.py b/app/utils/lookup.py new file mode 100644 index 00000000..1eb28935 --- /dev/null +++ b/app/utils/lookup.py @@ -0,0 +1,30 @@ + +class Lookup(): + """ + abstract lookup class + """ + + def __init__(self, type): + self.type = type + + @abstractmethod + def get(self, value, default): + raise NotImplementedError + +# this will keep track of all the lookup data classes +data_lookup_list = dict() + +def register_lookup(lookupObject : Lookup)->None: + """ + add the lookup class instance to the lookup registry list + """ + data_lookup_list[lookupObject.type] = lookupObject + +def get(lookup_type, key): + """ + return the lookup value associated with the `key` for the lookup + data type `typev` + """ + if lookup_type in data_lookup_list: + return data_lookup_list[lookup_type].get(key) + return None \ No newline at end of file diff --git a/app/utils/populations.py b/app/utils/populations.py index c02f15a9..f2ba1a51 100644 --- a/app/utils/populations.py +++ b/app/utils/populations.py @@ -5,6 +5,8 @@ import requests import app.io +import lookup +from lookup import Lookup LOGGER = logging.getLogger(__name__) GEONAMES_URL = "http://api.geonames.org/countryInfoJSON" @@ -49,12 +51,21 @@ def fetch_populations(save=False): # Mapping of alpha-2 codes country codes to population. POPULATIONS = fetch_populations() -# Retrieving. -def country_population(country_code, default=None): +class CountryPopulationLookup(Lookup): """ - Fetches the population of the country with the provided country code. - - :returns: The population. - :rtype: int + This is a lookup data class for returning population for a country code """ - return POPULATIONS.get(country_code, default) + def __init__(self): + self.type = "country_population" + + def get(self, value, default=DEFAULT_COUNTRY_CODE): + """ + Fetches the population of the country with the provided country code. + + :returns: The population. + :rtype: int + """ + return POPULATIONS.get(country_code, default) + +# register an instance of CountryPopulationLookup in lookup registry +lookup.register_lookup(CountryPopulationLookup()) diff --git a/tests/test_countries.py b/tests/test_countries.py index a9ce9c19..f920ea94 100644 --- a/tests/test_countries.py +++ b/tests/test_countries.py @@ -1,6 +1,7 @@ import pytest from app.utils import countries +from app.utils import lookup """ Todo: @@ -20,4 +21,4 @@ ], ) def test_countries_country_name__country_code(country_name, expected_country_code): - assert countries.country_code(country_name) == expected_country_code + assert lookup.get('country_code',country_name) == expected_country_code