diff 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
line wrap: on
line diff
--- /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