|
1 | 1 | # Zipcode |
2 | 2 | ### A simple python package for dealing with zip codes |
3 | 3 |
|
| 4 | +**IMPORTANT.** This package relies on up-to-date data from |
| 5 | +unitedstateszipcodes.org. To ensure that this package works, please follow the |
| 6 | +installation instructions closely, making sure that you also follow any rules |
| 7 | +governing zipcode data distributed by unitedstateszipcodes.org as per their |
| 8 | +site. The data is free for non-commercial use, and affordable for commercial |
| 9 | +use, you must download it from them though as I am not allowed to distribute it. |
4 | 10 |
|
5 | | -**IMPORTANT.** This package uses up-to-date data from unitedstateszipcodes.org. |
6 | | -If you wish to use this package for non-commercial purposes, please do! If you |
7 | | -are using this as part of a commercial purpose, that is fine too, but before you |
8 | | -do, you must buy a license to use this data from unitedstateszipcodes.org, |
9 | | -[here](https://www.unitedstateszipcodes.org/zip-code-database/) - the license |
10 | | -will be 40-200 dollars depending on the size of your business. |
| 11 | +## Installation |
11 | 12 |
|
| 13 | +1. go to https://www.unitedstateszipcodes.org/zip-code-database/ |
| 14 | +2. download the CSV file, pick free or a commercial version if you need to use |
| 15 | + it commercially. If you need to buy the commercial one, do so, but download |
| 16 | + the free one as that is the one supported by this package. |
| 17 | +3. move the downloaded file to a good location and set appropriate environment |
| 18 | + variables. |
| 19 | +```bash |
| 20 | +mkdir -p /var/lib/zipcode |
| 21 | +mv ~/Downloads/zip_code_database.csv /var/lib/zipcode/zip_code_database.csv |
| 22 | +echo 'ZIPS_CSV=/var/lib/zipcode/zip_code_database.csv' >> ~/.bash_profile |
| 23 | +source ~/.bash_profile |
| 24 | +``` |
| 25 | +4. set up the database. |
| 26 | + - for production applications a relational database like |
| 27 | + postgresql is recommended! sqlite is acceptable for lower use applications. |
| 28 | + - after you decide which database to use, *find your connection string* |
| 29 | + [here](http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine). |
| 30 | +```bash |
| 31 | +# set connection string, we use sqlite as an example (but use postgres in production!) |
| 32 | +echo 'ZIPCODE_CONNECTION_STRING=sqlite:///zipcode.db' >> ~/.bash_profile |
| 33 | +source ~/.bash_profile |
| 34 | +``` |
| 35 | +5. install zipcode |
| 36 | +```bash |
| 37 | +pip install zipcode |
| 38 | +``` |
| 39 | +6. populate the database |
| 40 | + - this might take a while ~10min for postgres. be patient, it'll be fast once loaded |
| 41 | +```bash |
| 42 | +build_zipcode_database |
| 43 | +``` |
| 44 | +Good to go. The next section shows you how to use the package. |
12 | 45 |
|
13 | 46 | ## Getting started |
14 | 47 |
|
15 | | -Simple package for dealing with zip codes in python. |
16 | | -Full documentation at https://pythonhosted.org/zipcode |
| 48 | +```py |
| 49 | +import zipcode |
17 | 50 |
|
18 | | - >>> import zipcode |
19 | | - >>> |
20 | | - >>> myzip = zipcode.isequal('44102') |
21 | | - >>> myzip.state #=> 'OH' |
22 | | - >>> myzip.city #=> 'Cleveland' |
23 | | - >>> |
24 | | - >>> dict(myzip) #=> {'zip_type': u'STANDARD', 'city': u'Cleveland', 'decommissioned': 0, 'zip': u'44102', 'state': u'OH', 'secondary_cities': [u''], 'location': 'Cleveland, OH', 'area_codes': [u'216'], 'lat': 41.48, 'timezone': u'America/New_York', 'lng': -81.74, 'population': 31930} |
25 | | - >>> |
26 | | - >>> #all keys in the dictionary can be fetched with dot notation. |
27 | | - >>> |
28 | | - >>> zipcode.islike('00') #=> list of Zip objects that begin with given prefix. |
29 | | - >>> |
30 | | - >>> cbus = (39.98, -82.98) |
31 | | - >>> zipcode.isinradius(cbus, 20) #=> list of all zip code objects within 20 miles of 'cbus' |
32 | | - >>> |
33 | | - >>> |
34 | | - >>> zipcode.hascity('Cleveland', 'OH') #=> list of zip codes in Cleveland, OH |
35 | | - >>> zipcode.hascity('', 'OH') #=> list of zip codes in OH |
36 | | - >>> |
37 | | - >>> |
38 | | - >>> zipcode.hasareacode(216) #=> list of zip codes associated with 216 |
39 | | - |
40 | | -## Keeping the database up-to-date |
| 51 | +myzip = zipcode.isequal('44102') |
| 52 | +myzip.state #=> 'OH' |
| 53 | +myzip.city #=> 'Cleveland' |
| 54 | +myzip.location #=> 'Cleveland, OH' |
41 | 55 |
|
42 | | -Zip codes don't change very often, but the borders do change, and new zip codes |
43 | | -are added, and others are removed. To keep your zipcode package ever up-to-date |
44 | | -we suggest that you set up a job to keep the sqlite3 database current. |
| 56 | +# all keys in the dictionary can also be fetched with dot notation. |
| 57 | +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': []} |
45 | 58 |
|
46 | | -You'll pull the latest version of the database from our server once a month, and |
47 | | -copy it to the install location of your current sqlite3 database. |
48 | 59 |
|
49 | | -```bash |
50 | | -$ pip show zipcode |
51 | | -Name: zipcode |
52 | | -Version: 3.0.0 |
53 | | -Summary: A simple python package for dealing with zip codes in python. |
54 | | -Home-page: https://github.com/buckmaxwell/zipcode |
55 | | -Author: Max Buck |
56 | | -Author-email: maxbuckdeveloper@gmail.com |
57 | | -License: MIT |
58 | | -Location: /<YOUR/<PATH> |
59 | | -Requires: haversine |
60 | | -``` |
| 60 | +zipcode.islike('00') #=> list of Zip objects that begin with given prefix. |
61 | 61 |
|
62 | | -Note the location line. This is the top level of your version of the zipcode |
63 | | -package. If the whole thing were set to a variable, the database would live at |
64 | | -$LOCATION/zipcode.db - and look something like |
65 | | -/<YOURPATH>/site-packages/zipcode/zipcode.db. For the next part imagine the |
66 | | -whole path to the database is set to $DB_PATH. |
| 62 | +cbus = (39.98, -82.98) |
| 63 | +zipcode.isinradius(cbus, 20) #=> list of all zip code objects within 20 miles of 'cbus' |
67 | 64 |
|
68 | | -Now, take the following and modify the user agent to a value of your choice. The |
69 | | -user agent must begin with robot. We ask that you limit your download requests |
70 | | -to monthly - the database will not change more often than that. You can copy the |
71 | | -script and put it in your crontab or a script that is run by your crontab. |
| 65 | +zipcode.hascity('Cleveland', 'OH') #=> list of zip codes in Cleveland, OH |
| 66 | +zipcode.hascity('', 'OH') #=> list of zip codes in state of OH |
| 67 | +zipcode.hascity('Flushing', 'NY', include_secondary=False) #=> don't include zips where flushing is a secondary city |
72 | 68 |
|
73 | | -```bash |
74 | | -wget -d --header="User-Agent: robot/<SOME UNIQUE NAME>" https://maxwellbuck.com/downloads/zipcode.db.gz |
75 | | -gunzip zipcode.db.gz |
76 | | -cp zipcode.db $DB_PATH |
| 69 | +zipcode.hasareacode(216) #=> list of zip codes associated with 216 |
77 | 70 | ``` |
78 | 71 |
|
| 72 | +## Keeping the database up-to-date |
| 73 | + |
| 74 | +Zip codes don't change very often, but the borders do change, and new zip codes |
| 75 | +are added, and others are removed. To keep your zipcode package ever up-to-date |
| 76 | +we suggest that you set up a job to keep the database current, or simply drop |
| 77 | +the zipcodes table in your database and re-rerun steps 1-3 and 6 once every 3 |
| 78 | +months or so. |
| 79 | + |
79 | 80 | You're all set. Get to it! |
0 commit comments