Refect(Core): Improve error catch in apply and apply_on_last_level
This commit is contained in:
parent
fe52d9b346
commit
8933359945
@ -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'
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
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
|
||||
|
||||
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.
|
||||
|
@ -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:
|
||||
raise NotImplementedError(f"Unknown operation ({node}) in typing")
|
||||
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)}")
|
||||
# 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
|
||||
|
Loading…
Reference in New Issue
Block a user