Last active
January 1, 2016 17:59
-
-
Save vinaychittora/8180764 to your computer and use it in GitHub Desktop.
This is a short python snippet to find the address detail and it's postal code using pygeocoder lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
AUTHOR - Vinay Chittora | |
This api is written under pygeocoder | |
Google Maps api-v3 | |
INSTALLATION | |
------------------------------------------------------ | |
$ pip install pygeocoder | |
USAGE | |
------------------------------------------------------ | |
$ python get_postal_code <address> | |
""" | |
import time | |
import sys | |
from pygeocoder import Geocoder | |
from pygeolib import GeocoderError | |
class AddressValidation(): | |
def __init__(self, address): | |
self.address = address | |
self.result = self.get_clean_address() | |
def benchmark(self): | |
start_time = time.time() | |
print "Looking for a valid address -> @%s " % start_time | |
if not self.get_clean_address(): | |
print "This doesn't seems like a valid address" | |
sys.exit(1) | |
print "Found result in -> @%s " % str(time.time() - start_time) | |
print "Formatted Address = %s " % str(self.result.formatted_address) | |
print "Postal Code = %s " % self.result.postal_code | |
print "Geo Coordinates = %s " % str(self.result[0].coordinates) | |
def get_clean_address(self): | |
try: | |
self.result = Geocoder.geocode(self.address) | |
except GeocoderError: | |
self.result = None | |
return self.result | |
def get_postal_code(self): | |
if self.result: | |
return self.result.postal_code | |
else : | |
return None | |
def get_geo_coordinates(self): | |
if self.result: | |
return self.result[0].coordinates | |
else : | |
return None | |
def get_formatted_address(self): | |
if self.result: | |
return self.result.formatted_address | |
else : | |
return None | |
if __name__ == '__main__': | |
AddressValidation(sys.argv[1]).benchmark() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment