diff globalneighbors/distance.py @ 22:e69cb496324e

we have a data dump
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 17:45:19 -0700
parents 811adc9736eb
children
line wrap: on
line diff
--- a/globalneighbors/distance.py	Sun Jun 25 16:28:56 2017 -0700
+++ b/globalneighbors/distance.py	Sun Jun 25 17:45:19 2017 -0700
@@ -158,6 +158,11 @@
     return neighbors
 
 
+def write_neighbors(fp, neighbors):
+    for key, value in neighbors.iteritems():
+        fp.write("{key} {value}\n".format(key=key,
+                                        value=json.dumps(value)))
+
 def main(args=sys.argv[1:]):
     """CLI"""
 
@@ -179,11 +184,11 @@
                         help="number of neighbors to determine [DEFAULT: %(default)s]")
     options = parser.parse_args(args)
 
-    # parse cities
-    cities = list(read_cities(options.cities, fields=fields))
+    # get locations
+    city_locations = locations(read_cities(options.cities, fields=fields))
+    options.cities.close()
+    options.output.close()
 
-    # get locations
-    city_locations = locations(cities)
 
     # calculate neighbors
     neighbors = calculate_neighbors(city_locations,
@@ -195,7 +200,9 @@
     # output
     print ("Outputting neighbors")
     sys.stdout.flush()
-    options.output.write(json.dumps(neighbors))
+    import pdb; pdb.set_trace()
+    with open(options.output.name, 'w') as f:
+        f.write(json.dumps(neighbors))
 
 if __name__ == '__main__':
     main()