Simple, strongly typed access to ISO 3166-1 and 3116-2
Project description
simpleiso3166
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 & statically defined in Python
- Includes defined types to allow integration with libraries like Pydantic for validation of code validity
from simpleiso3166 import CountryCodeAlpha2Typefor example, provides a Literal of all alpha-2 codes
- 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
slotsto 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.official_name == "United States of America"
assert country.name == "United States"
# ISO-3166 includes things like districts and territories
assert len(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:
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 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simpleiso3166-0.3.0.tar.gz.
File metadata
- Download URL: simpleiso3166-0.3.0.tar.gz
- Upload date:
- Size: 152.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e090bd1bcd5c3c75cc47c5dc1713bc7acf51563e1545d18f048d95caf0534e0
|
|
| MD5 |
4ac7525a543d019411fe2da02d0e97d4
|
|
| BLAKE2b-256 |
dee5069d4e22c35a7e812501d746635936587d3c1202060e20febf19dd4ed0b4
|
Provenance
The following attestation bundles were made for simpleiso3166-0.3.0.tar.gz:
Publisher:
ci.yml on stumpylog/simpleiso3166
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simpleiso3166-0.3.0.tar.gz -
Subject digest:
5e090bd1bcd5c3c75cc47c5dc1713bc7acf51563e1545d18f048d95caf0534e0 - Sigstore transparency entry: 590171557
- Sigstore integration time:
-
Permalink:
stumpylog/simpleiso3166@152a0a7959f6855dbe4f00d0dcff82dab0e1eeea -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/stumpylog
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@152a0a7959f6855dbe4f00d0dcff82dab0e1eeea -
Trigger Event:
push
-
Statement type:
File details
Details for the file simpleiso3166-0.3.0-py3-none-any.whl.
File metadata
- Download URL: simpleiso3166-0.3.0-py3-none-any.whl
- Upload date:
- Size: 276.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d829d87553e0a7b82f10999a63564d8cb94e772fc207830c5e22c45f728ffd19
|
|
| MD5 |
155039df81968ba543c3650ffb7cd204
|
|
| BLAKE2b-256 |
9a7abf0ccade701f1c785a7fb64e061551379512b646792b284400978a7ffe2e
|
Provenance
The following attestation bundles were made for simpleiso3166-0.3.0-py3-none-any.whl:
Publisher:
ci.yml on stumpylog/simpleiso3166
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simpleiso3166-0.3.0-py3-none-any.whl -
Subject digest:
d829d87553e0a7b82f10999a63564d8cb94e772fc207830c5e22c45f728ffd19 - Sigstore transparency entry: 590171750
- Sigstore integration time:
-
Permalink:
stumpylog/simpleiso3166@152a0a7959f6855dbe4f00d0dcff82dab0e1eeea -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/stumpylog
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@152a0a7959f6855dbe4f00d0dcff82dab0e1eeea -
Trigger Event:
push
-
Statement type: