Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
graph_drawing
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
graph_drawing
Commits
a34e4894
Commit
a34e4894
authored
Dec 09, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved graph unicode signs outside the function and applied pep8.
parent
ec0a7e59
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
29 deletions
+31
-29
graph.py
graph.py
+30
-29
line.py
line.py
+1
-0
No files found.
graph.py
View file @
a34e4894
# 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
...
...
line.py
View file @
a34e4894
from
node
import
Leaf
def
generate_line
(
root
):
"""
Print an expression tree in a single text line. Where needed, add
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment