From f53aa26542b91fc15ce2ce3f8f420639603414a5 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Wed, 21 Nov 2018 08:54:37 +0100 Subject: [PATCH] Refact(API): Split simplify into _optimize, _typing, _compute --- mapytex/calculus/API/expression.py | 98 +++++++++++++++++++++++------- 1 file changed, 75 insertions(+), 23 deletions(-) diff --git a/mapytex/calculus/API/expression.py b/mapytex/calculus/API/expression.py index 5112fa7..bb16279 100644 --- a/mapytex/calculus/API/expression.py +++ b/mapytex/calculus/API/expression.py @@ -88,6 +88,73 @@ class Expression(object): def __repr__(self): return f"" + def _optimize(self, exclude_nodes=["\\", "**"]): + """ Return a copy of self with an optimize tree + + """ + try: + # TODO: need to test exclude_nodes |ven. oct. 5 08:51:02 CEST 2018 + return Expression(self._tree.balance(exclude_nodes=exclude_nodes)) + except AttributeError: + return self + + def _typing(self): + """ Build a copy of self with as much typing as possible + + :example: + >>> e = Expression.from_str("2x") + >>> print(e._tree.map_on_leaf(type)) + * + > + > + >>> typed_e = e._typing() + >>> print(type(typed_e._tree)) + + >>> typed_e = e._typing() + >>> print(type(typed_e._tree)) + + + >>> e = Expression.from_str("2x+3+4/5") + >>> print(e._tree.map_on_leaf(type)) + + + > + + | > * + | | > + | | > + | > + > / + | > + | > + >>> typed_e = e._typing() + >>> print(typed_e._tree.map_on_leaf(type)) + + + > + > + """ + try: + t = self._tree.apply_on_last_level(typing) + except TypingError: + return self + except NotImplementedError: + return self + except AttributeError: + return self + else: + return Expression(t)._typing() + + def _compute(self): + """" Compute one step of self + + """ + try: + return Expression(self._tree.apply_on_last_level(compute)) + except AttributeError: + return self + + def set_ancestor(self, ancestor): + """ Set ancestor """ + self._ancestor = ancestor + def simplify(self, optimize=True): """ Compute as much as possible the expression @@ -104,34 +171,19 @@ class Expression(object): >>> f._ancestor """ - t = self._tree if optimize: - try: - # TODO: need to test exclude_nodes |ven. oct. 5 08:51:02 CEST 2018 - t = t.balance( - exclude_nodes=["\\", "**"] - ) - except AttributeError: - pass + opt_exp = self._optimize() + else: + opt_exp = self - more_typing = 1 - while more_typing: - try: - t = t.apply_on_last_level(typing) - except TypingError: - more_typing = 0 - except NotImplementedError: - more_typing = 0 - except AttributeError: - more_typing = 0 + typed_exp = opt_exp._typing() + comp_exp = typed_exp._compute() - try: - t = t.apply_on_last_level(compute) - except AttributeError: + if typed_exp == comp_exp: return self else: - e = Expression(t, ancestor=self) - return e.simplify(optimize=optimize) + comp_exp.set_ancestor(self) + return comp_exp.simplify(optimize=optimize) def explain(self): """ Yield every calculus step which have lead to self