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
45bee0e4
Commit
45bee0e4
authored
Dec 01, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed 'is this node a leaf?' issues.
parent
a509de43
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
10 additions
and
11 deletions
+10
-11
graph.py
graph.py
+3
-5
line.py
line.py
+5
-4
tests/test_graph.py
tests/test_graph.py
+2
-2
No files found.
graph.py
View file @
45bee0e4
# 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
...
...
line.py
View file @
45bee0e4
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
)
...
...
tests/test_graph.py
View file @
45bee0e4
...
@@ -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
,
s
tr
):
def
strip
(
self
,
s
):
return
s
tr
.
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
)
...
...
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