Skip to content

Commit

Permalink
parsing conversions into graph
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Hodes committed Jun 26, 2011
1 parent 78153d4 commit 7325695
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion parse.py
@@ -1,5 +1,5 @@
"""
This module is used to parse the various input files and store them in
This module is used to parse the various input files and store them in
the correct internal form.
"""
import re, core
Expand Down Expand Up @@ -52,3 +52,31 @@ def parse(infile, parseopts=None):
items.append(item)

return items


def parseConversions(infile, parseopts=None):
"""Returns a graph to be used for conversions. Generated from the
infile.
infile must have lines of the form:
n unit1 = m unit2
where n and m are arbitrary rational numbers, or blank (=1).
graph = [edge]
edge = (node_origin, node_dest, lambda node_origin: C * x)
where C is the conversion factor between node_origin and node_dest"""
graph = []
reg = r"(\d+/?\d*)?\s*(\w+)"
for line in infile:
if len(line)>1:
splits = line.split("=")
f = re.match(reg, splits[0]).groups()
g = re.match(reg, splits[1]).groups()
n = m = 1
if f[0]: n = Fraction(f[0])
if g[0]: m = Fraction(g[0])
graph.append((f[1], g[1], lambda x: Fraction((m/n) * x)))
graph.append((g[1], f[1], lambda x: Fraction((n/m) * x)))
return graph


0 comments on commit 7325695

Please sign in to comment.