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
608dc099
Commit
608dc099
authored
Dec 01, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed node type from graph-to-line coverter.
parent
2f60cbbc
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
13 deletions
+14
-13
line.py
line.py
+10
-9
tests/test_line.py
tests/test_line.py
+4
-4
No files found.
line.py
View file @
608dc099
def
generate_line
(
root
,
node_type
):
from
node
import
Node
def
generate_line
(
root
):
"""
Print an expression tree in a single text line. Where needed, add
parentheses.
>>> from node import Node, Leaf
>>> from graph import generate_graph
>>> from node import Leaf
>>> l0, l1 = Leaf(1), Leaf(2)
>>> plus = Node('+', l0, l1)
>>> print generate_line(plus
, Node
)
>>> print generate_line(plus)
1 + 2
>>> plus2 = Node('+', l0, l1)
>>> times = Node('*', plus, plus2)
>>> print generate_line(times
, Node
)
>>> print generate_line(times)
(1 + 2) * (1 + 2)
>>> l2 = Leaf(3)
>>> uminus = Node('-', l2)
>>> times = Node('*', plus, uminus)
>>> print generate_line(times
, Node
)
>>> print generate_line(times)
(1 + 2) * -3
>>> exp = Leaf('x')
>>> inf = Leaf('oo')
>>> minus_inf = Node('-', inf)
>>> integral = Node('int', exp, minus_inf, inf)
>>> print generate_line(integral
, Node
)
>>> print generate_line(integral)
int(x, -oo, oo)
"""
...
...
@@ -51,7 +52,7 @@ def generate_line(root, node_type):
"""
Get the associativity of an operator node.
"""
if
isinstance
(
node
,
node_typ
e
)
and
len
(
node
)
>
1
:
if
isinstance
(
node
,
Nod
e
)
and
len
(
node
)
>
1
:
op
=
node
.
title
()
for
i
,
group
in
enumerate
(
operators
):
...
...
@@ -68,7 +69,7 @@ def generate_line(root, node_type):
"""
s
=
node
.
title
()
if
not
isinstance
(
node
,
node_typ
e
):
if
not
isinstance
(
node
,
Nod
e
):
return
s
arity
=
len
(
node
)
...
...
tests/test_line.py
View file @
608dc099
...
...
@@ -15,22 +15,22 @@ class TestLine(unittest.TestCase):
def
test_simple
(
self
):
l0
,
l1
=
Leaf
(
1
),
Leaf
(
2
)
plus
=
Node
(
'+'
,
l0
,
l1
)
assert
generate_line
(
plus
,
Node
)
==
'1 + 2'
assert
generate_line
(
plus
)
==
'1 + 2'
def
test_parentheses
(
self
):
l0
,
l1
=
Leaf
(
1
),
Leaf
(
2
)
plus
=
Node
(
'+'
,
l0
,
l1
)
times
=
Node
(
'*'
,
plus
,
plus
)
assert
generate_line
(
times
,
Node
)
==
'(1 + 2) * (1 + 2)'
assert
generate_line
(
times
)
==
'(1 + 2) * (1 + 2)'
def
test_function
(
self
):
exp
=
Leaf
(
'x'
)
inf
=
Leaf
(
'oo'
)
minus_inf
=
Node
(
'-'
,
inf
)
integral
=
Node
(
'int'
,
exp
,
minus_inf
,
inf
)
assert
generate_line
(
integral
,
Node
)
==
'int(x, -oo, oo)'
assert
generate_line
(
integral
)
==
'int(x, -oo, oo)'
def
test_mod
(
self
):
l0
,
l1
=
Leaf
(
1
),
Leaf
(
2
)
mod
=
Node
(
'mod'
,
l1
,
l0
)
assert
generate_line
(
mod
,
Node
)
==
'2 mod 1'
assert
generate_line
(
mod
)
==
'2 mod 1'
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