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 from .expression import Expression
if __name__ == "__main__": if __name__ == "__main__":
e = Expression.from_str("2x^2+2x+3x") e = Expression.from_str("2+3/4")
print(e._tree.map_on_leaf(lambda x:(x,))) e_simplified = e.simplify()
for _ in range(5): print(e_simplified)
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)
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

View File

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

View File

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

View File

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