Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
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
trs
Commits
2a400ac2
Commit
2a400ac2
authored
Jan 26, 2012
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use is_leaf attribute instead of calling a method.
parent
1556572e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
20 additions
and
23 deletions
+20
-23
external/graph_drawing
external/graph_drawing
+1
-1
src/node.py
src/node.py
+14
-17
src/rules/factors.py
src/rules/factors.py
+1
-1
src/rules/negation.py
src/rules/negation.py
+1
-1
src/rules/numerics.py
src/rules/numerics.py
+1
-1
tests/test_node.py
tests/test_node.py
+2
-2
No files found.
graph_drawing
@
2b6168ce
Subproject commit
15abdd61c35310eb38af940593b8c65b1b9f790d
Subproject commit
2b6168ce320e21e7ef614f22e606d1144603af8c
src/node.py
View file @
2a400ac2
...
@@ -83,23 +83,23 @@ class ExpressionBase(object):
...
@@ -83,23 +83,23 @@ class ExpressionBase(object):
exponent property, or this node's coefficient property is less than
exponent property, or this node's coefficient property is less than
other's coefficient property. Otherwise, False is returned.
other's coefficient property. Otherwise, False is returned.
"""
"""
if
self
.
is_leaf
()
:
if
self
.
is_leaf
:
if
other
.
is_leaf
()
:
if
other
.
is_leaf
:
# Both are leafs, string compare the value.
# Both are leafs, string compare the value.
return
str
(
self
.
value
)
<
str
(
other
.
value
)
return
str
(
self
.
value
)
<
str
(
other
.
value
)
# Self is a leaf, thus has less value than an expression node.
# Self is a leaf, thus has less value than an expression node.
return
True
return
True
if
self
.
is_op
(
OP_NEG
)
and
self
[
0
].
is_leaf
()
:
if
self
.
is_op
(
OP_NEG
)
and
self
[
0
].
is_leaf
:
if
other
.
is_leaf
()
:
if
other
.
is_leaf
:
# Both are leafs, string compare the value.
# Both are leafs, string compare the value.
return
(
'-'
+
str
(
self
.
value
))
<
str
(
other
.
value
)
return
(
'-'
+
str
(
self
.
value
))
<
str
(
other
.
value
)
if
other
.
is_op
(
OP_NEG
)
and
other
[
0
].
is_leaf
()
:
if
other
.
is_op
(
OP_NEG
)
and
other
[
0
].
is_leaf
:
return
(
'-'
+
str
(
self
.
value
))
<
(
'-'
+
str
(
other
.
value
))
return
(
'-'
+
str
(
self
.
value
))
<
(
'-'
+
str
(
other
.
value
))
# Self is a leaf, thus has less value than an expression node.
# Self is a leaf, thus has less value than an expression node.
return
True
return
True
if
other
.
is_leaf
()
:
if
other
.
is_leaf
:
# Self is an expression node, and the other is a leaf. Thus, other
# Self is an expression node, and the other is a leaf. Thus, other
# is greater than self.
# is greater than self.
return
False
return
False
...
@@ -111,10 +111,10 @@ class ExpressionBase(object):
...
@@ -111,10 +111,10 @@ class ExpressionBase(object):
return
s_root
<
o_root
or
s_exp
<
o_exp
or
s_coeff
<
o_coeff
return
s_root
<
o_root
or
s_exp
<
o_exp
or
s_coeff
<
o_coeff
def
is_op
(
self
,
op
):
def
is_op
(
self
,
op
):
return
not
self
.
is_leaf
()
and
self
.
op
==
op
return
not
self
.
is_leaf
and
self
.
op
==
op
def
is_op_or_negated
(
self
,
op
):
def
is_op_or_negated
(
self
,
op
):
if
self
.
is_leaf
()
:
if
self
.
is_leaf
:
return
False
return
False
if
self
.
op
==
OP_NEG
:
if
self
.
op
==
OP_NEG
:
...
@@ -122,23 +122,20 @@ class ExpressionBase(object):
...
@@ -122,23 +122,20 @@ class ExpressionBase(object):
return
self
.
op
==
op
return
self
.
op
==
op
def
is_leaf
(
self
):
return
self
.
type
!=
TYPE_OPERATOR
def
is_leaf_or_negated
(
self
):
def
is_leaf_or_negated
(
self
):
if
self
.
is_leaf
()
:
if
self
.
is_leaf
:
return
True
return
True
if
self
.
is_op
(
OP_NEG
):
if
self
.
is_op
(
OP_NEG
):
return
self
[
0
].
is_leaf
()
return
self
[
0
].
is_leaf
return
False
return
False
def
is_power
(
self
):
def
is_power
(
self
):
return
not
self
.
is_leaf
()
and
self
.
op
==
OP_POW
return
not
self
.
is_leaf
and
self
.
op
==
OP_POW
def
is_nary
(
self
):
def
is_nary
(
self
):
return
not
self
.
is_leaf
()
and
self
.
op
in
[
OP_ADD
,
OP_SUB
,
OP_MUL
]
return
not
self
.
is_leaf
and
self
.
op
in
[
OP_ADD
,
OP_SUB
,
OP_MUL
]
def
is_identifier
(
self
):
def
is_identifier
(
self
):
return
self
.
type
==
TYPE_IDENTIFIER
return
self
.
type
==
TYPE_IDENTIFIER
...
@@ -362,13 +359,13 @@ class Scope(object):
...
@@ -362,13 +359,13 @@ class Scope(object):
return
iter
(
self
.
nodes
)
return
iter
(
self
.
nodes
)
def
remove
(
self
,
node
,
replacement
=
None
):
def
remove
(
self
,
node
,
replacement
=
None
):
if
node
.
is_leaf
()
:
if
node
.
is_leaf
:
node_cmp
=
hash
(
node
)
node_cmp
=
hash
(
node
)
else
:
else
:
node_cmp
=
node
node_cmp
=
node
for
i
,
n
in
enumerate
(
self
.
nodes
):
for
i
,
n
in
enumerate
(
self
.
nodes
):
if
n
.
is_leaf
()
:
if
n
.
is_leaf
:
n_cmp
=
hash
(
n
)
n_cmp
=
hash
(
n
)
else
:
else
:
n_cmp
=
n
n_cmp
=
n
...
...
src/rules/factors.py
View file @
2a400ac2
...
@@ -18,7 +18,7 @@ def match_expand(node):
...
@@ -18,7 +18,7 @@ def match_expand(node):
additions
=
[]
additions
=
[]
for
n
in
Scope
(
node
):
for
n
in
Scope
(
node
):
if
n
.
is_leaf
()
or
n
.
is_op
(
OP_NEG
)
and
n
[
0
].
is_leaf
()
:
if
n
.
is_leaf
or
n
.
is_op
(
OP_NEG
)
and
n
[
0
].
is_leaf
:
leaves
.
append
(
n
)
leaves
.
append
(
n
)
elif
n
.
op
==
OP_ADD
:
elif
n
.
op
==
OP_ADD
:
additions
.
append
(
n
)
additions
.
append
(
n
)
...
...
src/rules/negation.py
View file @
2a400ac2
...
@@ -18,7 +18,7 @@ def match_negate_group(node):
...
@@ -18,7 +18,7 @@ def match_negate_group(node):
# --a
# --a
return
[
P
(
node
,
double_negation
,
(
node
,))]
return
[
P
(
node
,
double_negation
,
(
node
,))]
if
not
val
.
is_leaf
()
:
if
not
val
.
is_leaf
:
scope
=
get_scope
(
val
)
scope
=
get_scope
(
val
)
if
not
any
(
map
(
lambda
n
:
n
.
is_op
(
OP_NEG
),
scope
)):
if
not
any
(
map
(
lambda
n
:
n
.
is_op
(
OP_NEG
),
scope
)):
...
...
src/rules/numerics.py
View file @
2a400ac2
...
@@ -119,7 +119,7 @@ def match_multiply_zero(node):
...
@@ -119,7 +119,7 @@ def match_multiply_zero(node):
assert
node
.
is_op
(
OP_MUL
)
assert
node
.
is_op
(
OP_MUL
)
left
,
right
=
node
left
,
right
=
node
is_zero
=
lambda
n
:
n
.
is_leaf
()
and
n
.
value
==
0
is_zero
=
lambda
n
:
n
.
is_leaf
and
n
.
value
==
0
if
is_zero
(
left
):
if
is_zero
(
left
):
negated
=
right
.
is_op
(
OP_NEG
)
negated
=
right
.
is_op
(
OP_NEG
)
...
...
tests/test_node.py
View file @
2a400ac2
...
@@ -37,8 +37,8 @@ class TestNode(RulesTestCase):
...
@@ -37,8 +37,8 @@ class TestNode(RulesTestCase):
self
.
assertFalse
(
self
.
l
[
0
].
is_op_or_negated
(
OP_ADD
))
self
.
assertFalse
(
self
.
l
[
0
].
is_op_or_negated
(
OP_ADD
))
def
test_is_leaf
(
self
):
def
test_is_leaf
(
self
):
self
.
assertTrue
(
L
(
2
).
is_leaf
()
)
self
.
assertTrue
(
L
(
2
).
is_leaf
)
self
.
assertFalse
(
N
(
'+'
,
*
self
.
l
[:
2
]).
is_leaf
()
)
self
.
assertFalse
(
N
(
'+'
,
*
self
.
l
[:
2
]).
is_leaf
)
def
test_is_leaf_or_negated
(
self
):
def
test_is_leaf_or_negated
(
self
):
self
.
assertTrue
(
L
(
2
).
is_leaf_or_negated
())
self
.
assertTrue
(
L
(
2
).
is_leaf_or_negated
())
...
...
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