Package: countryguess
countryguesslooks up country information by country codes or name matching.
It tries to be lean (but not mean) and fast: All dependencies are in the Python
Standard Library and country data is loaded lazily on demand.Code:CodebergPackage:PyPIUsageguess_country()uses the default country data that is already packaged.>>>fromcountryguessimportguess_country>>>guess_country("britain"){'name_short':'United Kingdom','name_official':'United Kingdom of Great Britain and Northern Ireland','iso2':'GB','iso3':'GBR',...}>>>guess_country("no such country")None>>>guess_country("no such country",default="Oh, well.")'Oh, well.'>>>guess_country("PoRtUgAl",attribute="iso2")'PT'>>>guess_country("TW",attribute="name_official")# 2-letter code lookup'Republic of China'>>>guess_country("tWn",attribute="name_short")# 3-letter code lookup'Taiwan'Matching by regular expression can be extended by mappingISO2codes tore.Patternobjects.>>>regex_map={..."MN":re.compile(r'^mongol\s+uls$',flags=re.IGNORECASE),..."JP":re.compile(r'^ni(?:pp|h)on',flags=re.IGNORECASE),...}>>>guess_country("Mongol Uls",attribute="name_short",regex_map=regex_map)'Mongolia'>>>guess_country("Nippon",attribute="name_short",regex_map=regex_map)'Japan'You can also create aCountryDatainstance yourself to provide your own
country data in a JSON file.>>>fromcountryguessimportCountryData>>>countries=CountryData("path/to/countries.json")>>>countries["vIeTnAm"]{'name_short':'Vietnam',...}>>>countries["vn"]{'name_short':'Vietnam',...}>>>countries["asdf"]KeyError:'asdf'>>>countries.get("asdf")None>>>countries.get("kuwait"){'name_short':'Kuwait',...}OnCountryDatainstances, every key in the JSON data is accessible as a
method.>>>countries.name_official("portugal")'Portuguese Republic'>>>countries.continent("vanuatu")'Oceania'Country LookupCountries are identified by name, 2-letter code
(ISO 3166-1 alpha-2) or
3-letter code
(ISO 3166-1 alpha-3). All
identifiers are matched case-insensitively.Names are matched with regular expressions that are stored in the JSON data. If
that fails, fuzzy matching againstname_shortandname_officialis done
withdifflib.Country DataCountry information is read from a JSON file. One is shipped with the package,
but you can also provide your own to theCountryDataclass as described
above. The information in the default file was gratefully extracted fromcountry-converter. (Many thanks!)The country data file must contain a list of JSON objects. Each object
represents a country that must contain at least the following keys:name_shortname_officialiso2iso3regexPackaged Classification SchemesThe following classification schemes are available in the included country data.ISO2 (ISO 3166-1 alpha-2) - including UK/EL for Britain/Greece (but always convert to GB/GR)ISO3 (ISO 3166-1 alpha-3)ISO - numeric (ISO 3166-1 numeric)UN numeric code (M.49 - follows to a large extend ISO-numeric)A standard or short nameThe "official" nameContinent: 6 continent classification with Africa, Antarctica, Asia, Europe, America, OceaniaContinent_7 classification- 7 continent classification spliting North/South AmericaUN regionEXIOBASE 1classification (2 and 3 letters)EXIOBASE 2classification (2 and 3 letters)EXIOBASE 3classification (2 and 3 letters)WIODclassificationEoraOECDmembership (per year)MESSAGE11-region classificationIMAGEREMINDUNmembership (per year)EUmembership (including EU12, EU15, EU25, EU27, EU27_2007, EU28)EEAmembershipSchengenregionCecilia2050 classificationAPECBRICBASICCIS(as by 2019, excl. Turkmenistan)G7G20(listing all EU member
states as individual members)FAOcode(numeric)GBDcode(numeric - Global Burden of
Disease country codes)IEA(World Energy Balances 2021)DACcode(numeric - OECD Development Assistance Committee)ccTLD- country code top-level domainsGWcode- Gledisch & Ward numerical codes as published inhttps://www.andybeger.com/states/articles/statelists.htmlCC41 - common classification for MRIOs (list of countries found in all public MRIOs)IOC- International Olympic Committee (IOC) country codesCommand Line Interfacecountryguesscomes with a simple CLI with the same name. It takes one or two
arguments:$countryguessoman{"name_short":"Oman","name_official":"Sultanate of Oman",...}$countryguess'puerto ricco'name_official
PuertoRicoContributingAll kinds of bug reports, feature requests and suggestions are welcome!