Skip to content

Commit

Permalink
Fix set use live default (#63)
Browse files Browse the repository at this point in the history
* Add failing test to demonstrate bug in issue #62

* Fix issue
  • Loading branch information
andy-isoc authored Nov 19, 2024
1 parent fa69943 commit 120dff8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/hdx/location/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def sort_list(colname):
@classmethod
def countriesdata(
cls,
use_live: bool = _use_live,
use_live: bool = None,
country_name_overrides: Dict = None,
country_name_mappings: Dict = None,
) -> List[Dict[str, Dict]]:
Expand All @@ -227,6 +227,8 @@ def countriesdata(
Returns:
List[Dict[str,Dict]]: Countries dictionaries
"""
if use_live is None:
use_live = cls._use_live
if cls._countriesdata is None:
countries = None
if country_name_overrides is not None:
Expand Down Expand Up @@ -326,7 +328,7 @@ def set_country_name_mappings(cls, country_name_mappings: Dict) -> None:
def get_country_info_from_iso3(
cls,
iso3: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[Dict[str, str]]:
"""Get country information from ISO3 code
Expand All @@ -352,7 +354,7 @@ def get_country_info_from_iso3(
def get_country_name_from_iso3(
cls,
iso3: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
formal: bool = False,
) -> Optional[str]:
Expand Down Expand Up @@ -389,7 +391,7 @@ def get_country_name_from_iso3(
def get_currency_from_iso3(
cls,
iso3: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[int]:
"""Get currency code from ISO3 code
Expand All @@ -415,7 +417,7 @@ def get_currency_from_iso3(
def get_iso2_from_iso3(
cls,
iso3: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[str]:
"""Get ISO2 from ISO3 code
Expand All @@ -441,7 +443,7 @@ def get_iso2_from_iso3(
def get_iso3_from_iso2(
cls,
iso2: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[str]:
"""Get ISO3 from ISO2 code
Expand All @@ -467,7 +469,7 @@ def get_iso3_from_iso2(
def get_country_info_from_iso2(
cls,
iso2: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[Dict[str, str]]:
"""Get country name from ISO2 code
Expand All @@ -493,7 +495,7 @@ def get_country_info_from_iso2(
def get_country_name_from_iso2(
cls,
iso2: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
formal: bool = False,
) -> Optional[str]:
Expand Down Expand Up @@ -521,7 +523,7 @@ def get_country_name_from_iso2(
def get_currency_from_iso2(
cls,
iso2: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[str]:
"""Get currency from ISO2 code
Expand All @@ -547,7 +549,7 @@ def get_currency_from_iso2(
def get_m49_from_iso3(
cls,
iso3: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[int]:
"""Get M49 from ISO3 code
Expand All @@ -573,7 +575,7 @@ def get_m49_from_iso3(
def get_iso3_from_m49(
cls,
m49: int,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[str]:
"""Get ISO3 from M49 code
Expand All @@ -599,7 +601,7 @@ def get_iso3_from_m49(
def get_country_info_from_m49(
cls,
m49: int,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[Dict[str, str]]:
"""Get country name from M49 code
Expand All @@ -623,7 +625,7 @@ def get_country_info_from_m49(
def get_country_name_from_m49(
cls,
m49: int,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
formal: bool = False,
) -> Optional[str]:
Expand Down Expand Up @@ -651,7 +653,7 @@ def get_country_name_from_m49(
def get_currency_from_m49(
cls,
m49: int,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[str]:
"""Get currency from M49 code
Expand Down Expand Up @@ -754,7 +756,7 @@ def simplify_countryname(cls, country: str) -> (str, List[str]):
def get_iso3_country_code(
cls,
country: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> Optional[str]:
"""Get ISO3 code for cls. Only exact matches or None are returned.
Expand Down Expand Up @@ -814,7 +816,7 @@ def get_iso3_country_code(
def get_iso3_country_code_fuzzy(
cls,
country: str,
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
min_chars: int = 5,
) -> Tuple[Optional[str], bool]:
Expand Down Expand Up @@ -904,7 +906,7 @@ def remove_matching_from_list(wordlist, word_or_part):
def get_countries_in_region(
cls,
region: Union[int, str],
use_live: bool = _use_live,
use_live: bool = None,
exception: Optional[ExceptionUpperBound] = None,
) -> List[str]:
"""Get countries (ISO3 codes) in region
Expand Down
9 changes: 9 additions & 0 deletions tests/hdx/location/test_country.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,17 @@ def test_use_live_default(self):
assert Country._use_live is True
Country.set_use_live_default(False)
assert Country._use_live is False
# We should now be able to load from local data without setting use_live=False
Country._countriesdata = None
Country.set_ocha_path(
script_dir_plus_file("Countries_UZB_Deleted.csv", TestCountry)
)
assert Country.get_iso3_country_code("UZBEKISTAN") is None
Country.set_use_live_default(None)
assert Country._use_live is True
Country._countriesdata = None
assert Country.get_iso3_country_code("UZBEKISTAN") == "UZB"
Country._countriesdata = None

def test_ocha_feed_file_working(self):
countries = hxl.data(
Expand Down

0 comments on commit 120dff8

Please sign in to comment.