Skip to content

Commit

Permalink
split iso2 output for regex - fix 113
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinstadler committed Aug 5, 2022
1 parent 03cc3a0 commit 213fd8a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log


## 0.7.7 - 20220805


### Bug fixes

- ISO2 did return the regex for GB|UK - fix #113


## 0.7.6 - 20220802

### Breaking
Expand Down
15 changes: 14 additions & 1 deletion country_converter/country_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,14 @@ def data_loader(data):
# class.
def fun_provider(df, datacol):
def fun_provided(to):
return df.loc[:, [to, datacol]].dropna()
ret = df.loc[:, [to, datacol]].dropna()
if to in ["ISO2", "ISO3"]:
ret.loc[:, to] = (
ret.loc[:, to]
.str.split("|")
.apply(lambda x: "".join((c for c in x[0] if c.isalnum())))
)
return ret

return fun_provided

Expand Down Expand Up @@ -606,6 +613,12 @@ def convert(
else:
outlist[ind_names] = []
for etr in result_list:
if to[0].lower() in ["iso2", "iso3"]:
# remove regex characters from output
etr = "".join(
c for c in etr.split("|")[0] if c.isalnum()
).upper()

try:
conv_etr = int(etr)
except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion country_converter/country_data.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Tuvalu Tuvalu tuvalu TV TUV 798 798 227 416 Oceania Polynesia WW WA WA WWW WWA W
Uganda Republic of Uganda uganda UG UGA 800 800 226 190 Africa Eastern Africa WW WF WF WWW WWF WWF RoW UGA AFR Eastern Africa SSA 1962 1962 RoW Other Africa 285
Ukraine Ukraine ukrain UA UKR 804 804 230 63 Europe Eastern Europe WW WE WE WWW WWE WWE RoW UKR FSU Ukraine region REF 1945 1945 RoW Ukraine 85
United Arab Emirates United Arab Emirates emirates|^u\.?a\.?e\.?$|united.?arab.?em AE ARE 784 784 225 156 Asia Western Asia WW WM WM WWW WWM WWM RoW ARE MEA Middle East MEA 1971 1971 RoW United Arab Emirates 576
United Kingdom United Kingdom of Great Britain and Northern Ireland .*(united.?kingdom|britain|^u\.?k\.?$|gb) ^uk$|^gb$ GBR 826 826 229 95 Europe Northern Europe GB GB GB GBR GBR GBR GBR GBR WEU Western Europe EUR 1961 EU28 EU27_2007 EU25 EU15 EU12 EEA 1945 1945 EU G7 G20 United Kingdom 12
United Kingdom United Kingdom of Great Britain and Northern Ireland .*(united.?kingdom|britain|^u\.?k\.?$|gb) ^GB$|^UK$ GBR 826 826 229 95 Europe Northern Europe GB GB GB GBR GBR GBR GBR GBR WEU Western Europe EUR 1961 EU28 EU27_2007 EU25 EU15 EU12 EEA 1945 1945 EU G7 G20 United Kingdom 12
United States United States of America ^(?!.*islands).*united.?states|^u\.?s\.?a\.?$|^u\.?s\.?$ US USA 840 840 231 102 America Northern America US US US USA USA USA USA USA NAM USA USA 1961 1945 1945 HI APEC G7 G20 United States 302
United States Minor Outlying Islands United States Minor Outlying Islands minor.?outlying.?is UM UMI 581 581 232 Oceania Micronesia WW WA WA WWW WWA WWA RoW OAS RoW Other non-OECD Asia
United States Virgin Islands Virgin Islands of the United States ^(?=.*\bu\.?\s?s).*virgin|^(?=.*states).*virgin VI VIR 850 850 240 422 America Caribbean WW WL WL WWW WWL WWL RoW VIR NAM Central America LAM RoW Other non-OECD Americas
Expand Down
2 changes: 1 addition & 1 deletion country_converter/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.7.6"
__version__ = "0.7.7"
14 changes: 13 additions & 1 deletion tests/test_functionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ def test_alternative_names(get_regex_test_data):
)


def test_toISO2_conversion():
converter = coco.CountryConverter()
assert "DE" == converter.convert("DEU", src="ISO3", to="ISO2")
assert "GB" == converter.convert("GBR", src="ISO3", to="ISO2")
assert "GB" == converter.convert("UK", src="ISO2", to="ISO2")
assert "GB" == converter.convert("UK", to="ISO2")
assert "GB" == converter.convert("GB", to="ISO2")
assert "GB" == converter.convert("GBR", to="ISO2")
assert "TR" == converter.convert("TR", src="ISO2", to="ISO2")
assert "TR" == converter.convert("TUR", src="ISO3", to="ISO2")


def test_additional_country_file():
converter_basic = coco.CountryConverter()
converter_extended = coco.CountryConverter(additional_data=custom_data)
Expand Down Expand Up @@ -496,7 +508,7 @@ def test_EU_output():
EU27_2007 = cc.EU27_2007as("ISO2")
assert len(EU27_2007 == 27)
assert cc.convert("Croatia", to="ISO2") not in EU27_2007.ISO2.tolist()
assert cc.convert("UK", src="regex", to="ISO2") in EU27_2007.ISO2.tolist()
assert cc.convert("GB", src="regex", to="ISO2") in EU27_2007.ISO2.tolist()


def test_EXIO_output():
Expand Down

0 comments on commit 213fd8a

Please sign in to comment.