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
parentheses.
>>> from node import Node, Leaf
>>> from graph import generate_graph
>>> from node import Leaf
>>> l0, l1 = Leaf(1), Leaf(2)
>>> plus = Node('+', l0, l1)
>>> print generate_line(plus, Node)
>>> print generate_line(plus)
1 + 2
>>> plus2 = Node('+', l0, l1)
>>> times = Node('*', plus, plus2)
>>> print generate_line(times, Node)
>>> print generate_line(times)
(1 + 2) * (1 + 2)
>>> l2 = Leaf(3)
>>> uminus = Node('-', l2)
>>> times = Node('*', plus, uminus)
>>> print generate_line(times, Node)
>>> print generate_line(times)
(1 + 2) * -3
>>> exp = Leaf('x')
>>> inf = Leaf('oo')
>>> minus_inf = Node('-', inf)
>>> integral = Node('int', exp, minus_inf, inf)
>>> print generate_line(integral, Node)
>>> print generate_line(integral)
int(x, -oo, oo)
"""
......@@ -51,7 +52,7 @@ def generate_line(root, node_type):
"""
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()
for i, group in enumerate(operators):
......@@ -68,7 +69,7 @@ def generate_line(root, node_type):
"""
s = node.title()
if not isinstance(node, node_type):
if not isinstance(node, Node):
return s
arity = len(node)
......
......@@ -15,22 +15,22 @@ class TestLine(unittest.TestCase):
def test_simple(self):
l0, l1 = Leaf(1), Leaf(2)
plus = Node('+', l0, l1)
assert generate_line(plus, Node) == '1 + 2'
assert generate_line(plus) == '1 + 2'
def test_parentheses(self):
l0, l1 = Leaf(1), Leaf(2)
plus = Node('+', l0, l1)
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):
exp = Leaf('x')
inf = Leaf('oo')
minus_inf = Node('-', 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):
l0, l1 = Leaf(1), Leaf(2)
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