From 0f3e0870fe415b34515a743db6d2f5fa4acd0334 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 21 Jan 2018 12:10:45 +0300 Subject: [PATCH] apply_on_last_branches is ok --- mapytex/calculus/core/tree.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/mapytex/calculus/core/tree.py b/mapytex/calculus/core/tree.py index 20be43b..79c81fb 100644 --- a/mapytex/calculus/core/tree.py +++ b/mapytex/calculus/core/tree.py @@ -226,17 +226,39 @@ class Tree(object): :param function: (op, a, a) -> b :returns: b if it is a 1 level Tree, Tree otherwise + + :example: + + >>> nested_par = ("+", ( + ... ("*", (3, 4)), + ... 2)) + >>> t = Tree.from_nested_parenthesis(nested_par) + >>> print(t) + + + > * + | > 3 + | > 4 + > 2 + >>> from .tree_tools import infix_str_concatenate + >>> tt = t.apply_on_last_branches(lambda *x: infix_str_concatenate(*x)) + >>> print(tt) + + + > 3 * 4 + > 2 + """ + left_is_leaf = 0 + right_is_leaf = 0 try: - # TODO: to change when typify is done |dim. févr. 19 14:49:06 EAT 2017 left_applied = self.left_value.apply_on_last_branches(function) except AttributeError: + left_applied = self.left_value left_is_leaf = 1 try: - # TODO: to change when typify is done |dim. févr. 19 14:49:06 EAT 2017 - left_applied = self.left_value.apply_on_last_branches(function) + right_applied = self.right_value.apply_on_last_branches(function) except AttributeError: + right_applied = self.right_value right_is_leaf = 1 if left_is_leaf and right_is_leaf: