From 8b201fa8d05a579429fa5542204d8aa1b72ed757 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 29 Jan 2018 07:51:54 +0300 Subject: [PATCH] Throw basis of LeafTree --- mapytex/calculus/core/tree.py | 99 ++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 42 deletions(-) diff --git a/mapytex/calculus/core/tree.py b/mapytex/calculus/core/tree.py index c1a57cd..c94634a 100644 --- a/mapytex/calculus/core/tree.py +++ b/mapytex/calculus/core/tree.py @@ -311,48 +311,6 @@ class Tree(object): return function(self.node, left_value, right_value) - def to_nested_parenthesis(self): - """ Transform the Tree into its nested parenthesis description - :example: - - >>> nested_par = ("+", (1, 2)) - >>> t = Tree.from_nested_parenthesis(nested_par) - >>> t.to_nested_parenthesis() - ('+', (1, 2)) - >>> nested_par = ("+", ( - ... ("*", (3, 4)), - ... 2)) - >>> t = Tree.from_nested_parenthesis(nested_par) - >>> t.to_nested_parenthesis() - ('+', (('*', (3, 4)), 2)) - """ - return self.apply(to_nested_parenthesis) - - def to_postfix(self): - """ Transform the Tree into postfix notation - - :example: - - >>> nested_par = ("+", (1, 2)) - >>> t = Tree.from_nested_parenthesis(nested_par) - >>> t.to_postfix() - [1, 2, '+'] - >>> nested_par = ("+", ( - ... ("*", (3, 4)), - ... 2)) - >>> t = Tree.from_nested_parenthesis(nested_par) - >>> t.to_postfix() - [3, 4, '*', 2, '+'] - >>> nested_par = ("+", ( - ... ("*", (3, 4)), - ... ("*", (5, 6)), - ... )) - >>> t = Tree.from_nested_parenthesis(nested_par) - >>> t.to_postfix() - [3, 4, '*', 5, 6, '*', '+'] - """ - return self.apply(postfix_concatenate) - def get_leafs(self, callback = lambda x:x): """ Generator which yield all the leaf value of the tree. Callback act on every leaf. @@ -475,6 +433,49 @@ class Tree(object): """ return self.apply(show_tree) + def to_nested_parenthesis(self): + """ Transform the Tree into its nested parenthesis description + :example: + + >>> nested_par = ("+", (1, 2)) + >>> t = Tree.from_nested_parenthesis(nested_par) + >>> t.to_nested_parenthesis() + ('+', (1, 2)) + >>> nested_par = ("+", ( + ... ("*", (3, 4)), + ... 2)) + >>> t = Tree.from_nested_parenthesis(nested_par) + >>> t.to_nested_parenthesis() + ('+', (('*', (3, 4)), 2)) + """ + return self.apply(to_nested_parenthesis) + + def to_postfix(self): + """ Transform the Tree into postfix notation + + :example: + + >>> nested_par = ("+", (1, 2)) + >>> t = Tree.from_nested_parenthesis(nested_par) + >>> t.to_postfix() + [1, 2, '+'] + >>> nested_par = ("+", ( + ... ("*", (3, 4)), + ... 2)) + >>> t = Tree.from_nested_parenthesis(nested_par) + >>> t.to_postfix() + [3, 4, '*', 2, '+'] + >>> nested_par = ("+", ( + ... ("*", (3, 4)), + ... ("*", (5, 6)), + ... )) + >>> t = Tree.from_nested_parenthesis(nested_par) + >>> t.to_postfix() + [3, 4, '*', 5, 6, '*', '+'] + """ + return self.apply(postfix_concatenate) + + class MutableTree(Tree): @@ -767,6 +768,20 @@ class MutableTree(Tree): self.right_value = nright_value +class LeafTree(Tree): + + """ A LeafTree is a tree which act as if it is a leaf. + It blocks thoses methods: + - apply + - apply_on_last_level + """ + + def apply(self, *args): + raise AttributeError("Can't use apply on a LeafTree") + + def apply_on_last_level(self, *args): + raise AttributeError("Can't use apply on a LeafTree") + # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: