Skip to content

Commit

Permalink
Merge pull request #131 from mkpathcreate/master
Browse files Browse the repository at this point in the history
Fixed issue#130
  • Loading branch information
vallettea authored May 14, 2018
2 parents 5765b30 + a851be9 commit ef2974b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
5 changes: 3 additions & 2 deletions koala/Spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, G, cellmap, named_ranges, pointers = set(), outputs = set(),
self.addr_to_name = addr_to_name

addr_to_range = {}

for c in list(self.cellmap.values()):
if c.is_range and len(list(c.range.keys())) != 0: # could be better, but can't check on Exception types here...
addr = c.address() if c.is_named_range else c.range.name
Expand Down Expand Up @@ -367,7 +368,7 @@ def find_pointer_arguments(self, outputs = None):
else:
raise Exception('Volatiles should always have a formula')

for parent in self.G.predecessors_iter(cell): # climb up the tree
for parent in self.G.predecessors(cell): # climb up the tree
todo.append(parent)

done.add(cell)
Expand Down Expand Up @@ -455,7 +456,7 @@ def set_value(self, address, val):

if address not in self.cellmap:
raise Exception("Address not present in graph.")

address = address.replace('$','')
cell = self.cellmap[address]

Expand Down
51 changes: 22 additions & 29 deletions koala/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

########### based on custom format #################
def dump(self, fname):
outfile = gzip.GzipFile(fname, 'w')

outfile = gzip.GzipFile(fname,'w')

# write simple cells first
simple_cells = [cell for cell in list(self.cellmap.values()) if cell.is_range == False]
Expand Down Expand Up @@ -50,15 +50,14 @@ def parse_cell_info(cell):

for cell in simple_cells:
parse_cell_info(cell)

value = cell.value
if isinstance(value, unicode):
outfile.write(cell.value.encode('utf-8') + "\n")
outfile.write(cell.value.encode('utf-8') + b"\n")
else:
outfile.write(str(cell.value) + "\n")
outfile.write("====" + "\n")
outfile.write((str(cell.value) + u"\n").encode('utf-8'))
outfile.write(b"====" + b"\n")

outfile.write("-----" + "\n")
outfile.write(b"-----" + b"\n")

for cell in range_cells:
parse_cell_info(cell)
Expand All @@ -68,22 +67,22 @@ def parse_cell_info(cell):
else:
outfile.write((cell.range.name + u"\n").encode('utf-8'))

outfile.write("====" + "\n")
outfile.write("====" + "\n")
outfile.write(b"====" + b"\n")
outfile.write(b"====" + b"\n")

# writing the edges
outfile.write("edges" + "\n")
outfile.write(b"edges" + b"\n")
for source, target in self.G.edges():
outfile.write((source.address() + SEP + target.address() + u"\n").encode('utf-8'))

# writing the rest
if self.outputs is not None:
outfile.write("outputs" + "\n")
outfile.write(b"outputs" + b"\n")
outfile.write((SEP.join(self.outputs) + u"\n").encode('utf-8'))
if self.inputs is not None:
outfile.write("inputs" + "\n")
outfile.write(b"inputs" + b"\n")
outfile.write((SEP.join(self.inputs) + u"\n").encode('utf-8'))
outfile.write("named_ranges" + "\n")
outfile.write(b"named_ranges" + b"\n")
for k in self.named_ranges:
outfile.write((k + SEP + self.named_ranges[k] + u"\n").encode('utf-8'))

Expand Down Expand Up @@ -120,19 +119,21 @@ def to_float(string):
outputs = None
inputs = None
named_ranges = {}
infile = gzip.GzipFile(fname, 'r')

infile = gzip.GzipFile(fname, 'rb')
for line in infile.read().splitlines():

if line == "====":
line= line.decode("utf-8")

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":
elif line == "edges":
cellmap = {n.address(): n for n in nodes}
mode = "edges"
continue
Expand All @@ -147,40 +148,32 @@ def to_float(string):
continue

if mode == "node0":
[address, formula, python_expression, is_range, is_named_range, is_pointer, should_eval] = line.split(SEP)
[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
should_eval = should_eval
mode = "node1"
elif 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":
Expand All @@ -194,8 +187,8 @@ def to_float(string):
k,v = line.split(SEP)
named_ranges[k] = v

G = DiGraph(data = edges)

G = DiGraph(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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
setup(
name = "koala2",

version = "0.0.18",
version = "0.0.19",

author = "Ants, open innovation lab",
author_email = "[email protected]",
Expand Down

0 comments on commit ef2974b

Please sign in to comment.