From 89333599459b2a9544327d365423061e4d9610dd Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 21 Nov 2018 15:54:07 +0100 Subject: [PATCH] Refect(Core): Improve error catch in apply and apply_on_last_level --- mapytex/calculus/API/__init__.py | 23 +++-------------------- mapytex/calculus/API/expression.py | 6 ++---- mapytex/calculus/core/tree.py | 16 +++++++++------- mapytex/calculus/core/typing/__init__.py | 16 +++++++++------- 4 files changed, 23 insertions(+), 38 deletions(-) diff --git a/mapytex/calculus/API/__init__.py b/mapytex/calculus/API/__init__.py index 837d669..8d74ae3 100644 --- a/mapytex/calculus/API/__init__.py +++ b/mapytex/calculus/API/__init__.py @@ -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' diff --git a/mapytex/calculus/API/expression.py b/mapytex/calculus/API/expression.py index b818cec..660ec91 100644 --- a/mapytex/calculus/API/expression.py +++ b/mapytex/calculus/API/expression.py @@ -163,11 +163,9 @@ class Expression(object): > """ 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 diff --git a/mapytex/calculus/core/tree.py b/mapytex/calculus/core/tree.py index a6157eb..e00d11d 100644 --- a/mapytex/calculus/core/tree.py +++ b/mapytex/calculus/core/tree.py @@ -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: diff --git a/mapytex/calculus/core/typing/__init__.py b/mapytex/calculus/core/typing/__init__.py index 32d6fb5..638d671 100644 --- a/mapytex/calculus/core/typing/__init__.py +++ b/mapytex/calculus/core/typing/__init__.py @@ -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