Skip to content

Commit 5f41fc7

Browse files
committed
version 0.2.6
1 parent 75d52ec commit 5f41fc7

8 files changed

Lines changed: 108 additions & 41 deletions

File tree

README.rst

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ Rich search methods are provided for getting zipcode in the way you want.
253253
.. code-block:: python
254254
255255
# Looking for Chicago and IL, but entered wrong spelling.
256-
>>> res = search.by_city_and_state("cicago", "il")
256+
>>> res = search.by_city_and_state("cicago", "il", returns=999) # only returns first 999 results
257257
>>> len(res) # 56 zipcodes in Chicago
258258
56
259259
>>> zipcode = res[0]
@@ -276,14 +276,37 @@ You can **easily sort your results** by any field, or distance from a coordinate
276276
Deploy Uszipcode as a Web Service
277277
------------------------------------------------------------------------------
278278

279-
If you want to build a private uszipcode API server you have two choice:
279+
I collect lots of feedback from organization user that people want to host the database file privately. And people may love to use different rdbms backend like mysql or psql. From ``0.2.6``, this is easy.
280280

281-
1. Use a VM like EC2 machine, and deploy a web api server with the machine.
282-
2. (RECOMMEND) Dump the sqlite database to any relational database like Postgres, MySQL, and inject the database connection info in your application server.
281+
**Host the database file privately**:
282+
283+
1. download db file from https://github.com/MacHu-GWU/uszipcode-project/releases/tag/0.2.6-db-file
284+
2. reupload it to your private storage.
285+
3. use ``download_url`` parameter:
286+
287+
.. code-block:: python
288+
289+
search = SearchEngine(download_url="your-private-host")
290+
291+
**Use different RDBMS backend**:
283292

284-
In the feature release, I will provide an easy way that allow you to deploy uszipcode as a private web api service.
293+
1. Let's use MySQL as example.
294+
2. Download db file.
295+
3. use `DBeaver <https://dbeaver.io/>`_ to connect to both sqlite and mysql.
296+
4. dump sqlite as csv and load it to mysql.
297+
5. use ``engine`` parameter
285298

286-
Thank you.
299+
.. code-block:: python
300+
301+
from uszipcode.pkg.sqlalchemy_mate import engine_creator
302+
303+
engine = create_postgresql(username, password, host, port, database)
304+
search = SearchEngine(engine=engine)
305+
306+
**Deploy uszipcode as Web API**:
307+
308+
1. Use a VM like EC2 machine, and deploy a web api server with the machine.
309+
2. (RECOMMEND) Dump the sqlite database to any relational database like Postgres, MySQL, and inject the database connection info in your application server.
287310

288311

289312
.. _install:

bin/settings.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ github_repo_name="uszipcode-project"
1313

1414
# Python
1515
package_name="uszipcode"
16-
py_ver_major="2"
16+
py_ver_major="3"
1717
py_ver_minor="7"
18-
py_ver_micro="13"
19-
use_pyenv="Y" # "Y" or "N"
18+
py_ver_micro="9"
19+
use_pyenv="N" # "Y" or "N"
2020
supported_py_versions="2.7.13 3.4.6 3.5.3 3.6.2" # e.g: "2.7.13 3.6.2"
2121

2222

release-history.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
1-
.. _changelog:
1+
.. _release_history:
22

33
Release and Version History
44
==============================================================================
55

66

7-
0.2.5 (TODO)
7+
0.2.7 (TODO)
88
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99
**Features and Improvements**
1010

11-
- Get lat lng and more geo info from Google Geocoding API for **Unique** and **PO Box** zipcode.
12-
- Allow user to dump the data to any relational database easily and use self-hosted database to query.
11+
**Minor Improvements**
12+
13+
**Bugfixes**
14+
15+
**Miscellaneous**
16+
17+
18+
0.2.6 (2021-06-07)
19+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+
21+
**Features and Improvements**
22+
23+
- Allow developer to use different download url in ``SearchEngine(..., download_url="your-own-download-url")``. So developer can upload the db file to a private file host server.
24+
- Allow developer to use different database backend like mysql, postgres, oracle, mssql in ``SearchEngine(..., engine=custome_sqlachemy_engine)``. So developer can dump the sqlite to csv and load it to any database you want to use.
1325

1426
**Minor Improvements**
1527

28+
- rehost the database file on GitHub
29+
1630
**Bugfixes**
1731

1832
**Miscellaneous**
1933

34+
- Drop support < Python3.6, only support 3.6+
35+
36+
37+
0.2.5 (2021-04-20)
38+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39+
40+
**Bugfixes**
41+
42+
- fix compability issue with sqlachemy>=1.4.X
43+
2044

2145
0.2.4 (2019-10-16)
2246
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
attrs
2-
SQLAlchemy>=1.4.0
2+
SQLAlchemy
33
pathlib_mate
44
requests

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.5"
1+
__version__ = "0.2.6"
22

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

uszipcode/db.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from __future__ import print_function
1414

1515
import requests
16-
1716
from pathlib_mate import PathCls as Path
1817

1918
try:
@@ -23,8 +22,9 @@
2322
from uszipcode.pkg.atomicwrites import atomic_write
2423
from uszipcode.pkg.sqlalchemy_mate import engine_creator
2524

26-
SIMPLE_DB_FILE_DOWNOAD_URL = "https://datahub.io/machu-gwu/uszipcode-0.2.0-simple_db/r/simple_db.sqlite"
27-
DB_FILE_DOWNOAD_URL = "https://datahub.io/machu-gwu/uszipcode-0.2.0-db/r/db.sqlite"
25+
SIMPLE_DB_FILE_DOWNOAD_URL = "https://github.com/MacHu-GWU/uszipcode-project/releases/download/0.2.6-db-file/simple_db.sqlite"
26+
DB_FILE_DOWNOAD_URL = "https://github.com/MacHu-GWU/uszipcode-project/releases/download/0.2.6-db-file/db.sqlite"
27+
2828

2929
def get_simple_db_file_path(db_file_dir):
3030
return Path(db_file_dir, "simple_db.sqlite")

uszipcode/pkg/sqlalchemy_mate/pt.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77

88
from sqlalchemy import select, Table
99
from sqlalchemy.orm import sessionmaker, Query
10-
from sqlalchemy.orm import DeclarativeMeta
10+
try:
11+
from sqlalchemy.orm import DeclarativeMeta
12+
except: # for sqlalchemy<1.4 compatibility
13+
from sqlalchemy.ext.declarative.api import DeclarativeMeta
1114
from sqlalchemy.sql.selectable import Select
12-
from sqlalchemy.engine.result import Result
15+
try:
16+
from sqlalchemy.engine.result import Result
17+
except: # for sqlalchemy<1.4 compatibility
18+
from sqlalchemy.engine.result import ResultProxy as Result
1319

1420
try:
1521
from .utils import execute_query_return_result_proxy
@@ -105,7 +111,7 @@ def from_object(orm_class, engine, limit=None):
105111
return from_db_cursor(result_proxy.cursor)
106112

107113

108-
def from_resultproxy(result_proxy):
114+
def from_resultproxy(result_proxy: Result):
109115
"""
110116
Construct a Prettytable from ``ResultProxy``.
111117

uszipcode/search.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
This module allows developer to query zipcode with super clean API.
55
"""
66

7-
import sys
8-
import math
97
import heapq
8+
import math
9+
import sys
10+
from collections import OrderedDict
1011

1112
import sqlalchemy as sa
12-
from collections import OrderedDict
13-
from sqlalchemy.orm import sessionmaker
14-
from six import integer_types, string_types
1513
from pathlib_mate import Path
14+
from six import integer_types, string_types
15+
from sqlalchemy.orm import sessionmaker
1616

1717
from .db import (
1818
is_simple_db_file_exists, is_db_file_exists,
1919
connect_to_simple_zipcode_db, connect_to_zipcode_db,
2020
download_simple_db_file, download_db_file
2121
)
2222
from .model import SimpleZipcode, Zipcode, ZipcodeType
23-
from .state_abbr import STATE_ABBR_SHORT_TO_LONG, STATE_ABBR_LONG_TO_SHORT
2423
from .pkg.fuzzywuzzy.process import extract, extractOne
24+
from .state_abbr import STATE_ABBR_SHORT_TO_LONG, STATE_ABBR_LONG_TO_SHORT
2525

2626
SORT_BY_DIST = "dist"
2727
"""
@@ -33,10 +33,8 @@
3333
default number of results to return.
3434
"""
3535

36-
3736
HOME = Path.home().abspath
3837
HOME_USZIPCODE = Path(HOME, ".uszipcode").abspath
39-
TMP = "/tmp"
4038

4139

4240
class SearchEngine(object):
@@ -49,7 +47,17 @@ class SearchEngine(object):
4947
available. If False, use the rich info database.
5048
5149
:type db_file_dir: str
52-
:param db_file_dir: where you want to download the sqlite database to.
50+
:param db_file_dir: where you want to download the sqlite database to. This
51+
property allows you to customize where you want to store the data file
52+
locally. by default it is ${HOME}/.uszipcode
53+
54+
:type download_url: str
55+
:param download_url: where you want to download the sqlite database file from.
56+
This property allows you to upload the .sqlite file to your private file
57+
host and download from it. In case the default download url fail.
58+
59+
:param engine: a sqlachemy engine object. It allows you to use any
60+
backend database.
5361
5462
Usage::
5563
@@ -85,19 +93,25 @@ class SearchEngine(object):
8593
_state_to_city_mapper = None
8694
_city_to_state_mapper = None
8795

88-
def __init__(self, simple_zipcode=True, db_file_dir=HOME_USZIPCODE, download_url=None):
96+
def __init__(self,
97+
simple_zipcode=True,
98+
db_file_dir=HOME_USZIPCODE,
99+
download_url=None,
100+
engine=None):
89101
Path(db_file_dir).mkdir(exist_ok=True)
90102

91-
if simple_zipcode:
92-
if not is_simple_db_file_exists(db_file_dir):
93-
download_simple_db_file(db_file_dir, download_url=download_url)
94-
engine = connect_to_simple_zipcode_db(db_file_dir)
95-
self.zip_klass = SimpleZipcode
96-
else: # pragma: no cover
97-
if not is_db_file_exists(db_file_dir):
98-
download_db_file(db_file_dir, download_url=download_url)
99-
engine = connect_to_zipcode_db(db_file_dir)
100-
self.zip_klass = Zipcode
103+
if engine is None:
104+
if simple_zipcode:
105+
if not is_simple_db_file_exists(db_file_dir):
106+
download_simple_db_file(db_file_dir, download_url=download_url)
107+
engine = connect_to_simple_zipcode_db(db_file_dir)
108+
self.zip_klass = SimpleZipcode
109+
else: # pragma: no cover
110+
if not is_db_file_exists(db_file_dir):
111+
download_db_file(db_file_dir, download_url=download_url)
112+
engine = connect_to_zipcode_db(db_file_dir)
113+
self.zip_klass = Zipcode
114+
101115
self.engine = engine
102116
self.ses = sessionmaker(bind=engine)()
103117

0 commit comments

Comments
 (0)