Refact(Tree): Rename type_function to fallback in apply_on_last_level
This commit is contained in:
parent
eae88d6c4c
commit
ecf7e7678f
@ -296,11 +296,11 @@ class Tree(object):
|
|||||||
|
|
||||||
def apply_on_last_level(self,
|
def apply_on_last_level(self,
|
||||||
function,
|
function,
|
||||||
type_function = lambda n,l,r: Tree(n,l,r)):
|
fallback = lambda n,l,r: Tree(n,l,r)):
|
||||||
""" Apply the function on last level of the tree before leaf
|
""" Apply the function on last level of the tree before leaf
|
||||||
|
|
||||||
:param function: (op, a, a) -> b function to apply on last level
|
:param function: (op, a, a) -> b function to apply on last level
|
||||||
:param type_function: (op, a, a) -> b typing function to apply on subtree which raise a TypeError. The return value will be concidered as an leaf
|
:param fallback: (op, a, a) -> b fallback function to apply on subtree which raise a NotImplementedError. The return value will be concidered as an leaf
|
||||||
:returns: b if it is a 1 level Tree, Tree otherwise
|
:returns: b if it is a 1 level Tree, Tree otherwise
|
||||||
|
|
||||||
|
|
||||||
@ -333,12 +333,12 @@ class Tree(object):
|
|||||||
right_is_leaf = 0
|
right_is_leaf = 0
|
||||||
try:
|
try:
|
||||||
left_applied = self.left_value.\
|
left_applied = self.left_value.\
|
||||||
apply_on_last_level(function, type_function)
|
apply_on_last_level(function, fallback)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
# TODO: overload __getitem__ to ease unpacking |dim. sept. 23 19:17:50 CEST 2018
|
# TODO: overload __getitem__ to ease unpacking |dim. sept. 23 19:17:50 CEST 2018
|
||||||
left_is_leaf = 1
|
left_is_leaf = 1
|
||||||
left_tree = self.left_value
|
left_tree = self.left_value
|
||||||
left_applied = type_function(left_tree.node,
|
left_applied = fallback(left_tree.node,
|
||||||
left_tree.left_value,
|
left_tree.left_value,
|
||||||
left_tree.right_value)
|
left_tree.right_value)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@ -347,11 +347,11 @@ class Tree(object):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
right_applied = self.right_value.\
|
right_applied = self.right_value.\
|
||||||
apply_on_last_level(function, type_function)
|
apply_on_last_level(function, fallback)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
right_is_leaf = 1
|
right_is_leaf = 1
|
||||||
right_tree = self.right_value
|
right_tree = self.right_value
|
||||||
right_applied = type_function(right_tree.node,
|
right_applied = fallback(right_tree.node,
|
||||||
right_tree.left_value,
|
right_tree.left_value,
|
||||||
right_tree.right_value)
|
right_tree.right_value)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
Loading…
Reference in New Issue
Block a user