Use is_leaf attribute instead of calling a method.

parent 1556572e
graph_drawing @ 2b6168ce
Subproject commit 15abdd61c35310eb38af940593b8c65b1b9f790d Subproject commit 2b6168ce320e21e7ef614f22e606d1144603af8c
...@@ -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
......
...@@ -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)
......
...@@ -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)):
......
...@@ -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)
......
...@@ -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())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment