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
0 commit comments