Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
topydo
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
topydo
Commits
fd6d2c0d
Commit
fd6d2c0d
authored
Jul 20, 2014
by
Bram Schoenmakers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add dot output to the Graph class.
parent
2ce645df
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
0 deletions
+30
-0
Graph.py
Graph.py
+22
-0
test/GraphTest.py
test/GraphTest.py
+8
-0
No files found.
Graph.py
View file @
fd6d2c0d
...
...
@@ -181,3 +181,25 @@ class DirectedGraph(object):
for
edge
in
removals
:
self
.
remove_edge
(
edge
[
0
],
edge
[
1
])
def
dot
(
self
,
p_print_labels
=
True
):
""" Prints the graph in Dot format. """
out
=
'digraph g {
\
n
'
for
from_node
,
neighbors
in
self
.
_edges
.
iteritems
():
out
+=
" %s
\
n
"
%
from_node
for
neighbor
in
neighbors
:
out
+=
" %s -> %s"
%
(
from_node
,
neighbor
)
edge_id
=
self
.
edge_id
(
from_node
,
neighbor
)
if
edge_id
and
p_print_labels
:
out
+=
' [label="%s"]'
%
edge_id
out
+=
"
\
n
"
out
+=
'}
\
n
'
return
out
def
__str__
(
self
):
""" Returns the graph in Dot format. """
return
self
.
dot
()
test/GraphTest.py
View file @
fd6d2c0d
...
...
@@ -126,3 +126,11 @@ class GraphTest(unittest.TestCase):
# the one and only edge must be removed now
self
.
assertFalse
(
self
.
graph
.
has_edge
(
1
,
3
))
def
test_str_output
(
self
):
out
=
'digraph g {
\
n
1
\
n
1 -> 2 [label="1"]
\
n
1 -> 3
\
n
2
\
n
2 -> 4
\
n
3
\
n
3 -> 5
\
n
4
\
n
4 -> 3
\
n
4 -> 6
\
n
5
\
n
6
\
n
6 -> 2
\
n
}
\
n
'
self
.
assertEquals
(
str
(
self
.
graph
),
out
)
def
test_dot_output_without_labels
(
self
):
out
=
'digraph g {
\
n
1
\
n
1 -> 2
\
n
1 -> 3
\
n
2
\
n
2 -> 4
\
n
3
\
n
3 -> 5
\
n
4
\
n
4 -> 3
\
n
4 -> 6
\
n
5
\
n
6
\
n
6 -> 2
\
n
}
\
n
'
self
.
assertEquals
(
self
.
graph
.
dot
(
False
),
out
)
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