Refect(Core): Improve error catch in apply and apply_on_last_level

This commit is contained in:
Bertrand Benjamin 2018-11-21 15:54:07 +01:00
parent fe52d9b346
commit 8933359945
4 changed files with 23 additions and 38 deletions

View File

@ -100,26 +100,9 @@ x^7
from .expression import Expression
if __name__ == "__main__":
e = Expression.from_str("2x^2+2x+3x")
print(e._tree.map_on_leaf(lambda x:(x,)))
for _ in range(5):
print("-"*10)
e = e._optimize()
try:
print(e._tree.map_on_leaf(lambda x:(x,)))
except AttributeError:
print(e)
e = e._typing()
try:
print(e._tree.map_on_leaf(lambda x:(x,)))
except AttributeError:
print(e)
e = e._compute()
try:
print(e._tree.map_on_leaf(lambda x:(x,)))
except AttributeError:
print(e)
e = Expression.from_str("2+3/4")
e_simplified = e.simplify()
print(e_simplified)
# -----------------------------
# Reglages pour 'vim'

View File

@ -163,11 +163,9 @@ class Expression(object):
> <class 'mapytex.calculus.core.MO.fraction.MOFraction'>
"""
try:
t = self._tree.apply_on_last_level(typing)
except (TypingError, NotImplementedError, AttributeError):
return Expression(self._tree.apply(typing))
except AttributeError:
return self
else:
return Expression(t)._typing()
def _compute(self):
"""" Compute one step of self

View File

@ -330,8 +330,6 @@ class Tree(object):
try:
left_applied = self.left_value.\
apply_on_last_level(function)
except NotImplementedError:
left_applied = self.left_value
except AttributeError:
left_applied = self.left_value
left_is_leaf = 1
@ -340,14 +338,15 @@ class Tree(object):
try:
right_applied = self.right_value.\
apply_on_last_level(function)
except NotImplementedError:
right_applied = self.right_value
except AttributeError:
right_applied = self.right_value
right_is_leaf = 1
if left_is_leaf and right_is_leaf:
return function(self.node, left_applied, right_applied)
try:
return function(self.node, left_applied, right_applied)
except NotImplementedError:
return Tree(self.node, left_applied, right_applied)
else:
return Tree(self.node, left_applied, right_applied)
@ -389,7 +388,10 @@ class Tree(object):
except AttributeError:
right_value = self.right_value
return function(self.node, left_value, right_value)
try:
return function(self.node, left_value, right_value)
except NotImplementedError:
return Tree(self.node, left_value, right_value)
def get_leafs(self, callback=lambda x:x):
""" Generator which yield all the leaf value of the tree.
@ -843,7 +845,7 @@ class Tree(object):
self.left_value,
self.right_value.short_branch)
return Tree(self.node, new_left, new_right).balance(exclude_nodes)
try:
left_v = self.left_value.balance(exclude_nodes)
except AttributeError:

View File

@ -7,7 +7,7 @@
# Distributed under terms of the MIT license.
"""
Computing with MO
Typing with MO
"""
from .exceptions import TypingError
@ -35,7 +35,8 @@ OPERATIONS = {
"^": power,
}
def typing(node, left_v, right_v):
def typing(node, left_v, right_v,\
raiseTypingError = True):
"""
Typing a try base on his root node
@ -46,11 +47,12 @@ def typing(node, left_v, right_v):
try:
operation = OPERATIONS[node]
except KeyError:
raise TypingError(f"Unknown operation ({node}) in typing")
try:
return operation(left_v, right_v)
except NotImplementedError:
raise TypingError(f"Can't create new MO with {node}, {type(left_v)} and {type(right_v)}")
raise NotImplementedError(f"Unknown operation ({node}) in typing")
return operation(left_v, right_v)
# try:
# return operation(left_v, right_v)
# except NotImplementedError:
# raise TypingError(f"Can't create new MO with {node}, {type(left_v)} and {type(right_v)}")
def typing_capacities(node):
""" Test an operation through all MOs