Skip to content
This repository was archived by the owner on Oct 14, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions pyzipcode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from settings import db_location
from .settings import db_location
try:
import sqlite3
except ImportError:
Expand All @@ -10,12 +10,12 @@ class ConnectionManager(object):
"""
Assumes a database that will work with cursor objects
"""

def __init__(self):
# test out the connection...
conn = sqlite3.connect(db_location)
conn.close()

def query(self, sql, args):
conn = None
retry_count = 0
Expand All @@ -24,13 +24,13 @@ def query(self, sql, args):
# then just give up...
try:
conn = sqlite3.connect(db_location)
except sqlite3.OperationalError, x:
except sqlite3.OperationalError as x:
retry_count += 1
time.sleep(0.001)

if not conn and retry_count > 10:
raise sqlite3.OperationalError("Can't connect to sqlite database.")

cursor = conn.cursor()
cursor.execute(sql, args)
res = cursor.fetchall()
Expand Down Expand Up @@ -59,56 +59,50 @@ def format_result(zips):

class ZipNotFoundException(Exception):
pass

class ZipCodeDatabase(object):

def __init__(self, conn_manager=None):
if conn_manager is None:
conn_manager = ConnectionManager()
self.conn_manager = conn_manager

def get_zipcodes_around_radius(self, zip, radius):
zips = self.get(zip)
if zips is None:
raise ZipNotFoundException("Could not find zip code you're searching by.")
else:
zip = zips[0]

radius = float(radius)

long_range = (zip.longitude-(radius/69.0), zip.longitude+(radius/69.0))
lat_range = (zip.latitude-(radius/49.0), zip.latitude+(radius/49.0))

return format_result(self.conn_manager.query(ZIP_RANGE_QUERY % (
long_range[0], long_range[1],
lat_range[0], lat_range[1]
)))

def find_zip(self, city=None, state=None):
if city is None:
city = "%"
else:
city = city.upper()

if state is None:
state = "%"
else:
state = state.upper()

return format_result(self.conn_manager.query(ZIP_FIND_QUERY, [city, state]))

def get(self, zip):
return format_result(self.conn_manager.query(ZIP_QUERY, [zip]))

def __getitem__(self, zip):
zip = self.get(str(zip))
if zip is None:
raise IndexError("Couldn't find zip")
else:
return zip[0]






8 changes: 4 additions & 4 deletions pyzipcode/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import csv
try:
from settings import db_location
from .settings import db_location
except:
from pyzipcode.settings import db_location

Expand All @@ -18,10 +18,10 @@

reader = csv.reader(open('zipcode.csv', "rb"))
reader.next() # prime it

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,
Expand All @@ -31,7 +31,7 @@
timezone,
dst
))

conn.commit()

# We can also close the cursor if we are done with it
Expand Down
Binary file modified pyzipcode/zipcodes.db
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
import sys, os

version = '0.4'
version = '0.7'

try:
import sqlite3
Expand Down