forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountries.py
More file actions
31 lines (23 loc) · 1.05 KB
/
countries.py
File metadata and controls
31 lines (23 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""app.utils.countries.py"""
import logging
import app.io
# Default country code.
# 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
# fmt: on
class CountryCodeUtil:
LOGGER = logging.getLogger(__name__)
DEFAULT_COUNTRY_CODE = "XX"
COUNTRY_NAME__COUNTRY_CODE_PATH = "country_name_country_code_path.json" #put that giant map in data folder as json
def __init__(self):
self.COUNTRY_NAME__COUNTRY_CODE = app.io.load(COUNTRY_NAME__COUNTRY_CODE_PATH)
def get_country_code(self,country_name):
code = self.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 '{country_name}'. Using '{code}'!")
return code
def add_country_code(self,country_name,country_code):
self.COUNTRY_NAME__COUNTRY_CODE[country_name] = country_code