Skip to content

Commit fb53072

Browse files
Ali TayyabiAli Tayyabi
authored andcommitted
structural pattern applied
1 parent 1c7e4ae commit fb53072

File tree

7 files changed

+79
-23
lines changed

7 files changed

+79
-23
lines changed

app/location/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""app.location"""
22
from ..coordinates import Coordinates
3-
from ..utils import countries
4-
from ..utils.populations import country_population
5-
3+
from ..utils import lookup
64

75
# pylint: disable=redefined-builtin,invalid-name
86
class Location: # pylint: disable=too-many-instance-attributes
@@ -35,7 +33,7 @@ def country_code(self):
3533
:returns: The country code.
3634
:rtype: str
3735
"""
38-
return (countries.country_code(self.country) or countries.DEFAULT_COUNTRY_CODE).upper()
36+
return lookup.get('country_code',self.country).upper()
3937

4038
@property
4139
def country_population(self):
@@ -45,7 +43,7 @@ def country_population(self):
4543
:returns: The population.
4644
:rtype: int
4745
"""
48-
return country_population(self.country_code)
46+
return lookup.get('country_population',self.country_code)
4947

5048
def serialize(self):
5149
"""

app/services/location/jhu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ...coordinates import Coordinates
1313
from ...location import TimelinedLocation
1414
from ...models import Timeline
15-
from ...utils import countries
15+
from ...utils import lookup
1616
from ...utils import date as date_util
1717
from ...utils import httputils
1818
from . import LocationService
@@ -98,7 +98,7 @@ async def get_category(category):
9898
{
9999
# General info.
100100
"country": country,
101-
"country_code": countries.country_code(country),
101+
"country_code": lookup.get('country_code',country),
102102
"province": item["Province/State"],
103103
# Coordinates.
104104
"coordinates": {"lat": item["Lat"], "long": item["Long"],},

app/utils/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import lookup
2+
import countries
3+
import populations

app/utils/countries.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""app.utils.countries.py"""
22
import logging
3+
import lookup
4+
from lookup import Lookup
35

46
LOGGER = logging.getLogger(__name__)
57

@@ -367,14 +369,25 @@
367369
}
368370

369371
# fmt: on
370-
def country_code(value):
372+
373+
class CountryCodeLookup(Lookup):
371374
"""
372-
Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1
373-
Defaults to "XX".
375+
This is a Lookup data class for returning the country code for county name
374376
"""
375-
code = COUNTRY_NAME__COUNTRY_CODE.get(value, DEFAULT_COUNTRY_CODE)
376-
if code == DEFAULT_COUNTRY_CODE:
377-
# log at sub DEBUG level
378-
LOGGER.log(5, f"No country code found for '{value}'. Using '{code}'!")
377+
def __init__(self):
378+
self.type = "country_code"
379+
380+
def get(self, value, default=DEFAULT_COUNTRY_CODE):
381+
"""
382+
Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1
383+
Defaults to "XX".
384+
"""
385+
code = COUNTRY_NAME__COUNTRY_CODE.get(value, default)
386+
if code == default:
387+
# log at sub DEBUG level
388+
LOGGER.log(5, f"No country code found for '{value}'. Using '{code}'!")
389+
390+
return code
379391

380-
return code
392+
# register an instance of CountryCodeLookup in lookup registry
393+
lookup.register_lookup(CountryCodeLookup())

app/utils/lookup.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
class Lookup():
3+
"""
4+
abstract lookup class
5+
"""
6+
7+
def __init__(self, type):
8+
self.type = type
9+
10+
@abstractmethod
11+
def get(self, value, default):
12+
raise NotImplementedError
13+
14+
# this will keep track of all the lookup data classes
15+
data_lookup_list = dict()
16+
17+
def register_lookup(lookupObject : Lookup)->None:
18+
"""
19+
add the lookup class instance to the lookup registry list
20+
"""
21+
data_lookup_list[lookupObject.type] = lookupObject
22+
23+
def get(lookup_type, key):
24+
"""
25+
return the lookup value associated with the `key` for the lookup
26+
data type `typev`
27+
"""
28+
if lookup_type in data_lookup_list:
29+
return data_lookup_list[lookup_type].get(key)
30+
return None

app/utils/populations.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import requests
66

77
import app.io
8+
import lookup
9+
from lookup import Lookup
810

911
LOGGER = logging.getLogger(__name__)
1012
GEONAMES_URL = "http://api.geonames.org/countryInfoJSON"
@@ -49,12 +51,21 @@ def fetch_populations(save=False):
4951
# Mapping of alpha-2 codes country codes to population.
5052
POPULATIONS = fetch_populations()
5153

52-
# Retrieving.
53-
def country_population(country_code, default=None):
54+
class CountryPopulationLookup(Lookup):
5455
"""
55-
Fetches the population of the country with the provided country code.
56-
57-
:returns: The population.
58-
:rtype: int
56+
This is a lookup data class for returning population for a country code
5957
"""
60-
return POPULATIONS.get(country_code, default)
58+
def __init__(self):
59+
self.type = "country_population"
60+
61+
def get(self, value, default=DEFAULT_COUNTRY_CODE):
62+
"""
63+
Fetches the population of the country with the provided country code.
64+
65+
:returns: The population.
66+
:rtype: int
67+
"""
68+
return POPULATIONS.get(country_code, default)
69+
70+
# register an instance of CountryPopulationLookup in lookup registry
71+
lookup.register_lookup(CountryPopulationLookup())

tests/test_countries.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from app.utils import countries
4+
from app.utils import lookup
45

56
"""
67
Todo:
@@ -20,4 +21,4 @@
2021
],
2122
)
2223
def test_countries_country_name__country_code(country_name, expected_country_code):
23-
assert countries.country_code(country_name) == expected_country_code
24+
assert lookup.get('country_code',country_name) == expected_country_code

0 commit comments

Comments
 (0)