def cell_from_dict(d): cell_is_range = type(d["value"]) == dict if cell_is_range: range = d["value"] if len(range["values"]) == 0: range["values"] = [None] * len(range["cells"]) value = RangeCore(range["cells"], range["values"], nrows = range["nrows"], ncols = range["ncols"]) else: value = d["value"] new_cell = Cell(d["address"], None, value=value, formula=d["formula"], is_range = cell_is_range, is_named_range=d["is_named_range"], should_eval=d["should_eval"]) new_cell.python_expression = d["python_expression"] new_cell.compile() return {"id": new_cell}
def load(fname): def clean_bool(string): if string == "0": return None else: return string def to_bool(string): if string == "1" or string == "True": return True elif string == "0" or string == "False": return False else: return string def to_float(string): if string == "None": return None try: return float(string) except: return string mode = "node0" nodes = [] edges = [] pointers = set() outputs = None inputs = None named_ranges = {} infile = gzip.GzipFile(fname, 'r') for line in infile.read().splitlines(): if line == "====": mode = "node0" continue if line == "-----": cellmap_temp = {n.address(): n for n in nodes} Range = RangeFactory(cellmap_temp) mode = "node0" continue elif line == "edges": cellmap = {n.address(): n for n in nodes} mode = "edges" continue elif line == "outputs": mode = "outputs" continue elif line == "inputs": mode = "inputs" continue elif line == "named_ranges": mode = "named_ranges" continue if mode == "node0": [ address, formula, python_expression, is_range, is_named_range, is_pointer, should_eval ] = line.split(SEP) formula = clean_bool(formula) python_expression = clean_bool(python_expression) is_range = to_bool(is_range) is_named_range = to_bool(is_named_range) is_pointer = to_bool(is_pointer) should_eval = should_eval mode = "node1" elif mode == "node1": if is_range: reference = json.loads( line ) if is_pointer else line # in order to be able to parse dicts vv = Range(reference) if is_pointer: if not is_named_range: address = vv.name pointers.add(address) cell = Cell(address, None, vv, formula, is_range, is_named_range, should_eval) cell.python_expression = python_expression nodes.append(cell) else: value = to_bool(to_float(line)) cell = Cell(address, None, value, formula, is_range, is_named_range, should_eval) cell.python_expression = python_expression if formula: if 'OFFSET' in formula or 'INDEX' in formula: pointers.add(address) cell.compile() nodes.append(cell) elif mode == "edges": source, target = line.split(SEP) edges.append((cellmap[source], cellmap[target])) elif mode == "outputs": outputs = line.split(SEP) elif mode == "inputs": inputs = line.split(SEP) elif mode == "named_ranges": k, v = line.split(SEP) named_ranges[k] = v G = DiGraph(data=edges) print "Graph loading done, %s nodes, %s edges, %s cellmap entries" % (len( G.nodes()), len(G.edges()), len(cellmap)) return (G, cellmap, named_ranges, pointers, outputs, inputs)
def load(fname): def clean_bool(string): if string == "0": return None else: return string def to_bool(string): if string == "1" or string == "True": return True elif string == "0" or string == "False": return False else: return string def to_float(string): if string == "None": return None try: return float(string) except: return string mode = "node0" nodes = [] edges = [] volatiles = set() outputs = None inputs = None named_ranges = {} infile = gzip.GzipFile(fname, 'r') for line in infile.read().splitlines(): if line == "====": mode = "node0" continue if line == "-----": cellmap_temp = {n.address(): n for n in nodes} Range = RangeFactory(cellmap_temp) mode = "node0" continue elif line == "edges": cellmap = {n.address(): n for n in nodes} mode = "edges" continue elif line == "outputs": mode = "outputs" continue elif line == "inputs": mode = "inputs" continue elif line == "named_ranges": mode = "named_ranges" continue if mode == "node0": [address, formula, python_expression, is_range, is_named_range, is_volatile, should_eval] = line.split(SEP) formula = clean_bool(formula) python_expression = clean_bool(python_expression) is_range = to_bool(is_range) is_named_range = to_bool(is_named_range) is_volatile = to_bool(is_volatile) should_eval = should_eval mode = "node1" elif mode == "node1": if is_range: reference = json.loads(line) if is_volatile else line # in order to be able to parse dicts vv = Range(reference) if is_volatile: if not is_named_range: address = vv.name volatiles.add(address) cell = Cell(address, None, vv, formula, is_range, is_named_range, should_eval) cell.python_expression = python_expression nodes.append(cell) else: value = to_bool(to_float(line)) cell = Cell(address, None, value, formula, is_range, is_named_range, should_eval) cell.python_expression = python_expression if formula: if 'OFFSET' in formula or 'INDEX' in formula: volatiles.add(address) cell.compile() nodes.append(cell) elif mode == "edges": source, target = line.split(SEP) edges.append((cellmap[source], cellmap[target])) elif mode == "outputs": outputs = line.split(SEP) elif mode == "inputs": inputs = line.split(SEP) elif mode == "named_ranges": k,v = line.split(SEP) named_ranges[k] = v G = DiGraph(data = edges) print "Graph loading done, %s nodes, %s edges, %s cellmap entries" % (len(G.nodes()),len(G.edges()),len(cellmap)) return (G, cellmap, named_ranges, volatiles, outputs, inputs)