annotate globalneighbors/neighbors.py @ 20:2fef925fbf37

display country + population in autocomplete drop down
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 16:12:08 -0700
parents
children e69cb496324e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 """
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 read neighbors file;
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 this should be in the form of:
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 `{geoid: [(geoid_closest_neighbor, distance),
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 (geoid_2nd_closest_neighbor, distance),
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 ...]
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 }`
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 """
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import json
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import os
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 string = (str, basestring) # python2
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 def read_neighbors_file(f):
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 if isinstance(f, string):
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 with open(f) as _f:
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 return read_neighbors_file(f)
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 retval = {}
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 for line in f:
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 data = json.loads(line)
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 retval.update(data)
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 return retval