Moved graph unicode signs outside the function and applied pep8.

parent ec0a7e59
# 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).
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): 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
...@@ -112,49 +122,39 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -112,49 +122,39 @@ def generate_graph(root, separator=' ', verbose=False):
box_widths = [len(lines[0]) for lines in child_lines] box_widths = [len(lines[0]) for lines in child_lines]
node_len = len(node) node_len = len(node)
middle_node = int(node_len / 2) 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] middle = node_middle[node]
title_line = center_text(node.title(), line_width, middle) 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: if node_len == 1:
# Unary operators # 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: elif node_len % 2:
# n-ary operators (n is odd) # n-ary operators (n is odd)
edge_line = '' edge_line = ''
for i, child in enumerate(node): for i, child in enumerate(node):
if i > 0: if i > 0:
edge_line += dash_sign edge_line += DASH_SIGN
if i < middle_node: 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], edge_line += center_text(marker, box_widths[i],
middle=0, right=dash_sign) middle=0, right=DASH_SIGN)
else: else:
if i == middle_node: if i == middle_node:
marker = cross_sign marker = CROSS_SIGN
edge_line += center_text(marker, box_widths[i], edge_line += center_text(marker, box_widths[i],
middle=0, right=dash_sign, middle=0, right=DASH_SIGN,
left=dash_sign) left=DASH_SIGN)
else: else:
if i == node_len - 1: if i == node_len - 1:
marker = right_sign marker = RIGHT_SIGN
else: else:
marker = tsplit_dn_sign marker = TSPLIT_DN_SIGN
edge_line += center_text(marker, box_widths[i], edge_line += center_text(marker, box_widths[i],
middle=0, left=dash_sign) middle=0, left=DASH_SIGN)
else: else:
# n-ary operators (n is even) # n-ary operators (n is even)
edge_line = '' edge_line = ''
...@@ -162,24 +162,24 @@ def generate_graph(root, separator=' ', verbose=False): ...@@ -162,24 +162,24 @@ def generate_graph(root, separator=' ', verbose=False):
for i, child in enumerate(node): for i, child in enumerate(node):
if i > 0: if i > 0:
if i == middle_node: if i == middle_node:
edge_line += tsplit_up_sign edge_line += TSPLIT_UP_SIGN
else: else:
edge_line += dash_sign edge_line += DASH_SIGN
if i < middle_node: 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], edge_line += center_text(marker, box_widths[i],
middle=node_middle[child], middle=node_middle[child],
right=dash_sign) right=DASH_SIGN)
else: else:
if i == node_len - 1: if i == node_len - 1:
marker = right_sign marker = RIGHT_SIGN
else: else:
marker = tsplit_dn_sign marker = TSPLIT_DN_SIGN
edge_line += center_text(marker, box_widths[i], edge_line += center_text(marker, box_widths[i],
middle=node_middle[child], middle=node_middle[child],
left=dash_sign) left=DASH_SIGN)
try: try:
assert len(title_line) == len(edge_line) assert len(title_line) == len(edge_line)
...@@ -215,10 +215,11 @@ def center_text(text, width, middle=0, left=' ', right=' '): ...@@ -215,10 +215,11 @@ def center_text(text, width, middle=0, left=' ', right=' '):
>>> center_text('+', 15, 11) >>> 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 >>> len(left) == 11
True 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 >>> len(right) == 3
True True
>>> edge_line = left + '┴'.decode('utf-8') + right >>> edge_line = left + '┴'.decode('utf-8') + right
......
from node import Leaf 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
......
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