Skip to content

Commit 8c7a24e

Browse files
authored
Add include_unofficial option (#72)
1 parent e6e2001 commit 8c7a24e

File tree

7 files changed

+217
-46
lines changed

7 files changed

+217
-46
lines changed

documentation/index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ The usage of the country mappings functionality is best illustrated by some exam
7070

7171
from hdx.location.country import Country
7272

73-
Country.countriesdata(use_live=False, country_name_overrides={"PSE": "oPt"})
74-
# Set up using non live data from repo rather and override default country name
75-
# (Leaving out this step will use live data and no overrides)
73+
Country.countriesdata(include_unofficial=True, use_live=False, country_name_overrides={"PSE": "oPt"})
74+
# Set up including unofficial alpha2 (eg. AN) and alpha3 codes (eg. XKX)
75+
# Use non live data from repo and override default country name
76+
# (Leaving out this step will exclude unofficial alpha codes, use live data and no overrides)
7677
Country.get_country_name_from_iso3("jpn", use_live=False) # returns "Japan"
7778
Country.get_country_name_from_iso3("vEn", formal=True) # returns "the Bolivarian Republic of Venezuela"
7879
# uselive=False forces the use of internal files instead of accessing the live feeds.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ classifiers = [
3434
requires-python = ">=3.8"
3535

3636
dependencies = [
37-
"hdx-python-utilities>=3.8.6",
37+
"hdx-python-utilities>=3.8.7",
3838
"libhxl>=5.2.2",
3939
"tenacity",
4040
]

requirements.txt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ colorama==0.4.6
3030
# via mkdocs-material
3131
coverage==7.8.0
3232
# via pytest-cov
33-
cydifflib==1.2.0
34-
# via hdx-python-utilities
3533
distlib==0.3.9
3634
# via virtualenv
3735
et-xmlfile==2.0.0
@@ -42,15 +40,15 @@ frictionless==5.18.1
4240
# via hdx-python-utilities
4341
ghp-import==2.1.0
4442
# via mkdocs
45-
hdx-python-utilities==3.8.6
43+
hdx-python-utilities==3.8.7
4644
# via hdx-python-country (pyproject.toml)
4745
humanize==4.12.3
4846
# via frictionless
4947
identify==2.6.10
5048
# via pre-commit
5149
idna==3.10
5250
# via requests
53-
ijson==3.3.0
51+
ijson==3.4.0
5452
# via hdx-python-utilities
5553
iniconfig==2.1.0
5654
# via pytest
@@ -95,15 +93,15 @@ mergedeep==1.3.4
9593
# via
9694
# mkdocs
9795
# mkdocs-get-deps
98-
mkapi==4.3.2
96+
mkapi==4.4.0
9997
# via hdx-python-country (pyproject.toml)
10098
mkdocs==1.6.1
10199
# via
102100
# mkapi
103101
# mkdocs-material
104102
mkdocs-get-deps==0.2.0
105103
# via mkdocs
106-
mkdocs-material==9.6.12
104+
mkdocs-material==9.6.14
107105
# via mkapi
108106
mkdocs-material-extensions==1.3.1
109107
# via mkdocs-material
@@ -121,11 +119,11 @@ pathspec==0.12.1
121119
# via mkdocs
122120
petl==1.7.16
123121
# via frictionless
124-
platformdirs==4.3.7
122+
platformdirs==4.3.8
125123
# via
126124
# mkdocs-get-deps
127125
# virtualenv
128-
pluggy==1.5.0
126+
pluggy==1.6.0
129127
# via pytest
130128
ply==3.11
131129
# via
@@ -170,7 +168,7 @@ pyyaml==6.0.2
170168
# pymdown-extensions
171169
# pyyaml-env-tag
172170
# tableschema-to-template
173-
pyyaml-env-tag==0.1
171+
pyyaml-env-tag==1.1
174172
# via mkdocs
175173
ratelimit==2.2.1
176174
# via hdx-python-utilities
@@ -190,7 +188,7 @@ rfc3986==2.0.0
190188
# via frictionless
191189
rich==14.0.0
192190
# via typer
193-
rpds-py==0.24.0
191+
rpds-py==0.25.0
194192
# via
195193
# jsonschema
196194
# referencing
@@ -214,7 +212,7 @@ tenacity==9.1.2
214212
# via hdx-python-country (pyproject.toml)
215213
text-unidecode==1.3
216214
# via python-slugify
217-
typer==0.15.3
215+
typer==0.15.4
218216
# via frictionless
219217
typing-extensions==4.13.2
220218
# via
@@ -236,7 +234,7 @@ urllib3==2.4.0
236234
# requests
237235
validators==0.35.0
238236
# via frictionless
239-
virtualenv==20.31.1
237+
virtualenv==20.31.2
240238
# via pre-commit
241239
watchdog==6.0.0
242240
# via mkdocs

src/hdx/location/Countries & Territories Taxonomy MVP - C&T Taxonomy with HXL Tags.csv

Lines changed: 6 additions & 6 deletions
Large diffs are not rendered by default.

src/hdx/location/country.py

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import Dict, List, Optional, Tuple, Union
99

1010
import hxl
11-
from hxl import InputOptions
11+
from hxl import Dataset, InputOptions
1212

1313
from hdx.utilities.path import script_dir_plus_file
1414
from hdx.utilities.text import get_words_in_sentence
@@ -69,6 +69,8 @@ class Country:
6969
"KINGDOM",
7070
"PROTECTORATE",
7171
]
72+
_include_unofficial_default = False
73+
_include_unofficial = _include_unofficial_default
7274
_use_live_default = True
7375
_use_live = _use_live_default
7476
_countriesdata = None
@@ -102,6 +104,8 @@ def _add_countriesdata(cls, iso3: str, hxlcountry: hxl.Row) -> Dict:
102104
if countryname is not None:
103105
country["#country+name+override"] = countryname
104106
iso2 = hxlcountry.get("#country+code+v_iso2")
107+
if not iso2 and cls._include_unofficial:
108+
iso2 = hxlcountry.get("#country+code+alpha2")
105109
if iso2:
106110
cls._countriesdata["iso2iso3"][iso2] = iso3
107111
# different types so keys won't clash
@@ -159,12 +163,12 @@ def add_country_to_set(colname, idval, iso3):
159163
return country
160164

161165
@classmethod
162-
def set_countriesdata(cls, countries: str) -> None:
166+
def set_countriesdata(cls, countries: Dataset) -> None:
163167
"""
164-
Set up countries data from data in form provided by UNStats and World Bank
168+
Set up countries data from OCHA countries and territories dataset
165169
166170
Args:
167-
countries (str): Countries data in HTML format provided by UNStats
171+
countries (Dataset): Countries data from countries and territories dataset
168172
169173
Returns:
170174
None
@@ -186,7 +190,12 @@ def set_countriesdata(cls, countries: str) -> None:
186190
for country in countries:
187191
iso3 = country.get("#country+code+v_iso3")
188192
if not iso3:
189-
continue
193+
if cls._include_unofficial:
194+
iso3 = country.get("#country+code+alpha3")
195+
if not iso3:
196+
continue
197+
else:
198+
continue
190199
iso3 = iso3.upper()
191200
countrydict = cls._add_countriesdata(iso3, country)
192201
cls._countriesdata["countries"][iso3] = countrydict
@@ -202,30 +211,37 @@ def sort_list(colname):
202211
@classmethod
203212
def countriesdata(
204213
cls,
205-
use_live: bool = None,
206-
country_name_overrides: Dict = None,
207-
country_name_mappings: Dict = None,
214+
include_unofficial: Optional[bool] = None,
215+
use_live: Optional[bool] = None,
216+
country_name_overrides: Optional[Dict] = None,
217+
country_name_mappings: Optional[Dict] = None,
208218
) -> List[Dict[str, Dict]]:
209219
"""
210-
Read countries data from OCHA countries feed (falling back to file)
220+
Read countries data from OCHA countries feed (falling back to file).
221+
include_unofficial, use_live, country_name_overrides and country_name_mappings
222+
are taken from internal defaults if they are None, otherwise the internal
223+
defaults are overridden.
211224
212225
Args:
213-
use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True.
214-
country_name_overrides (Dict): Dictionary of mappings from iso3 to country name
215-
country_name_mappings (Dict): Dictionary of mappings from country name to iso3
226+
include_unofficial (Optional[bool]): Include unofficial country alpha codes. Defaults to False.
227+
use_live (Optional[bool]): Try to get latest data from web rather than file in package. Defaults to True.
228+
country_name_overrides (Optional[Dict]): Dictionary of mappings from iso3 to country name
229+
country_name_mappings (Optional[Dict]): Dictionary of mappings from country name to iso3
216230
217231
Returns:
218232
List[Dict[str,Dict]]: Countries dictionaries
219233
"""
220-
if use_live is None:
221-
use_live = cls._use_live
234+
if include_unofficial is not None:
235+
cls._include_unofficial = include_unofficial
236+
if use_live is not None:
237+
cls._use_live = use_live
222238
if cls._countriesdata is None:
223239
countries = None
224240
if country_name_overrides is not None:
225241
cls.set_country_name_overrides(country_name_overrides)
226242
if country_name_mappings is not None:
227243
cls.set_country_name_mappings(country_name_mappings)
228-
if use_live:
244+
if cls._use_live:
229245
try:
230246
countries = hxl.data(cls._ochaurl, InputOptions(encoding="utf-8"))
231247
except OSError:
@@ -240,6 +256,24 @@ def countriesdata(
240256
cls.set_countriesdata(countries)
241257
return cls._countriesdata
242258

259+
@classmethod
260+
def set_include_unofficial_default(
261+
cls, include_unofficial: Optional[bool] = None
262+
) -> None:
263+
"""
264+
Set the default for include_unofficial which defines if unofficial alpha2 and
265+
alpha3 codes such as AN and XKX will be included.
266+
267+
Args:
268+
include_unofficial (bool): Default value to use for include_unofficial. Defaults to internal value (False).
269+
270+
Returns:
271+
None
272+
"""
273+
if include_unofficial is None:
274+
include_unofficial = cls._include_unofficial
275+
cls._include_unofficial = include_unofficial
276+
243277
@classmethod
244278
def set_use_live_default(cls, use_live: Optional[bool] = None) -> None:
245279
"""

0 commit comments

Comments
 (0)