Skip to main content

Find population for ISO 3166 alpha-2/3 country codes

Project description

Lint & Tests

pypopulation

Lightweight population lookup using ISO 3166 alpha-1/2 country codes for Python 3.5 and higher.

>>> import pypopulation
>>> 
>>> pypopulation.get_population("DE")  # Germany
83132799

The aim is to provide a minimalist package with no dependencies that does one thing only, as best as possible. Population figures are read from a JSON file into Python dictionaries on the first lookup, not at import time. The API then only exposes the dictionaries.

The given figures are estimates at best. Read below for more details on the data source.

Interface

The API is formed by 3 functions:

  • get_population_a2: population for a 2-letter country code
  • get_population_a3: population for a 3-letter country code
  • get_population: population for either either a 2-letter or a 3-letter country code

All functions return None if no country is found for the given country code. Lookup is case insensitive, i.e. "DE" and "de" give same results.

Lookups using country names are difficult & not currently supported, but the source JSON file does contain them. This is to make the source file more comprehensible. If all you have to work with is a country name, consider using pycountry to resolve your names to ISO 3166 codes first.

If you would like to build your own wrapper around the source JSON, you can do:

countries: t.List[t.Dict] = pypopulation._load_file()

Note: This function is wrapped in functools.lru_cache(max_size=1).

Installation

With pip from PyPI:

pip install pypopulation

Development

I'm using Pipenv to maintain development dependencies.

  • pipenv sync --dev: create venv with dev deps
  • pipenv run lint: run Flake8 & friends
  • pipenv run test: run unittest suite and produce a .coverage file

Data source

The population figures were sourced from The World Bank (2020-07-01). This dataset provides the country name, alpha-3 code, and population figures found in the resource JSON file. The data was enriched with alpha-2 country codes for each row. Rows not corresponding to political countries were removed, e.g. "Middle East & North Africa (excluding high income)". Some country names were adjusted for readability, e.g. expanded abbreviations. No adjustments were made to the population figures. Please refer to the linked page for a more detailed description of the dataset.

This projects aims to expose the linked data to Python code. It does not guarantee correctness of the provided figures.

Project details


Download files

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

Source Distribution

pypopulation-2020.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

pypopulation-2020.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file pypopulation-2020.1.tar.gz.

File metadata

  • Download URL: pypopulation-2020.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for pypopulation-2020.1.tar.gz
Algorithm Hash digest
SHA256 b8a5f0f5ceea92df1f6ae52b516757182a4feb65a155aa388678a1c132a031ee
MD5 5e265e1933cfc0c3d275c1abeec58866
BLAKE2b-256 eb530f4b2b289ec21c35601292505959a18d8f6552a6e30b1149360493040c62

See more details on using hashes here.

File details

Details for the file pypopulation-2020.1-py3-none-any.whl.

File metadata

  • Download URL: pypopulation-2020.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for pypopulation-2020.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d2c85f153cc0bf13808ee8d132295fcf4bb9c2a3e966e0839217ca50f183bb78
MD5 5a640595a2a381fc61a872bf49120c26
BLAKE2b-256 01b029f9fd9a1834d7860472e8be402ced897bad05303af2f23d9589e470be0f

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