Skip to main content

Quick and dirty script/api for syncing postal codes / zip codes with a local sqlite3 database.

Project description

# GEONAMESZIP
A quick and dirty script/api for syncing postal codes / zip codes with a local sqlite3 database.

### Why?
Personally I dislike hitting up a remote server for mostly static information.
Since GeoNames provides this data under a Creative Commons license, this script
pulls all countries and drops it in a sqlite3 for (relatively) fast lookup.

## Important!
This data is provided by GeoNames under the Creative Commons license (http://creativecommons.org/licenses/by/3.0/).
If you use this data, you must provide credit to them whenever it is used.

The python-geonameszip library itself is under the MIT license.

### Example:

Main uses are for postal code validation:

```python
import geonameszip
# NOTE: country is required due to duplicate zip codes based on country.
us_result = geonameszip.lookup_postal_code('77098', 'US')
print(us_result)
# {'city': u'Houston', 'country': u'US', 'lon': -95.4118, 'county': u'Harris', 'state': u'Texas', 'postal_code': u'77098', 'lat': 29.735, 'state_abbreviation': u'TX'}
mx_result = geonameszip.lookup_postal_code('77098', 'MX')
print(mx_result)
# {'city': u'Barrio Bravo', 'country': u'MX', 'lon': -88.6458, 'county': u'Othon P Blanco', 'state': u'Quintana Roo', 'postal_code': u'77098', 'lat': 19.4083, 'state_abbreviation': u'ROO'}
```


## API

`geonameszip`

```python
import_from_file(source_path)
```
- Drops the postal_codes table in the sqlite3 database
- Re-creates the tabl.e
- Inserts all the data provided by a file formatted like `allCountries.txt`.

```python
lookup_postal_code(postal_code, country, conn=None, cursor=None)
```
- Selects the first available option for the `postal_code`, `country` combination.
- NOTE: Currently, if multiple entries match, only the top-most is provided.

```python
update_postal_code(postal_code, country, city, state, state_abbreviation, county, lat, lon, conn=None,commit=True, cursor=None)
```
- _Inserts_ the values provided in the arguments into the database.
- NOTE: This always _inserts_ the data, never updates. The data isn't consistently unique and the database has no primary key so it isn't easy to provide a way to update a single row.
- This can create duplicate entries.


For python-geonameszip (this python code ONLY -- not the data):
The MIT License
===============

Copyright (c) 2009 Anton Grigoryev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


# GEONAMES Data:

Creative Commons Deed
=====================
_Licensees may copy, distribute, display and perform the work and make derivative works based on it only if they give the author or licensor the credits in the manner specified by [http://creativecommons.org/licenses/by/3.0/](http://creativecommons.org/licenses/by/3.0/)._

## Summary
_This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by/3.0/)._


### You are free to:
- *Share* — copy and redistribute the material in any medium or format
- *Adapt* — remix, transform, and build upon the material
- for any purpose, even commercially.
- The licensor cannot revoke these freedoms as long as you follow the license terms.


### Under the following terms:
- *Attribution* — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- *No additional restrictions* — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.


### Notices:

You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

geonameszip-0.1.4.tar.gz (7.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page