Throw basis of LeafTree
This commit is contained in:
parent
72864f12cd
commit
8b201fa8d0
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user