Skip to main content

Simple, strongly typed access to ISO 3166-1 and 3116-2

Project description

simpleiso3166

PyPI - Version PyPI - Python Version codecov


Table of Contents

Installation

pip install simpleiso3166

For the searching by partial name, include the extra:

pip install simpleiso3166[search]

License

simpleiso3166 is distributed under the terms of the MPL-2.0 license.

Features

  • Strongly typed interface to ISO 3166-1 countries and ISO 3166-2 subdivisions (e.g. states, provinces)
  • No JSON parsing at runtime, fully defined in Python
  • Includes defined types to allow integration with libraries like Pydantic for validation of code validity
  • Lazy loading of subdivision data on demand for reduced memory usage
  • Searching for countries by exact or partial name
  • Searching for subdivisions by exact or partial name
  • Supports Python 3.9+
  • On Python 3.10 and newer, uses slots to efficiently store country and subdivision data

Why?

This library was created as a way to provide strongly typed access to the ISO 3166 standard, which is used to define country codes and subdivision codes. It's designed to be lightweight and easy to use in Python projects.

Other existing libraries include pycountry, but they don't provide the same strongly typed interface as this library does. They also load JSON files at runtime, which can be a performance issue and takes memory.

Usage

Country Based Interface

If you know the country's code:

country = Country.from_alpha2("US")

assert country.name == "United States of America"
assert country.common_name == "United States"

# Subdivision data isn't loaded until the first ask for it
assert len(list(country.subdivisions)) == 57

If you need to search for the country:

results = list(Country.from_partial_name("Kingdom"))

assert len(results) == 17
assert results[0].alpha2 == "BE"
assert results[0].name == "Kingdom of Belgium"
assert results[0].common_name == "Belgium"

assert results[10].alpha2 == "NL"
assert results[10].name == "Kingdom of the Netherlands"
assert results[10].common_name == "Netherlands"

assert results[16].alpha2 == "TO"
assert results[16].name == "Kingdom of Tonga"
assert results[16].common_name == "Tonga"

Access a particular subdivision:

country = Country.from_alpha2("DE")

# You can check this subdivision code is in this country
assert country.contains_subdivision("DE-BW")

# This will return None if the subdivision isn't valid
subdivision = country.get_subdivision("DE-BW")
assert subdivision.name == "Baden-Württemberg"

Subdivision Based Interface

Search for a subdivision by name. This will have to load ALL subdivision data though:

results = list(Subdivision.search_by_name("Connecticut"))

assert len(results) == 1
assert results[0] == "US-CT"

Roadmap

Improved Searching

This is partly implemented, but more aliases would be helpful

  • Search using common aliases for countries (e.g. "USA" for "United States of America")
  • Extended searching for subdivisions by common aliases (eg "DC" for "Washington DC")
  • Search using re-ordered names (e.g. "The Democratic Republic of the Congo" for instead of "Congo, The Democratic Republic of the")

ISO 3166-3

  • Access to deleted country data

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

simpleiso3166-0.1.0.tar.gz (131.1 kB view details)

Uploaded Source

Built Distribution

simpleiso3166-0.1.0-py3-none-any.whl (238.2 kB view details)

Uploaded Python 3

File details

Details for the file simpleiso3166-0.1.0.tar.gz.

File metadata

  • Download URL: simpleiso3166-0.1.0.tar.gz
  • Upload date:
  • Size: 131.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for simpleiso3166-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eed88e04d215864a8a48719dc290d595269f470a3cc676d9c17ffa01f71fcc65
MD5 9342177d8049b88d561976c2bd375aa3
BLAKE2b-256 8d87f3b30e9abf5481831af56063083b7eab990339a135247692381de8eccd4b

See more details on using hashes here.

File details

Details for the file simpleiso3166-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simpleiso3166-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c72ce88896c72b08717129b6aee5cfa979263b79f94bf7ecfd0a68028dc1481
MD5 2d445ef551051f701a4268e7a3f8ada0
BLAKE2b-256 8af0b3ef3ec95b86da85a863642065c228e42f884caa0c47202be90c208c33f0

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