From 3e258b2d41609fc1bf5a5b25277d27398d491f36 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Tue, 16 Jul 2019 09:30:30 +0200 Subject: [PATCH] Feat: Expression call works with tokens --- mapytex/calculus/API/expression.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/mapytex/calculus/API/expression.py b/mapytex/calculus/API/expression.py index 196ae0e..b876ff3 100644 --- a/mapytex/calculus/API/expression.py +++ b/mapytex/calculus/API/expression.py @@ -442,6 +442,9 @@ class Expression(object): def __call__(self, value): """ Call a Expression to evaluate itself on value + :param value: evaluate the Expression with this value + :return: Expression simplified if the value is not a string with a length greater than 1. + :examples: >>> f = Expression.from_str("3*x^2 + 2x + 1") >>> for s in f(2).explain(): @@ -450,14 +453,27 @@ class Expression(object): 3 * 4 + 4 + 1 12 + 5 17 + >>> f(f(2)) + + >>> f(17) + + >>> f("n") + + >>> f("u_n") + """ tree = self._tree variable = (set(tree.get_leafs(extract_variable)) - {None}).pop() - dest = moify(value) + try: + dest = value._mo + except AttributeError: + dest = moify(value) replace_var = partial(replace, origin=variable, dest=dest) tree = tree.map_on_leaf(replace_var) + if isinstance(value, str) and len(value) > 1: + return Expression(tree) return Expression(tree).simplify()