Skip to main content

No-SQLite U.S. zipcode validation Python package, ready for use in AWS Lambda

Project description

Zipcodes

A lightweight U.S. zip-code validation package for Python (2 and 3). This package was built as zipcode provided too much functionality and relied on sqlite3, which is not available on platforms such as AWS Lambda. While more difficult to add additional zip-codes, this package provides the essential functionality of zip-code validation with fewer dependencies and minimal working logic.

https://pepy.tech/badge/zipcodes

Contributions are welcome!

Installation

$ pip install zipcodes

Synopsis

Below is the expected usage of this package and a demonstration of supported functionality.

>>> from pprint import pprint
>>> import zipcodes

>>> # Simple zip-code matching.
>>> pprint(zipcodes.matching('77429'))
[{'zip_code': '77429',
  'zip_code_type': 'STANDARD',
  'city': 'CYPRESS',
  'state': 'TX',
  'lat': 29.96,
  'long': -95.69,
  'world_region': 'NA',
  'country': 'US',
  'active': True}]

>>> # Handles of Zip+4 zip-codes nicely. :)
>>> pprint(zipcodes.matching('77429-1145'))
[{'zip_code': '77429',
  'zip_code_type': 'STANDARD',
  'city': 'CYPRESS',
  'state': 'TX',
  'lat': 29.96,
  'long': -95.69,
  'world_region': 'NA',
  'country': 'US',
  'active': True}]

>>> # Will try to handle invalid zip-codes gracefully...
>>> print(zipcodes.matching('06463'))
[]

>>> # Until it cannot.
>>> zipcodes.matching('0646a')
Traceback (most recent call last):
  ...
TypeError: Invalid characters, zipcode may only contain digits and "-".

>>> zipcodes.matching('064690')
Traceback (most recent call last):
  ...
TypeError: Invalid format, zipcode must be of the format: "#####" or "#####-####"

>>> zipcodes.matching(None)
Traceback (most recent call last):
  ...
TypeError: Invalid type, zipcode must be a string.

>>> # Whether the zip-code exists within the database.
>>> print(zipcodes.is_real('06463'))
False

>>> # How handy!
>>> print(zipcodes.is_real('06469'))
True

>>> # Search for zipcodes that begin with a pattern.
>>> pprint(zipcodes.similar_to('0643'))
[{'active': True,
  'city': 'GUILFORD',
  'country': 'US',
  'lat': 41.28,
  'long': -72.67,
  'state': 'CT',
  'world_region': 'NA',
  'zip_code': '06437',
  'zip_code_type': 'STANDARD'},

 {'active': True,
  'city': 'HADDAM',
  'country': 'US',
  'lat': 41.45,
  'long': -72.5,
  'state': 'CT',
  'world_region': 'NA',
  'zip_code': '06438',
  'zip_code_type': 'STANDARD'},
... # remaining results truncated for readability...
]

>>> # Use filter_by to filter a list of zip-codes by specific attribute->value pairs.
>>> pprint(zipcodes.filter_by(city="WINDSOR", state="CT"))
[{"zip_code": "06006",
  "zip_code_type": "UNIQUE",
  "city": "WINDSOR",
  "state": "CT",
  "lat": 41.85,
  "long": -72.65,
  "world_region": "NA",
  "country": "US",
  "active": True },

 {"zip_code": "06095",
  "zip_code_type": "STANDARD",
  "city": "WINDSOR",
  "state": "CT",
  "lat": 41.85,
  "long": -72.65,
  "world_region": "NA",
  "country": "US",
  "active": True},
],

>>> # Arbitrary nesting of similar_to and filter_by calls, allowing for great precision while filtering.
>>> pprint(zipcodes.similar_to('2', zips=zipcodes.filter_by(active=True, city='WINDSOR')))
[{'active': True,
  'city': 'WINDSOR',
  'country': 'US',
  'lat': 33.48,
  'long': -81.51,
  'state': 'SC',
  'world_region': 'NA',
  'zip_code': '29856',
  'zip_code_type': 'STANDARD'},
 {'active': True,
  'city': 'WINDSOR',
  'country': 'US',
  'lat': 36.8,
  'long': -76.73,
  'state': 'VA',
  'world_region': 'NA',
  'zip_code': '23487',
  'zip_code_type': 'STANDARD'},
 {'active': True,
  'city': 'WINDSOR',
  'country': 'US',
  'lat': 36.0,
  'long': -76.94,
  'state': 'NC',
  'world_region': 'NA',
  'zip_code': '27983',
  'zip_code_type': 'STANDARD'}]

>>> # Have any other ideas? Make a pull request and start contributing today!
>>> # Made with love by Sean Pianka

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

zipcodes-1.0.5.tar.gz (574.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zipcodes-1.0.5-py2.py3-none-any.whl (571.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file zipcodes-1.0.5.tar.gz.

File metadata

  • Download URL: zipcodes-1.0.5.tar.gz
  • Upload date:
  • Size: 574.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for zipcodes-1.0.5.tar.gz
Algorithm Hash digest
SHA256 54de2736e41484e2751a36a219979960b02edccc271712424d54098a1c3e253a
MD5 de0bed7f78df7b0711a08c579096d012
BLAKE2b-256 944eec0a47bcd38313694a94fc2154f8e1c7dc12c8f68c2702a79b8855da17d5

See more details on using hashes here.

File details

Details for the file zipcodes-1.0.5-py2.py3-none-any.whl.

File metadata

  • Download URL: zipcodes-1.0.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 571.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for zipcodes-1.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9f5694b50e9484d4f5057bd295715aa23108fc7471b8e330b37d79d8cb2306cf
MD5 17954374099e140bdda56b3228969d4e
BLAKE2b-256 81d1b52c2d5bd93c8532f78cb2df688baa16cc121351ebd947274eabd944531d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page