Giter Club home page Giter Club logo

zipcode's Introduction

Zipcode

A simple python package for dealing with zip codes

IMPORTANT. This package relies on up-to-date data from unitedstateszipcodes.org. To ensure that this package works, please follow the installation instructions closely, making sure that you also follow any rules governing zipcode data distributed by unitedstateszipcodes.org as per their site. The data is free for non-commercial use, and affordable for commercial use, you must download it from them though as I am not allowed to distribute it.

Installation

  1. go to https://www.unitedstateszipcodes.org/zip-code-database/
  2. download the CSV file, pick free or a commercial version if you need to use it commercially. If you need to buy the commercial one, do so, but download the free one as that is the one supported by this package.
  3. move the downloaded file to a good location and set appropriate environment variables.
mkdir -p /var/lib/zipcode
mv ~/Downloads/zip_code_database.csv /var/lib/zipcode/zip_code_database.csv
echo 'ZIPS_CSV=/var/lib/zipcode/zip_code_database.csv' >> ~/.bash_profile
source ~/.bash_profile
  1. set up the database.
  • for production applications a relational database like postgresql is recommended! sqlite is acceptable for lower use applications.
  • after you decide which database to use, find your connection string here.
# set connection string, we use sqlite as an example (but use postgres in production!)
echo 'ZIPCODE_CONNECTION_STRING=sqlite:///zipcode.db' >> ~/.bash_profile
source ~/.bash_profile
  1. install zipcode
pip install zipcode
  1. populate the database
  • this might take a while ~10min for postgres. be patient, it'll be fast once loaded
build_zipcode_database

Good to go. The next section shows you how to use the package.

Getting started

import zipcode

myzip = zipcode.isequal('44102')
myzip.state     #=> 'OH'
myzip.city      #=> 'Cleveland'
myzip.location #=> 'Cleveland, OH'

# all keys in the dictionary can also be fetched with dot notation.
dict(myzip) #=> {'zipcode': '44102', 'zipcode_type': 'STANDARD', 'city': 'Cleveland', 'state': 'OH', 'timezone': 'America/New_York', 'lat': 41.48, 'lng': -81.74, 'county': 'Cuyahoga County', 'location': 'Cleveland, OH', 'decommissioned': True, 'population': 31930, 'area_codes': ['216'], 'secondary_cities': []} 


zipcode.islike('00') #=> list of Zip objects that begin with given prefix.

cbus = (39.98, -82.98)
zipcode.isinradius(cbus, 20) #=> list of all zip code objects within 20 miles of 'cbus'

zipcode.hascity('Cleveland', 'OH') #=> list of zip codes in Cleveland, OH
zipcode.hascity('', 'OH') #=> list of zip codes in state of OH
zipcode.hascity('Flushing', 'NY', include_secondary=False) #=> don't include zips where flushing is a secondary city

zipcode.hasareacode(216) #=> list of zip codes associated with 216 

Keeping the database up-to-date

Zip codes don't change very often, but the borders do change, and new zip codes are added, and others are removed. To keep your zipcode package ever up-to-date we suggest that you set up a job to keep the database current, or simply drop the zipcodes table in your database and re-rerun steps 1-3 and 6 once every 3 months or so.

You're all set. Get to it!

zipcode's People

Contributors

buckmaxwell avatar chrismuir avatar hendler avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.