Mercurial > hg > GlobalNeighbors
annotate globalneighbors/neighbors.py @ 22:e69cb496324e
we have a data dump
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 25 Jun 2017 17:45:19 -0700 |
parents | 2fef925fbf37 |
children | 6891c5523b69 |
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 |
22 | 5 `geoid [(geoid_closest_neighbor, distance), (geoid_2nd_closest_neighbor, distance), ...]` |
6 | |
7 *PER LINE* this format was chosen because it is easier to | |
8 iteratively read and write vs JSON. | |
9 | |
10 While CSV could be made to fit this model, because | |
11 there are both distances and geo IDs as pairs, it is not | |
12 the most natural fit. So we'll settle for our own data model. | |
13 No, it's not the best, but so be it (for now). | |
20
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 """ |
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 import json |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 import os |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 string = (str, basestring) # python2 |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 def read_neighbors_file(f): |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 if isinstance(f, string): |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 with open(f) as _f: |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 return read_neighbors_file(f) |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 retval = {} |
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 for line in f: |
22 | 29 key, value = line.split(None, 1) |
30 key = int(key) | |
31 data = json.loads(value) | |
32 data = [tuple(item) for item in data] | |
33 retval[key] = data | |
20
2fef925fbf37
display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 return retval |