Moved graph unicode signs outside the function and applied pep8.

parent ec0a7e59
# vim: set fileencoding=utf-8 :
# XXX Used in doctests (we should use them in the __main__ section below too).
PIPE_SIGN = '│'.decode('utf-8')
DASH_SIGN = '─'.decode('utf-8')
CROSS_SIGN = '┼'.decode('utf-8')
TSPLIT_DN_SIGN = '┬'.decode('utf-8')
TSPLIT_UP_SIGN = '┴'.decode('utf-8')
LEFT_SIGN = '╭'.decode('utf-8')
RIGHT_SIGN = '╮'.decode('utf-8')
def generate_graph(root, separator=' ', verbose=False):
"""
Return a text-based, utf-8 graph of a tree-like structure. Each tree node
......@@ -112,49 +122,39 @@ def generate_graph(root, separator=' ', verbose=False):
box_widths = [len(lines[0]) for lines in child_lines]
node_len = len(node)
middle_node = int(node_len / 2)
#middle = sum([box_widths[i] for i in range(middle_node)]) \
# + max(middle_node - int(node_len % 2 == 0), 0) * separator_len
middle = node_middle[node]
title_line = center_text(node.title(), line_width, middle)
pipe_sign = '│'.decode('utf-8')
dash_sign = '─'.decode('utf-8')
cross_sign = '┼'.decode('utf-8')
tsplit_dn_sign = '┬'.decode('utf-8')
tsplit_up_sign = '┴'.decode('utf-8')
left_sign = '╭'.decode('utf-8')
right_sign = '╮'.decode('utf-8')
if node_len == 1:
# Unary operators
edge_line = center_text(pipe_sign, box_widths[0], middle)
edge_line = center_text(PIPE_SIGN, box_widths[0], middle)
elif node_len % 2:
# n-ary operators (n is odd)
edge_line = ''
for i, child in enumerate(node):
if i > 0:
edge_line += dash_sign
edge_line += DASH_SIGN
if i < middle_node:
marker = (left_sign if i == 0 else tsplit_dn_sign)
marker = (LEFT_SIGN if i == 0 else TSPLIT_DN_SIGN)
edge_line += center_text(marker, box_widths[i],
middle=0, right=dash_sign)
middle=0, right=DASH_SIGN)
else:
if i == middle_node:
marker = cross_sign
marker = CROSS_SIGN
edge_line += center_text(marker, box_widths[i],
middle=0, right=dash_sign,
left=dash_sign)
middle=0, right=DASH_SIGN,
left=DASH_SIGN)
else:
if i == node_len - 1:
marker = right_sign
marker = RIGHT_SIGN
else:
marker = tsplit_dn_sign
marker = TSPLIT_DN_SIGN
edge_line += center_text(marker, box_widths[i],
middle=0, left=dash_sign)
middle=0, left=DASH_SIGN)
else:
# n-ary operators (n is even)
edge_line = ''
......@@ -162,24 +162,24 @@ def generate_graph(root, separator=' ', verbose=False):
for i, child in enumerate(node):
if i > 0:
if i == middle_node:
edge_line += tsplit_up_sign
edge_line += TSPLIT_UP_SIGN
else:
edge_line += dash_sign
edge_line += DASH_SIGN
if i < middle_node:
marker = (left_sign if i == 0 else tsplit_dn_sign)
marker = (LEFT_SIGN if i == 0 else TSPLIT_DN_SIGN)
edge_line += center_text(marker, box_widths[i],
middle=node_middle[child],
right=dash_sign)
right=DASH_SIGN)
else:
if i == node_len - 1:
marker = right_sign
marker = RIGHT_SIGN
else:
marker = tsplit_dn_sign
marker = TSPLIT_DN_SIGN
edge_line += center_text(marker, box_widths[i],
middle=node_middle[child],
left=dash_sign)
left=DASH_SIGN)
try:
assert len(title_line) == len(edge_line)
......@@ -215,10 +215,11 @@ def center_text(text, width, middle=0, left=' ', right=' '):
>>> center_text('+', 15, 11)
' + '
>>> left = center_text('╭'.decode('utf-8'), 11, 8, right='─'.decode('utf-8'))
>>> sep = '─'.decode('utf-8')
>>> left = center_text('╭'.decode('utf-8'), 11, 8, right=sep)
>>> len(left) == 11
True
>>> right = center_text('╮'.decode('utf-8'), 3, 2, left='─'.decode('utf-8'))
>>> right = center_text('╮'.decode('utf-8'), 3, 2, left=sep)
>>> len(right) == 3
True
>>> edge_line = left + '┴'.decode('utf-8') + right
......
from node import Leaf
def generate_line(root):
"""
Print an expression tree in a single text line. Where needed, add
......
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