UKPC
ukpc is a Python module that parses United Kingdom postcode strings into constituent parts:
area
district
sector
unit
Install:
pip install ukpc
Currently, only Python 3.x is supported. Let me know if you need 2.7+ support.
Usage:
>>> from ukpc import PostCode
pc = PostCode('NW3 2RR')
>>> str(pc)
'area=NW, district=3, sector=2, unit=RR'
You can get parts with properties as expected. We also provide PostCode.farea, PostCode.fdistrict, PostCode.fsector, PostCode.funit properties. Some people refer to sector as the cumulative postcode up to and including sector rather than just the sector number:
>>> pc.sector '2' >>> pc.fsector 'NW3 2'
farea and area are the same. funit is the input string, the full postcode.
There is not too much validation and no reformatting of postcodes! An exception will be thrown if the parser can’t parse the string. The parser expects well-formed, upper-case strings as input. If you want to validate or format postcodes, there are some other projects that appear to do this well:
https://github.com/andersonbispo/ukpcode
https://github.com/hamstah/ukpostcodeparser/blob/master/ukpostcodeparser/parser.py
We also provide a utility for reading a file of postcodes. The file is expected to be csv and the first column only is read as a well-formed, upper-case postcode.
from ukpc.utils import iterate_file iterate_file(path, callback=store_pc_in_database)
callback is any callable that you must provide. This will cause store_pc_in_database() to be called with a PostCode instance.
There is also a command line util that calls iterate_file():
(.env)➜ ukpc python -m ukpc.utils postcodes.csv --stats time in secs: 27 areas: 123 districts: 3089 sectors: 12302 units: 401
Get a list of all postcodes with geo information here:
https://www.doogal.co.uk/PostcodeDownloads.php
We follow this specification:
https://www.mrs.org.uk/pdf/postcodeformat.pdf
Testing
pip install pytest cd logtrace pytest test.py --verbose
or
python3 logtrace/test.py
Performance
Metadata
Release files for ukpc 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ukpc-0.1.0.tar.gz | 5.0 kB | Without provenance | Details for ukpc-0.1.0.tar.gz |
Release files / ukpc-0.1.0.tar.gz
| Download URL | ukpc-0.1.0.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
72ad8ffb17738d8f66ad045464ddfb6039197cc19aef4e07a886ce6962186377
|
|
BLAKE2b-256 checksum How to use checksums |
e967dc42bedb9a3f8151265408909166b0742b6303051b37a098be0c2c06d07e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for ukpc 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.