Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-isoc committed Nov 15, 2024
1 parent 194b5ad commit dc9d1f8
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2,289 deletions.
246 changes: 123 additions & 123 deletions django_countries_hdx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,165 +1,71 @@
from pathlib import Path

from hdx.location.country import Country

from django_countries_hdx.country_regions import COUNTRY_REGIONS
from django_countries_hdx.regions import REGIONS, SUBREGIONS
from hdx.location.country import Country as HDX

# custom_file_url = Path(__file__).parent.resolve() / "hdx_plus_m49.csv"
# This next will need to change once our PR in the hdx lib has been merged
# Country.set_ocha_url(str(custom_file_url))
Country.set_use_live_default(False)


def get_country_data(country_code: str) -> dict[str, str] | None:
"""Retrieves annotated country information. Will accept either ISO2 or ISO3 country code.
:param country_code: ISO2 or ISO3 country code.
:return: Dictionary of country information with HXL hashtags as keys.
"""
if country_code is None:
return None

if len(country_code) == 2:
return Country.get_country_info_from_iso2(country_code)

return Country.get_country_info_from_iso3(country_code)


def get_region_name(region_code: int) -> str | None:
"""Retrieves region or sub-region name for a region code.
:param region_code: UN M49 region code.
:return: String. Region name
"""
if not region_code:
return None

try:
countriesdata = Country.countriesdata()
return countriesdata["regioncodes2names"].get(region_code) # noqa
except KeyError:
return None
HDX.set_use_live_default(False)

def get_countries_by_region() -> dict[int, dict[str, list[tuple[str, str]]]]:
"""Retrieves lists of countries keyed by region, with region name and country tuples.

:param regions: Boolean to determine whether to use regions or subregions.
:return: Dict. Keyed by region code, the value is a dictionary containing the
region name and a list of country_code, country_name tuples.
"""
region_codes = (2, 9, 19, 142, 150,)

return {
region_code: {
"name": get_region_name(region_code),
"countries": sorted(
[
(
Country.get_iso2_from_iso3(code),
Country.get_country_name_from_iso3(code)
)
for code in Country.get_countries_in_region(region_code)
],
key=lambda x: x[1] # Sort by country name
)
}
for region_code in sorted(region_codes, key=lambda x: get_region_name(x))
}


def get_countries_by_subregion() -> dict[int, dict[str, list[tuple[str, str]]]]:
"""Retrieves lists of countries keyed by region, with region name and country tuples.
:param regions: Boolean to determine whether to use regions or subregions.
:return: Dict. Keyed by region code, the value is a dictionary containing the
region name and a list of country_code, country_name tuples.
class Regions:
"""
subregion_codes = (
5, 11, 13, 14, 15, 17, 18, 21, 29, 30, 34, 35, 39, 53, 54, 57, 61, 143, 145, 151, 154, 155,
)

return {
region_code: {
"name": get_region_name(region_code),
"countries": sorted(
[
(
Country.get_iso2_from_iso3(code),
Country.get_country_name_from_iso3(code)
)
for code in Country.get_countries_in_region(region_code)
],
key=lambda x: x[1] # Sort by country name
)
}
for region_code in sorted(subregion_codes, key=lambda x: get_region_name(x))
}


def get_countries_in_region(region_code: int) -> list[tuple[str, str]] | None:
"""Retrieves lists of countries in a region/sub-region.
:return: List. ISO2 country_code, country_name tuples.
An object that can query a UN M49 list of geographical regions and subregions and return a list of countries in
that region.
"""
if not region_code:
return None

countries_iso3 = Country.get_countries_in_region(region_code)

countries_list = [(
Country.get_iso2_from_iso3(code),
Country.get_country_name_from_iso3(code)
) for code in countries_iso3
]
def get_country_data(self, country_code: str) -> dict[str, str] | None:
"""Retrieves annotated country information. Will accept either ISO2 or ISO3 country code.
return countries_list
:param country_code: ISO2 or ISO3 country code.
:return: Dictionary of country information with HXL hashtags as keys.
"""
if country_code is None:
return None

if len(country_code) == 2:
return HDX.get_country_info_from_iso2(country_code)

class Regions:
"""
An object that can query a UN M49 list of geographical regions and subregions and return a list of countries in
that region.
"""
return HDX.get_country_info_from_iso3(country_code)

def country_region(self, country_code: str) -> int | None:
def country_region(self, country) -> int | None:
"""Return a UN M49 region code for a country.
Extends django-countries by adding a .region() method to the Country field.
:param country_code: Two-letter ISO country code.
:return: Integer. UN M49 region code.
"""
country_data = get_country_data(country_code)
country_data = self.get_country_data(country.code)
if country_data:
return int(country_data["#region+code+main"])
else:
return None

def country_region_name(self, country_code: str) -> str | None:
def country_region_name(self, country) -> str | None:
"""Retrieves region name for a country.
Extends django-countries by adding a .region_name() method to the Country field.
:param country_code: Two-letter ISO country code.
:return: String. Region name
"""
country_data = get_country_data(country_code)
country_data = self.get_country_data(country.code)

if country_data:
return country_data["#region+main+name+preferred"]

return None

def country_subregion(self, country_code: str) -> int | None:
def country_subregion(self, country) -> int | None:
"""Return a UN M49 sub-region code for a country.
Extends django-countries by adding a .subregion() method to the Country field.
:param country_code: Two-letter ISO country code.
:return: Integer. UN M49 sub-region code.
"""
country_data = get_country_data(country_code)
country_data = self.get_country_data(country.code)

if country_data:
# Return the intermediate region if populated.
Expand All @@ -172,13 +78,13 @@ def country_subregion(self, country_code: str) -> int | None:

return None

def country_subregion_name(self, country_code: str) -> str | None:
def country_subregion_name(self, country) -> str | None:
"""Return the sub-region name for a country
:param country_code: Two-letter ISO country code.
:return: String
"""
country_data = get_country_data(country_code)
country_data = self.get_country_data(country.code)

if country_data:
# Return the intermediate region if populated.
Expand All @@ -187,44 +93,138 @@ def country_subregion_name(self, country_code: str) -> str | None:

return None

def is_sids(self, country_code: str) -> bool | None:
def is_sids(self, country) -> bool | None:
"""Returns whether a country is classed as a SIDS
:param country_code: Two-letter ISO country code.
:return: Boolean
"""
country_data = get_country_data(country_code)
country_data = self.get_country_data(country.code)

if country_data:
return bool(country_data["#meta+bool+sids"])

return None

def is_ldc(self, country_code: str) -> bool | None:
def is_ldc(self, country) -> bool | None:
"""Returns whether a country is classed as a LDC
:param country_code: Two-letter ISO country code.
:return: Boolean
"""
country_data = get_country_data(country_code)
country_data = self.get_country_data(country.code)

if country_data:
return bool(country_data["#meta+bool+ldc"])

return None

def is_lldc(self, country_code: str) -> bool | None:
def is_lldc(self, country) -> bool | None:
"""Returns whether a country is classed as a LLDC
:param country_code: Two-letter ISO country code.
:return: Boolean
"""
country_data = get_country_data(country_code)
country_data = self.get_country_data(country.code)

if country_data:
return bool(country_data["#meta+bool+lldc"])

return None

def get_region_name(self, region_code: int) -> str | None:
"""Retrieves region or sub-region name for a region code.
:param region_code: UN M49 region code.
:return: String. Region name
"""
if not region_code:
return None

try:
countriesdata = HDX.countriesdata()
return countriesdata["regioncodes2names"].get(region_code) # noqa
except KeyError:
return None

def get_countries_by_region(self) -> dict[int, dict[str, list[tuple[str, str]]]]:
"""Retrieves lists of countries keyed by region, with region name and country tuples.
:param regions: Boolean to determine whether to use regions or subregions.
:return: Dict. Keyed by region code, the value is a dictionary containing the
region name and a list of country_code, country_name tuples.
"""
region_codes = (2, 9, 19, 142, 150,)

return {
region_code: {
"name": self.get_region_name(region_code),
"countries": sorted(
[
(
HDX.get_iso2_from_iso3(code),
HDX.get_country_name_from_iso3(code)
)
for code in HDX.get_countries_in_region(region_code)
],
key=lambda x: x[1] # Sort by country name
)
}
for region_code in sorted(region_codes, key=lambda x: self.get_region_name(x))
}

def get_countries_by_subregion(self) -> dict[int, dict[str, list[tuple[str, str]]]]:
"""Retrieves lists of countries keyed by region, with region name and country tuples.
:param regions: Boolean to determine whether to use regions or subregions.
:return: Dict. Keyed by region code, the value is a dictionary containing the
region name and a list of country_code, country_name tuples.
"""
subregion_codes = (
5, 11, 13, 14, 15, 17, 18, 21, 29, 30, 34, 35, 39, 53, 54, 57, 61, 143, 145, 151, 154, 155,
)

return {
region_code: {
"name": self.get_region_name(region_code),
"countries": sorted(
[
(
HDX.get_iso2_from_iso3(code),
HDX.get_country_name_from_iso3(code)
)
for code in HDX.get_countries_in_region(region_code)
],
key=lambda x: x[1] # Sort by country name
)
}
for region_code in sorted(subregion_codes, key=lambda x: self.get_region_name(x))
}

def countries_by_region(self, region_code: int) -> list[str] | None:
"""Return a list of country codes found within a region.
:param region_code: UN M49 region code.
:return: List of two-letter ISO country codes.
"""
if region_code:
try:
return [c[0] for c in self.get_countries_by_region()[region_code]["countries"]]
except KeyError:
return None
return None

def countries_by_subregion(self, region_code: int) -> list[str] | None:
"""Return a list of country codes found within a sub-region
:param region_code: UN M49 sub-region code.
:return: List of two-letter ISO country codes.
"""
if region_code:
try:
return [c[0] for c in self.get_countries_by_subregion()[region_code]["countries"]]
except KeyError:
return None
return None

regions = Regions()
Loading

0 comments on commit dc9d1f8

Please sign in to comment.