Fixed 'is this node a leaf?' issues.

parent a509de43
# vim: set fileencoding=utf-8 : # vim: set fileencoding=utf-8 :
# XXX Used in doctests (we should use them in the __main__ section below too). # XXX Used in doctests (we should use them in the __main__ section below too).
from node import Leaf, Node
def generate_graph(root, separator=' ', verbose=False): def generate_graph(root, separator=' ', verbose=False):
""" """
Return a text-based, utf-8 graph of a tree-like structure. Each tree node Return a text-based, utf-8 graph of a tree-like structure. Each tree node
...@@ -10,6 +7,7 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -10,6 +7,7 @@ def generate_graph(root, separator=' ', verbose=False):
``title``, that attribute will be called. That way, the node can return a ``title``, that attribute will be called. That way, the node can return a
specific title, otherwise ``+`` is used. specific title, otherwise ``+`` is used.
>>> from node import Leaf, Node
>>> l0, l1 = Leaf(0), Leaf(1) >>> l0, l1 = Leaf(0), Leaf(1)
>>> n0 = Node('+', l0, l1) >>> n0 = Node('+', l0, l1)
>>> l2 = Leaf(2) >>> l2 = Leaf(2)
...@@ -42,7 +40,7 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -42,7 +40,7 @@ def generate_graph(root, separator=' ', verbose=False):
# Leaves do not have children and therefore the length of its title is # Leaves do not have children and therefore the length of its title is
# the width of the leaf. # the width of the leaf.
if isinstance(node, Leaf): if not node.nodes:
node_width[node] = title_len node_width[node] = title_len
node_middle[node] = int((title_len - 1) / 2) node_middle[node] = int((title_len - 1) / 2)
return title_len return title_len
...@@ -80,7 +78,7 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -80,7 +78,7 @@ def generate_graph(root, separator=' ', verbose=False):
return width return width
def format_lines(node): def format_lines(node):
if isinstance(node, Leaf): if not node.nodes:
# Leaf titles do not need to be centered, since the parent will # Leaf titles do not need to be centered, since the parent will
# center those lines. And if there are no parents, the entire graph # center those lines. And if there are no parents, the entire graph
# consists of a single leaf, so in that case there still is no # consists of a single leaf, so in that case there still is no
......
from node import Leaf
def generate_line(root): 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
...@@ -50,7 +48,7 @@ def generate_line(root): ...@@ -50,7 +48,7 @@ def generate_line(root):
""" """
Get the associativity of an operator node. Get the associativity of an operator node.
""" """
if not isinstance(node, Leaf) and len(node) > 1: if not node.nodes and len(node) > 1:
op = node.title() op = node.title()
for i, group in enumerate(operators): for i, group in enumerate(operators):
...@@ -65,9 +63,12 @@ def generate_line(root): ...@@ -65,9 +63,12 @@ def generate_line(root):
1. Visit the root 1. Visit the root
2. Traverse the subtrees in left-to-right order 2. Traverse the subtrees in left-to-right order
""" """
if not node:
return '<empty expression>'
s = node.title() s = node.title()
if isinstance(node, Leaf): if not node.nodes:
return s return s
arity = len(node) arity = len(node)
......
...@@ -170,8 +170,8 @@ class TestGraph(unittest.TestCase): ...@@ -170,8 +170,8 @@ class TestGraph(unittest.TestCase):
3 5 3 7 3 5 3 7
""") """)
def strip(self, str): def strip(self, s):
return str.replace('\n ', '\n')[1:-1] return s.replace('\n ', '\n')[1:-1]
def assertEqualGraphs(self, g, expect): def assertEqualGraphs(self, g, expect):
expect = self.strip(expect) expect = self.strip(expect)
......
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