Database Reference
In-Depth Information
that you can process it sequentially. This listing is a baseline that you can
compare against a parallel version.
Listing 12.7 : Resolving to ZIP code (add_zip.py)
import json
import sys
# Imports from files in local directory:
from kdtree import KDTree
class ZipPoint(tuple):
'''Tuple containing a lat, long, and zip code.'''
def __new__(cls, json_dict):
return super(ZipPoint, cls).__new__(
cls, (json_dict['lat'], json_dict['lng']))
def __init__(self, json_dict):
self.zip = json_dict['zip']
with open('zip_centers.json', 'r') as f:
ZIP_INDEX = KDTree([ZipPoint(json.loads(r)) for r in
f])
def apply(input):
val = json.loads(input)
closest = ZIP_INDEX.query((val['lat'], val['lng']))
if closest:
val['zip'] = closest[0].zip
yield json.dumps(val) + '\n'
else:
yield input
if __name__ == '__main__':
for line in sys.stdin:
for o in apply(line):
print o,
 
Search WWH ::




Custom Search