Mercurial > hg > config
comparison python/address.py @ 0:f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
| author | k0s <k0scist@gmail.com> |
|---|---|
| date | Thu, 15 Oct 2009 11:41:26 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f3ab51c79813 |
|---|---|
| 1 #!/usr/bin/python | |
| 2 | |
| 3 import sys | |
| 4 from states import * | |
| 5 | |
| 6 def validate_zipcode(zip, zip4=None): | |
| 7 """ validate a zipcode""" | |
| 8 | |
| 9 if not zip: | |
| 10 # a non-existant zip-code is a valid zipcode | |
| 11 # ... i think.... | |
| 12 return True | |
| 13 | |
| 14 if '-' in zip: # in this case, split zip into zip5 + zip4 | |
| 15 zip, zip4 = zip.split('-') | |
| 16 | |
| 17 zdict = { 'zip5': zip, 'zip4': zip4 } | |
| 18 | |
| 19 # if the 4-digit extension exists, add it to zip | |
| 20 if zip4: | |
| 21 zip += '-' + zip4 | |
| 22 | |
| 23 # validate zip code format | |
| 24 for i in 5, 4: | |
| 25 zstring = 'zip' + str(i) | |
| 26 z = zdict.get(zstring, '') | |
| 27 if z: | |
| 28 if (not z.isdigit()) or (len(z) != i): | |
| 29 return False | |
| 30 | |
| 31 return zip | |
| 32 | |
| 33 def normalizeaddress(query): | |
| 34 """ returns normalize address, if it is an address """ | |
| 35 | |
| 36 # normalize the address | |
| 37 query = ','.join([i.strip() for i in query.split('\n')]) | |
| 38 querylist = [i.strip() for i in query.split(',')] | |
| 39 | |
| 40 lastentry = querylist[-1] | |
| 41 | |
| 42 if lastentry[-1].isdigit(): | |
| 43 # it must be a zip code | |
| 44 if lastentry[1].isalpha(): | |
| 45 querylist = querylist[:-1] + lastentry.split() | |
| 46 if not validate_zipcode(querylist[-1]): | |
| 47 return False | |
| 48 state = querylist[-2] | |
| 49 else: | |
| 50 state = querylist[-1] | |
| 51 | |
| 52 if not getstate(state): | |
| 53 return False | |
| 54 | |
| 55 return ', '.join(querylist) | |
| 56 | |
| 57 def address(query): | |
| 58 """ | |
| 59 format an address | |
| 60 -- query should be a string | |
| 61 """ | |
| 62 | |
| 63 if __name__ == '__main__': | |
| 64 i = normalizeaddress(' '.join(sys.argv[1:])) | |
| 65 if i: | |
| 66 print i | |
| 67 else: | |
| 68 print 'Not an address' |
