From a0f5c008acec7bdf42e83d6c2d8ab13586f29521 Mon Sep 17 00:00:00 2001 From: Rostislav Svoboda Date: Fri, 27 Mar 2020 01:57:12 +0100 Subject: [PATCH] Renamings --- app/location/__init__.py | 4 ++-- app/services/location/jhu.py | 4 ++-- app/utils/{countrycodes.py => countries.py} | 19 +++++++++---------- app/utils/populations.py | 2 +- ...test_countrycodes.py => test_countries.py} | 12 ++++++------ 5 files changed, 20 insertions(+), 21 deletions(-) rename app/utils/{countrycodes.py => countries.py} (97%) rename tests/{test_countrycodes.py => test_countries.py} (53%) diff --git a/app/location/__init__.py b/app/location/__init__.py index cafcf547..4782fddb 100644 --- a/app/location/__init__.py +++ b/app/location/__init__.py @@ -1,5 +1,5 @@ from ..coordinates import Coordinates -from ..utils import countrycodes +from ..utils import countries from ..utils.populations import country_population @@ -31,7 +31,7 @@ def country_code(self): :returns: The country code. :rtype: str """ - return (countrycodes.country_code(self.country) or countrycodes.default_code).upper() + return (countries.country_code(self.country) or countries.default_country_code).upper() @property def country_population(self): diff --git a/app/services/location/jhu.py b/app/services/location/jhu.py index 9fde386f..ef99dddc 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -7,7 +7,7 @@ from ...coordinates import Coordinates from ...location import TimelinedLocation from ...timeline import Timeline -from ...utils import countrycodes +from ...utils import countries from ...utils import date as date_util from . import LocationService @@ -80,7 +80,7 @@ def get_category(category): { # General info. "country": country, - "country_code": countrycodes.country_code(country), + "country_code": countries.country_code(country), "province": item["Province/State"], # Coordinates. "coordinates": {"lat": item["Lat"], "long": item["Long"],}, diff --git a/app/utils/countrycodes.py b/app/utils/countries.py similarity index 97% rename from app/utils/countrycodes.py rename to app/utils/countries.py index f3e90e8f..aaec67a4 100644 --- a/app/utils/countrycodes.py +++ b/app/utils/countries.py @@ -4,13 +4,13 @@ LOGGER = logging.getLogger(__name__) # Default country code. -default_code = "XX" +default_country_code = "XX" # Mapping of country names to alpha-2 codes according to # https://en.wikipedia.org/wiki/ISO_3166-1. # As a reference see also https://github.com/TakahikoKawasaki/nv-i18n (in Java) # fmt: off -is_3166_1 = { +country_name__country_code = { "Afghanistan" : "AF", "Ă…land Islands" : "AX", "Albania" : "AL", @@ -269,13 +269,13 @@ "Iraq-Saudi Arabia Neutral Zone" : "XE", "Spratly Islands" : "XS", - # TODO "Disputed Territory" conflicts with `default_code` + # TODO "Disputed Territory" conflicts with `default_country_code` # "Disputed Territory" : "XX", } # Mapping of alternative names, spelling, typos to the names of countries used # by the ISO 3166-1 norm -synonyms = { +country_alias__country_name = { "Mainland China" : "China", "Czechia" : "Czech Republic", "Channel Islands" : "United Kingdom", @@ -372,16 +372,15 @@ def country_code(country): Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1 Defaults to "XX". """ - # Look in synonyms if not found. - if not country in is_3166_1 and country in synonyms: - country = synonyms[country] + if not country in country_name__country_code and country in country_alias__country_name: + country = country_alias__country_name[country] # Get country or fallback to default_code. - country_code = is_3166_1.get(country, default_code) + country_code = country_name__country_code.get(country, default_country_code) # Default picked? - if country_code == default_code: - LOGGER.warning(f"No country_code found for '{country}'. Using '{country_code}'!") + if country_code == default_country_code: + LOGGER.warning(f"No country code found for '{country}'. Using '{country_code}'!") # Return. return country_code diff --git a/app/utils/populations.py b/app/utils/populations.py index 8a78ec50..ea72c334 100644 --- a/app/utils/populations.py +++ b/app/utils/populations.py @@ -5,7 +5,7 @@ import requests from cachetools import TTLCache, cached -from .countrycodes import country_code +from .countries import country_code LOGGER = logging.getLogger(__name__) diff --git a/tests/test_countrycodes.py b/tests/test_countries.py similarity index 53% rename from tests/test_countrycodes.py rename to tests/test_countries.py index 1b132266..1d50421d 100644 --- a/tests/test_countrycodes.py +++ b/tests/test_countries.py @@ -1,6 +1,6 @@ import pytest -from app.utils import countrycodes +from app.utils import countries """ @@ -18,13 +18,13 @@ ("BlaBla", "XX"), ], ) -def test_countrycodes_is_3166_1(country_name, expected_country_code): - assert countrycodes.country_code(country_name) == expected_country_code +def test_countries_country_name__country_code(country_name, expected_country_code): + assert countries.country_code(country_name) == expected_country_code @pytest.mark.parametrize( - "country_name_synonym, expected_country_code", + "country_name_alias, expected_country_code", [("Deutschland", "DE"), ("Iran (Islamic Republic of)", "IR"), ("British Virgin Islands", "VG")], ) -def test_countrycodes_synonym(country_name_synonym, expected_country_code): - assert countrycodes.country_code(country_name_synonym) == expected_country_code +def test_country_name_alias(country_name_alias, expected_country_code): + assert countries.country_code(country_name_alias) == expected_country_code