Commit 608dc099 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Removed node type from graph-to-line coverter.

parent 2f60cbbc
def generate_line(root, node_type): from node import Node
def generate_line(root):
""" """
Print an expression tree in a single text line. Where needed, add Print an expression tree in a single text line. Where needed, add
parentheses. parentheses.
>>> from node import Node, Leaf >>> from node import Leaf
>>> from graph import generate_graph
>>> l0, l1 = Leaf(1), Leaf(2) >>> l0, l1 = Leaf(1), Leaf(2)
>>> plus = Node('+', l0, l1) >>> plus = Node('+', l0, l1)
>>> print generate_line(plus, Node) >>> print generate_line(plus)
1 + 2 1 + 2
>>> plus2 = Node('+', l0, l1) >>> plus2 = Node('+', l0, l1)
>>> times = Node('*', plus, plus2) >>> times = Node('*', plus, plus2)
>>> print generate_line(times, Node) >>> print generate_line(times)
(1 + 2) * (1 + 2) (1 + 2) * (1 + 2)
>>> l2 = Leaf(3) >>> l2 = Leaf(3)
>>> uminus = Node('-', l2) >>> uminus = Node('-', l2)
>>> times = Node('*', plus, uminus) >>> times = Node('*', plus, uminus)
>>> print generate_line(times, Node) >>> print generate_line(times)
(1 + 2) * -3 (1 + 2) * -3
>>> exp = Leaf('x') >>> exp = Leaf('x')
>>> inf = Leaf('oo') >>> inf = Leaf('oo')
>>> minus_inf = Node('-', inf) >>> minus_inf = Node('-', inf)
>>> integral = Node('int', exp, minus_inf, inf) >>> integral = Node('int', exp, minus_inf, inf)
>>> print generate_line(integral, Node) >>> print generate_line(integral)
int(x, -oo, oo) int(x, -oo, oo)
""" """
...@@ -51,7 +52,7 @@ def generate_line(root, node_type): ...@@ -51,7 +52,7 @@ def generate_line(root, node_type):
""" """
Get the associativity of an operator node. Get the associativity of an operator node.
""" """
if isinstance(node, node_type) and len(node) > 1: if isinstance(node, Node) and len(node) > 1:
op = node.title() op = node.title()
for i, group in enumerate(operators): for i, group in enumerate(operators):
...@@ -68,7 +69,7 @@ def generate_line(root, node_type): ...@@ -68,7 +69,7 @@ def generate_line(root, node_type):
""" """
s = node.title() s = node.title()
if not isinstance(node, node_type): if not isinstance(node, Node):
return s return s
arity = len(node) arity = len(node)
......
...@@ -15,22 +15,22 @@ class TestLine(unittest.TestCase): ...@@ -15,22 +15,22 @@ class TestLine(unittest.TestCase):
def test_simple(self): def test_simple(self):
l0, l1 = Leaf(1), Leaf(2) l0, l1 = Leaf(1), Leaf(2)
plus = Node('+', l0, l1) plus = Node('+', l0, l1)
assert generate_line(plus, Node) == '1 + 2' assert generate_line(plus) == '1 + 2'
def test_parentheses(self): def test_parentheses(self):
l0, l1 = Leaf(1), Leaf(2) l0, l1 = Leaf(1), Leaf(2)
plus = Node('+', l0, l1) plus = Node('+', l0, l1)
times = Node('*', plus, plus) times = Node('*', plus, plus)
assert generate_line(times, Node) == '(1 + 2) * (1 + 2)' assert generate_line(times) == '(1 + 2) * (1 + 2)'
def test_function(self): def test_function(self):
exp = Leaf('x') exp = Leaf('x')
inf = Leaf('oo') inf = Leaf('oo')
minus_inf = Node('-', inf) minus_inf = Node('-', inf)
integral = Node('int', exp, minus_inf, inf) integral = Node('int', exp, minus_inf, inf)
assert generate_line(integral, Node) == 'int(x, -oo, oo)' assert generate_line(integral) == 'int(x, -oo, oo)'
def test_mod(self): def test_mod(self):
l0, l1 = Leaf(1), Leaf(2) l0, l1 = Leaf(1), Leaf(2)
mod = Node('mod', l1, l0) mod = Node('mod', l1, l0)
assert generate_line(mod, Node) == '2 mod 1' assert generate_line(mod) == '2 mod 1'
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment