Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update to maxmind database

  • Loading branch information...
commit 216e64a410ec8120cd1379fd56548d54a22dca6f 1 parent 4e47e9b
@vangheem vangheem authored
View
12 CHANGES.txt
@@ -1,6 +1,18 @@
ChangeLog
=========
+0.4
+---
+
+* updated to use maxmind database http://www.maxmind.com/app/postalcode
+
+* now also keeps timezone and dst values
+
+* longitude and latitude is now contains negative numbers
+
+*
+
+
0.3
---
View
22 README.txt
@@ -2,29 +2,28 @@ Introduction
============
This package will allow you to get zip code information. The data used in this
-package is retrieved from http://www.census.gov/tiger/tms/gazetteer/zips.txt
+package is retrieved from http://pablotron.org/files/zipcodes-csv-10-Aug-2004.zip
pyzipcode uses a local sqlite database to run. You can replace it with your own
other storage mechanism with a little effort.
-Also, all longitude and latitude data is store assuming northern and western
-part of the world so there is no negative degrees...
-
Here is some basic usage..
>>> from pyzipcode import ZipCodeDatabase
>>> zcdb = ZipCodeDatabase()
- >>> zipcode = zcdb['54115']
+ >>> zipcode = zcdb[54115]
>>> zipcode.zip
- '54115'
+ u'54115'
>>> zipcode.city
- u'DE PERE'
+ u'De Pere'
>>> zipcode.state
u'WI'
>>> zipcode.longitude
- 88.080613
+ -88.078959999999995
>>> zipcode.latitude
- 44.438778999999997
+ 44.42042
+ >>> zipcode.timezone
+ -6
Search zip codes...
@@ -32,13 +31,12 @@ Search zip codes...
>>> from pyzipcode import ZipCodeDatabase
>>> zcdb = ZipCodeDatabase()
>>> len(zcdb.find_zip(city="Oshkosh"))
- 3
+ 7
Get a list of zipcodes around a radius of a zipcode
-
>>> from pyzipcode import ZipCodeDatabase
>>> zcdb = ZipCodeDatabase()
>>> [z.zip for z in zcdb.get_zipcodes_around_radius('54901', 10)]
- ['54901', '54904', '54932', '54952', '54956', '54979']
+ [u'54901', u'54902', u'54903', u'54904', u'54906', u'54927', u'54952', u'54956', u'54957', u'54979', u'54985']
View
2  pyzipcode/__init__.py
@@ -45,6 +45,8 @@ def __init__(self, data):
self.state = data[2]
self.longitude = data[3]
self.latitude = data[4]
+ self.timezone = data[5]
+ self.dst = data[6]
def format_result(zips):
if len(zips) > 0:
View
24 pyzipcode/import.py
@@ -1,6 +1,7 @@
from pysqlite2 import dbapi2 as sqlite3
import os
+import csv
try:
from settings import db_location
except:
@@ -10,28 +11,25 @@
c = conn.cursor()
c.execute("DROP TABLE IF EXISTS ZipCodes;")
-c.execute("CREATE TABLE ZipCodes(zip VARCHAR(5), city TEXT, state TEXT, longitude DOUBLE, latitude DOUBLE);")
+c.execute("CREATE TABLE ZipCodes(zip VARCHAR(5), city TEXT, state TEXT, longitude DOUBLE, latitude DOUBLE, timezone INT, dst INT);")
c.execute("CREATE INDEX zip_index ON ZipCodes(zip);")
c.execute("CREATE INDEX city_index ON ZipCodes(city);")
c.execute("CREATE INDEX state_index ON ZipCodes(state);")
-fi = open('zips.txt')
-lines = fi.readlines()
-states = {}
-cities = {}
-zips = {}
-for line in lines:
- zip, state, city, longt, lat = line.split(',')[1:-2]
- zip = int(zip.strip('"'))
- state = state.strip('"')
- city = city.strip('"')
+reader = csv.reader(open('zipcode.csv', "rb"))
+reader.next() # prime it
- c.execute("INSERT INTO ZipCodes values('%s', '%s', '%s', %s, %s)" % (
+for row in reader:
+ zip, city, state, lat, longt, timezone, dst = row
+
+ c.execute('INSERT INTO ZipCodes values("%s", "%s", "%s", %s, %s, %s, %s)' % (
zip,
city,
state,
float(longt),
- float(lat)
+ float(lat),
+ timezone,
+ dst
))
conn.commit()
View
22 pyzipcode/tests.py
@@ -11,19 +11,35 @@ def setUp(self):
def test_retrieves_zip_code_information(self):
zip = self.db['54115']
self.assertEquals(zip.zip, '54115')
- self.assertEquals(zip.city, "DE PERE")
+ self.assertEquals(zip.city, "De Pere")
self.assertEquals(zip.state, "WI")
+ def test_correct_longitude_value(self):
+ zip = self.db[54115]
+ self.assertTrue(zip.latitude > 44.42041 and zip.latitude < 44.42043)
+
+ def test_correct_latitude_value(self):
+ zip = self.db[54115]
+ self.assertTrue(zip.longitude > -88.07897 and zip.longitude < -88.07895)
+
+ def test_correct_timezone(self):
+ zip = self.db[54115]
+ self.assertEquals(zip.timezone, -6)
+
+ def test_correct_dst(self):
+ zip = self.db[54115]
+ self.assertEquals(zip.dst, 1)
+
def test_radius(self):
zips = self.db.get_zipcodes_around_radius('54115', 30)
self.assertTrue('54304' in [zip.zip for zip in zips])
def test_find_zip_by_city(self):
- zip = self.db.find_zip(city="DE PERE")[0]
+ zip = self.db.find_zip(city="De Pere")[0]
self.assertEquals('54115', zip.zip)
def test_find_zip_by_city_with_multiple_zips(self):
- zips = self.db.find_zip(city="GREEN BAY")
+ zips = self.db.find_zip(city="Green Bay")
self.assertTrue('54302' in [zip.zip for zip in zips])
def test_find_zips_in_state(self):
View
BIN  pyzipcode/zipcodes.db
Binary file not shown
View
2  setup.py
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
import sys, os
-version = '0.3'
+version = '0.4'
setup(name='pyzipcode',
version=version,
Please sign in to comment.
Something went wrong with that request. Please try again.