Skip to content

Commit 5522f1a

Browse files
committed
0.2.4 allow user to specify the database file dir to download to
1 parent 48289c3 commit 5522f1a

3 files changed

Lines changed: 43 additions & 25 deletions

File tree

uszipcode/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.2.3"
1+
__version__ = "0.2.4"
22

33
if __name__ == "__main__": # pragma: no cover
44
print(__version__)

uszipcode/db.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,51 @@
2323
from uszipcode.pkg.atomicwrites import atomic_write
2424
from uszipcode.pkg.sqlalchemy_mate import engine_creator
2525

26-
db_file_dir = Path("/tmp")
27-
db_file_dir.mkdir(exist_ok=True)
2826

29-
simple_db_file_path = db_file_dir.append_parts("uszipcode_simple_db.sqlite")
30-
db_file_path = db_file_dir.append_parts("uszipcode_db.sqlite")
27+
def get_simple_db_file_path(db_file_dir):
28+
return Path(db_file_dir, "simple_db.sqlite")
3129

3230

33-
def is_simple_db_file_exists():
31+
def get_db_file_path(db_file_dir):
32+
return Path(db_file_dir, "db.sqlite")
33+
34+
35+
def is_simple_db_file_exists(db_file_dir):
36+
simple_db_file_path = get_simple_db_file_path(db_file_dir)
3437
if simple_db_file_path.exists():
3538
if simple_db_file_path.size >= 5 * 1000 * 1000:
3639
return True
3740
return False
3841

3942

40-
def is_db_file_exists():
43+
def is_db_file_exists(db_file_dir):
44+
db_file_path = get_db_file_path(db_file_dir)
4145
if db_file_path.exists():
4246
if db_file_path.size >= 100 * 1000 * 1000:
4347
return True
4448
return False
4549

4650

47-
def connect_to_simple_zipcode_db():
48-
return engine_creator.create_sqlite(path=simple_db_file_path.abspath)
51+
def connect_to_simple_zipcode_db(db_file_dir):
52+
return engine_creator.create_sqlite(
53+
path=get_simple_db_file_path(db_file_dir).abspath)
4954

5055

51-
def connect_to_zipcode_db():
52-
return engine_creator.create_sqlite(path=db_file_path.abspath)
56+
def connect_to_zipcode_db(db_file_dir):
57+
return engine_creator.create_sqlite(
58+
path=get_db_file_path(db_file_dir).abspath)
5359

5460

55-
def download_simple_db_file():
61+
def download_simple_db_file(db_file_dir):
5662
simple_db_file_download_url = "https://datahub.io/machu-gwu/uszipcode-0.2.0-simple_db/r/simple_db.sqlite"
5763

58-
if not is_simple_db_file_exists():
64+
if not is_simple_db_file_exists(db_file_dir):
5965
print("Start downloading data for simple zipcode database, total size 9MB ...")
6066
response = requests.get(simple_db_file_download_url, stream=True)
6167
chunk_size = 1 * 1024 ** 2
6268

6369
counter = 0
64-
with atomic_write(simple_db_file_path.abspath, mode="wb", overwrite=True) as f:
70+
with atomic_write(get_simple_db_file_path(db_file_dir).abspath, mode="wb", overwrite=True) as f:
6571
for chunk in response.iter_content(chunk_size):
6672
if not chunk:
6773
break
@@ -71,16 +77,16 @@ def download_simple_db_file():
7177
print(" Complete!")
7278

7379

74-
def download_db_file():
80+
def download_db_file(db_file_dir):
7581
db_file_download_url = "https://datahub.io/machu-gwu/uszipcode-0.2.0-db/r/db.sqlite"
76-
if not is_db_file_exists():
82+
if not is_db_file_exists(db_file_dir):
7783
print(
7884
"Start downloading data for rich info zipcode database, total size 450+MB ...")
7985
response = requests.get(db_file_download_url, stream=True)
8086
chunk_size = 10 * 1024 ** 2
8187

8288
counter = 0
83-
with atomic_write(db_file_path.abspath, mode="wb", overwrite=True) as f:
89+
with atomic_write(get_db_file_path(db_file_dir).abspath, mode="wb", overwrite=True) as f:
8490
for chunk in response.iter_content(chunk_size):
8591
if not chunk:
8692
break

uszipcode/search.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from collections import OrderedDict
1313
from sqlalchemy.orm import sessionmaker
1414
from six import integer_types, string_types
15+
from pathlib_mate import Path
1516

1617
from .db import (
1718
is_simple_db_file_exists, is_db_file_exists,
@@ -33,14 +34,23 @@
3334
"""
3435

3536

37+
HOME = Path.home().abspath
38+
HOME_USZIPCODE = Path(HOME, ".uszipcode").abspath
39+
TMP = "/tmp"
40+
41+
3642
class SearchEngine(object):
3743
"""
3844
Zipcode Search Engine.
3945
40-
:param simple_zipcode: bool, default True, if True, use the simple zipcode
46+
:type simple_zipcode: bool
47+
:param simple_zipcode: default True, if True, use the simple zipcode
4148
db. Rich Demographics, Real Estate, Employment, Education info is not
4249
available. If False, use the rich info database.
4350
51+
:type db_file_dir: str
52+
:param db_file_dir: where you want to download the sqlite database to.
53+
4454
Usage::
4555
4656
>>> search = SearchEngine()
@@ -75,16 +85,18 @@ class SearchEngine(object):
7585
_state_to_city_mapper = None
7686
_city_to_state_mapper = None
7787

78-
def __init__(self, simple_zipcode=True):
88+
def __init__(self, simple_zipcode=True, db_file_dir=HOME_USZIPCODE):
89+
Path(db_file_dir).mkdir(exist_ok=True)
90+
7991
if simple_zipcode:
80-
if not is_simple_db_file_exists():
81-
download_simple_db_file()
82-
engine = connect_to_simple_zipcode_db()
92+
if not is_simple_db_file_exists(db_file_dir):
93+
download_simple_db_file(db_file_dir)
94+
engine = connect_to_simple_zipcode_db(db_file_dir)
8395
self.zip_klass = SimpleZipcode
8496
else: # pragma: no cover
85-
if not is_db_file_exists():
86-
download_db_file()
87-
engine = connect_to_zipcode_db()
97+
if not is_db_file_exists(db_file_dir):
98+
download_db_file(db_file_dir)
99+
engine = connect_to_zipcode_db(db_file_dir)
88100
self.zip_klass = Zipcode
89101
self.engine = engine
90102
self.ses = sessionmaker(bind=engine)()

0 commit comments

Comments
 (0)