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
8 changes: 3 additions & 5 deletions app/location/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions app/services/location/jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"],},
Expand Down
3 changes: 3 additions & 0 deletions app/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import .lookup
import .countries
import .populations
29 changes: 21 additions & 8 deletions app/utils/countries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""app.utils.countries.py"""
import logging
import lookup
from lookup import Lookup

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -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())
30 changes: 30 additions & 0 deletions app/utils/lookup.py
Original file line number Diff line number Diff line change
@@ -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
25 changes: 18 additions & 7 deletions app/utils/populations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
3 changes: 2 additions & 1 deletion tests/test_countries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from app.utils import countries
from app.utils import lookup

"""
Todo:
Expand All @@ -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