view globalneighbors/neighbors.py @ 24:40a9850076a4

[documentation] links
author Jeff Hammel <k0scist@gmail.com>
date Mon, 04 Sep 2017 14:54:38 -0700
parents 6891c5523b69
children
line wrap: on
line source

"""
read neighbors file;
this should be in the form of:

`geoid [(geoid_closest_neighbor, distance), (geoid_2nd_closest_neighbor, distance), ...]`

*PER LINE*  this format was chosen because it is easier to
iteratively read and write vs JSON.

While CSV could be made to fit this model, because
there are both distances and geo IDs as pairs, it is not
the most natural fit.  So we'll settle for our own data model.
No, it's not the best, but so be it (for now).
"""

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:
        key, value = line.split(None, 1)
        key = int(key)
        data = json.loads(value)
        data = [tuple(item) for item in data]
        retval[key] = data
    return retval