Skip to main content

A convenience API for transformations between different ISO 3166 country codes.

Project description

incf.countryutils2 ===

Disclaimer from friendly forkers —

We decided to download this package from PyPI in 2015 because we needed to make some adjustments. We tried contacting the OP several times without success. We will accept Pull Requests. Cheers, Wyldebeast & Wunderliebe.

Disclaimer —

This code is completely copied from the source egg that is found on https://pypi-hypernode.com/pypi/incf.countryutils/1.0 The code seemed no longer maintained and the original author doesn’t respond to email, so after looking at the license I decided to copy the code into this github repo and update the country data

Description —

incf.countryutils provides a convenience API on top of a list of countries by continent (data_file) as found on Wikipedia (a copy is included in the distribution).

It supports transformations between the different types of country codes and names like in:

>>> from incf.countryutils import transformations
>>> transformations.cca2_to_ccn('de')
'276'
>>> transformations.ccn_to_cn(276)
'Germany'

Country codes supported are numeric (ccn; internal reference format), two letter country codes (cca2), and three letter country codes (cca3). In addition, incf.countryutils knows the simple English name of each country (cn) as well as the official English name.

Transformation names follow the pattern <f1>_to_<f2> where f1 and f2 refer to country codes or names. All transformations to and from the reference format (ccn) are provided.

When providing a numeric country code as an argument integer as well as string representations are accepted.

Moreover, there are some combined transformations available for convenience. Most notably this is <any country code>_to_<simple or official name>:

>>> transformations.cc_to_cn('FR')
'France'
>>> transformations.cc_to_cn('FRa')
'France'
>>> transformations.cc_to_cn('250')
'France'
>>> transformations.cc_to_cn(250)
'France'
>>> transformations.cc_to_con(250)
'French Republic France'

Lookup by name is only supported for the simple English name:

>>> transformations.cn_to_ccn('Italy')
'380'
>>> transformations.ccn_to_con('380')
'Italian Republic Italy'

Continents —

Based on the data from Wikipedia incf.countryutils allows looking up the continent a country belongs to (ctn: continent name; ctca2: two letter continent code):

>>> transformations.cn_to_ctn('Italy')
'Europe'
>>> transformations.cca_to_ctn('us')
'North America'

Continents have two letter codes as well like in:

>>> transformations.cca_to_ctca2('usa')
'NA'

Given a continent, one can obtain its constituent countries:

>>> transformations.ctca2_to_ccn('AN')
['010', '074', '239', '334']

Alternative API (OO) —

There is an alternative, more object-oriented API based on the notion of Country and Continent types. On creation, a country or continent needs to be passed a name or code:

>>> from incf.countryutils.datatypes import Country
>>> china = Country('China')
>>> china
<incf.countryutils.datatypes.Country object at 0x...>

The country’s names and codes are available as attributes:

>>> china.name
'China'
>>> china.official_name
"People's Republic of China"
>>> china.numeric
'156'
>>> china.alpha2
'CN'
>>> china.alpha3
'CHN'

and the continent property refers to a corresponding Continent instance:

>>> china.continent
<incf.countryutils.datatypes.Continent object at 0x...>

which in turn has the following attributes:

>>> china.continent.name
'Asia'
>>> china.continent.alpha2
'AS'

Asking a continent for its constituent countries returns a generator object returning country instances in turn:

>>> type(china.continent.countries)
<class 'generator'>
>>> next(china.continent.countries)
<incf.countryutils.datatypes.Country object at 0x...>
>>> [c.name for c in china.continent.countries]
['Afghanistan', 'Azerbaijan', 'Bahrain', 'Bangladesh', 'Armenia', 'Bhutan', 'Brunei', 'Myanmar', 'Cambodia', 'Sri Lanka', 'China', 'Taiwan', 'Christmas Island', 'Cocos (Keeling) Islands', 'Cyprus', 'Georgia', 'Hong Kong', 'India', 'Indonesia', 'Iran', 'Iraq', 'Israel', 'Japan', 'Kazakhstan', 'Jordan', 'Korea', 'Korea', 'Kuwait', 'Kyrgyz Republic', "Lao People's Democratic Republic", 'Lebanon', 'Macao', 'Malaysia', 'Maldives', 'Mongolia', 'Oman', 'Nepal', 'Pakistan', 'Palestine', 'Philippines', 'Timor-Leste', 'Qatar', 'Russian Federation', 'Saudi Arabia', 'Singapore', 'Vietnam', 'Syrian Arab Republic', 'Tajikistan', 'Thailand', 'United Arab Emirates', 'Turkey', 'Turkmenistan', 'Egypt', 'Uzbekistan', 'Yemen', 'Disputed Territory', 'Disputed Territory']

Related packages —

The use case driving the development of this package has been the wish to be able to get at a continent given a country where the country can be specified in any ISO 3166 compliant way.

There is also pycountry which handles also regional subdivision, currency, and language. It may also gain the capabilities provided here but note that pycountry requires lxml which may not always be readily available on some platforms.

Finally, for those interested in looking up countries by IP address there is ip2cc.

Changelog

1.2 - (2021-06-23)

  • some extra countries added

  • fix tests

  • fix small bug

  • release on pypi as incf.countryutils2

1.1 - (2016-01-07)

  • Updated data from wikipedia (huubbouma)

1.0 - (2009-04-20)

  • Initial release

Project details


Release history Release notifications | RSS feed

This version

1.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

incf.countryutils2-1.2.tar.gz (22.1 kB view details)

Uploaded Source

File details

Details for the file incf.countryutils2-1.2.tar.gz.

File metadata

  • Download URL: incf.countryutils2-1.2.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.6

File hashes

Hashes for incf.countryutils2-1.2.tar.gz
Algorithm Hash digest
SHA256 6d36d4559d7416864d0146cdc434c3feec3a15215c68f795f41a5403b145a5fd
MD5 d12f71a5d189797248f716b42dcce951
BLAKE2b-256 61da64d4c252893201192efa1a96723ddcb82036d5c5ce76be3d8324d16398eb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page