# HG changeset patch # User Jeff Hammel # Date 1498432328 25200 # Node ID 2fef925fbf377a9a66e81022196790e840aab9ed # Parent 811adc9736eb5997b8e3ad267b665e90e815d03a display country + population in autocomplete drop down diff -r 811adc9736eb -r 2fef925fbf37 globalneighbors/grid.py --- a/globalneighbors/grid.py Sun Jun 25 15:43:45 2017 -0700 +++ b/globalneighbors/grid.py Sun Jun 25 16:12:08 2017 -0700 @@ -78,7 +78,8 @@ class GriddedLocations(object): def __init__(self, locations): - raise NotImplementedError('TODO') + self.locations = locations + import pdb; pdb.set_trace() def main(args=sys.argv[1:]): """CLI""" @@ -90,6 +91,9 @@ # read locations city_locations = locations(parser.read_cities()) + # make a grid + gridded_locations = GriddedLocations(locations) + if __name__ == '__main__': main() diff -r 811adc9736eb -r 2fef925fbf37 globalneighbors/neighbors.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/globalneighbors/neighbors.py Sun Jun 25 16:12:08 2017 -0700 @@ -0,0 +1,26 @@ +""" +read neighbors file; +this should be in the form of: + +`{geoid: [(geoid_closest_neighbor, distance), + (geoid_2nd_closest_neighbor, distance), + ...] + }` +""" + +import json +import os + +string = (str, basestring) # python2 + +def read_neighbors_file(f): + + if isinstance(f, string): + with open(f) as _f: + return read_neighbors_file(f) + + retval = {} + for line in f: + data = json.loads(line) + retval.update(data) + return retval diff -r 811adc9736eb -r 2fef925fbf37 globalneighbors/templates/index.html --- a/globalneighbors/templates/index.html Sun Jun 25 15:43:45 2017 -0700 +++ b/globalneighbors/templates/index.html Sun Jun 25 16:12:08 2017 -0700 @@ -21,7 +21,7 @@ template: { type: "custom", method: function (value, item) { - return '' + value + ''; + return '' + value + ' ' + item["country code"] + '
Population: ' + item.population + '
'; } } }; diff -r 811adc9736eb -r 2fef925fbf37 globalneighbors/web.py --- a/globalneighbors/web.py Sun Jun 25 15:43:45 2017 -0700 +++ b/globalneighbors/web.py Sun Jun 25 16:12:08 2017 -0700 @@ -16,6 +16,7 @@ from wsgiref import simple_server from .locations import locations from .locations import city_dict +from .neighbors import read_neighbors_file from .read import read_cities from .read import read_city_list from .schema import fields @@ -113,7 +114,10 @@ content_type = 'text/html' - def __init__(self, datafile, template_dir=template_dir): + def __init__(self, + datafile, + neighbors_file=None, + template_dir=template_dir): # parse data @@ -121,6 +125,8 @@ self.cities = read_city_list(self.datafile, fields=fields) self.locations = locations(self.cities) + if neighbors_file: + pass # TODO # get country codes self.country_codes = sorted(set([city['country code'] @@ -180,10 +186,13 @@ parser.add_argument('--hostname', dest='hostname', default='localhost', help="host name [DEFAULT: %(default)s]") + parser.add_argument('--neighbors', dest='neighbors_file', + help="file for nearest neighbors") options = parser.parse_args(args) # instantiate WSGI handler - app = GlobalHandler(datafile=options.cities) + app = GlobalHandler(datafile=options.cities, + neighbors_file=options.neighbors_file) # wrap it in a static file server app = PassthroughFileserver(app) diff -r 811adc9736eb -r 2fef925fbf37 gunicorn.conf --- a/gunicorn.conf Sun Jun 25 15:43:45 2017 -0700 +++ b/gunicorn.conf Sun Jun 25 16:12:08 2017 -0700 @@ -1,2 +1,2 @@ [program:globalneighbors] -command=/home/jhammel/GlobalNeighbors/bin/gunicorn -w 2 --chdir /home/jhammel/GlobalNeighbors/src/GlobalNeighbors -b 0.0.0.0:8080 wsgiapp:application \ No newline at end of file +command=/home/jhammel/GlobalNeighbors/bin/gunicorn -w 2 --chdir /home/jhammel/GlobalNeighbors/src/GlobalNeighbors -b 0.0.0.0:8080 wsgiapp:application