Class `CountryCodes()` to search and convert between `iso` and `fips` country codes.
from fastcore.test import test_eq
from nbdev.showdoc import show_doc
Solution to a problem¶
Joshua Project uses FIPS codes (US Federal Information Processing Standard) for countries.
FIPS 10-4 (April 1995) -- Countries, Dependencies, Areas of Special Sovereignty, and TheirPrincipal Administrative Divisions.
However, FIPS 10-4 was withdrawn by NIST on September 2, 2008 in favor of the international ISO 3166 standard.
In the meantime, as of Jan 2020 Joshua Project has no plans to switch onto ISO codes, I was notified. sigh
Sadly, FIPS in 60% cases differs from the ISO.
This library provides a CountryCodes()
class to workaround this discrepancy.
Wherever the library uses the term iso code
, ISO 3166 alpha 2 country code is implied as defined here: https://www.iso.org/obp/ui/
from joshuaproject.countrycodes import CountryCodes
cc = CountryCodes()
test_eq(len(cc),len(cc.data))
len(cc)
cc.head(5)
cc.data[cc.data.FIPS.isna()]
cc.tail(5)
cc[101]
test_eq(cc[101].cname, 'Iceland')
test_eq(cc[101].FIPS,'IC')
test_eq(cc[101].ISO, 'IS')
Also supports slicing.
cc[10:13]
cc.like('United')
test_eq(cc.like('Russia'),[{'cname': 'Russian Federation', 'FIPS': 'RS', 'ISO': 'RU'}])
test_eq(cc.like('United',2),
[{'cname': 'Tanzania, United Republic of', 'FIPS': 'TZ', 'ISO': 'TZ'},
{'cname': 'United Arab Emirates', 'FIPS': 'AE', 'ISO': 'AE'}])
test_eq(cc.like('Non-existing'),[])
test_eq(cc.iso('Russia'), 'RU')
Note that two forms of calls are possible:
test_eq(cc.fips('Russia'), 'RS')
test_eq(cc('Russia'), 'RS')