Skip to main content

Postal Codes Tools

https://img.shields.io/badge/code%20style-black-000000.svg

Library that helps working with different postal codes. Purpose of this library is to validate string if it matches the postal code format.

Postal code format checking is based on European Central Bank - List of postal code formatting rules and regular expressions per country spreadsheet version 1.1.

Intention of the library is to keep version number same as the ECB spreadsheet, eg. v1.1.0, so it is easy the track regex and ECB changes in the spreadsheet change log. Just the last number, eg. v1.1.3 can change with bug fixes and updates.

Installation

Install the released version:

pip install postal-codes-tools

Usage

Basic usage is to verify if the postal code has correct format:

>>> from postal_codes_tools.postal_codes import verify_postal_code_format
>>> verify_postal_code_format(country_iso_code="US", postal_code="00716-9999")
True

Certain countries does not heave posta code, library also provides a check if a country does have postal code. You can also get the whole list of countries does not heave posta code:

>>> from postal_codes_tools.postal_codes import country_without_postal_code, has_postal_code
>>> country_without_postal_code("DE")
False
>>> country_without_postal_code("AE")
True
>>> has_postal_code("DE")
True
>>> has_postal_code("AE")
False
>>>
>>> from postal_codes_tools.postal_codes import get_countries_without_postal_code
>>> get_countries_without_postal_code()
('AE', 'SB', 'GY', 'UG', 'SL', 'KM', 'CK', 'UM', 'KI', 'TK', 'BO', 'DJ', 'GD', 'BQ', 'GQ', 'CF', 'MR', 'SY', 'SX', 'GH', 'ST', 'ZW', 'BZ', 'BJ', 'RW', 'BI', 'AO', 'TL', 'CD', 'CM', 'QA', 'SC', 'TV', 'KP', 'AW', 'CG', 'TD', 'XX', 'BV', 'ML', 'TG', 'ER', 'FJ', 'TO', 'BS', 'GA', 'CI', 'YE', 'CW', 'DM', 'SR', 'VU', 'GM', 'BW')

Since version 1.1 of the ECB spreadsheet, ECB does not define countries without postal code, they just gave them default regex string ^.{1,255}$, how ever some of those has an example of posta codes. List of countires without postal code matches the list of default regex without an example of postal code.

Library provide a map of territories, so you can easily find out to which country they belong. If the country code is not in the map, original country code is returned. For example Martinique (ISO2 country code: MQ) is overseas department of France in the Caribbean:

>>> from postal_codes_tools.mappers import territory_to_parent_mapper
>>> territory_to_parent_mapper('MQ')
'FR'
>>> territory_to_parent_mapper('FR')
'FR'

Library provides a function to get the regex used for postal code verification and also supports strict postal code checks for territories:

>>> from postal_codes_tools.postal_codes import get_postal_code_regex
>>> get_postal_code_regex('US')
'^\\d{5}(-\\d{4})?$'
>>> get_postal_code_regex('MQ')
'^\\d{5}$'
>>> get_postal_code_regex('MQ', strict=True)
'^972\\d{2}$'

Development

We look forward to any kind of improvements and support for new postal codes.

Clone a repository locally and make sure you work in your own branch and once you are happy with the functionality create pull request. All new code should be covered with tests. We try to use test driven development for the project.

If you find a bug feel free to create an issue with description, how ever we appreciate even more if you create failing test.

Release

Release is done via twine. The whole package is uploaded in the form of .dist file.

To create .dist file use command:

python setup.py sdist bdist_wheel

Now our binary .dist file is created, now we need to upload it using the below command:

python -m twine dist/*

Alternatively if you want to selfhost in a custom PyPI repo you can also upload there as well:

python -m twine upload--repository-url https://gitlab.com/custom/repo/path dist/* --cert /custom/cert

Testing

Tests are written in pytest and stored in tests directory. Library has 100% test coverage. If you found an error, write a failing test first.

Run the tests by following command:

pytest postal_codes_tools

Metadata

Release files for postal-codes-tools 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for postal-codes-tools 1.1.3
File Size Uploaded
postal-codes-tools-1.1.3.tar.gz 11.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for postal-codes-tools 1.1.3
File Interpreter ABI Platform
postal_codes_tools-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 22.1 kB

Release files / postal-codes-tools-1.1.3.tar.gz

Download URL postal-codes-tools-1.1.3.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5ff78c8a6ececeb4a843ba4f3e3d2b6f29b95f021ffe83f255fccd1cc61768ec
BLAKE2b-256 checksum
How to use checksums
7eb07bc4828f06120a7373628ac030c08940eae4d50f9c74eee1afc052e8aa1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release files / postal_codes_tools-1.1.3-py3-none-any.whl

Download URL postal_codes_tools-1.1.3-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6fe2b90507e95580311b26034f970bbf362b7e319a15fc18d0ddcf49db490c63
BLAKE2b-256 checksum
How to use checksums
a88cb7f899061d92f4bc6f7b763cc39c7a8a4799489d91e9b4616897fbabf5ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release history Release notifications | RSS feed

This release

1.1.3 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page