本文整理汇总了Python中tree.Tree.add_child方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.add_child方法的具体用法?Python Tree.add_child怎么用?Python Tree.add_child使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.add_child方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_traverse_returns_none_when_operation_always_returns_false
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test_traverse_returns_none_when_operation_always_returns_false():
t = Tree("a")
t.add_child("b")
node = t.traverse(lambda n: False)
assert_that(node, is_(None))
示例2: read_tree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def read_tree(self, line):
parents = map(int,line.split())
trees = dict()
root = None
for i in xrange(1,len(parents)+1):
#if not trees[i-1] and parents[i-1]!=-1:
if i-1 not in trees.keys() and parents[i-1]!=-1:
idx = i
prev = None
while True:
parent = parents[idx-1]
if parent == -1:
break
tree = Tree()
if prev is not None:
tree.add_child(prev)
trees[idx-1] = tree
tree.idx = idx-1
#if trees[parent-1] is not None:
if parent-1 in trees.keys():
trees[parent-1].add_child(tree)
break
elif parent==0:
root = tree
break
else:
prev = tree
idx = parent
return root
示例3: test_find_returns_node_with_the_specified_value_when_such_exists
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test_find_returns_node_with_the_specified_value_when_such_exists():
t = Tree("a")
t.add_child("b")
t.add_child("c").add_child("d").add_child("e")
node = t.find("d")
assert_that(node.children[0].value, is_("e"))
示例4: test_add_child_appends_node_to_the_child_list
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test_add_child_appends_node_to_the_child_list():
t = Tree("a")
t.add_child("b")
t.add_child("c")
assert_that(t.children[0].value, is_("b"))
assert_that(t.children[1].value, is_("c"))
示例5: test_traverse_uses_depth_first_strategy
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test_traverse_uses_depth_first_strategy():
t = Tree("a")
child = t.add_child("b")
child.add_child("d")
child.add_child("e")
t.add_child("c")
visited = []
def visit_all(node):
visited.append(node.value)
return False
t.traverse(visit_all)
assert_that(visited, is_(["a", "b", "d", "e", "c"]))
示例6: build_tree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def build_tree(math_exp_string):
"""
This method is used to build a tree from given input mathematical expression.
Following consideration has been taken
1. higher order operations are given with complete parenthesis ex. 1 - (2*3)
2. add left and right parenthesis if not given ex. (1 - (2 * 3))
3. print error message for any exception
"""
if not validate_math_exp(math_exp_string):
raise InvalidInput('Validation Error, one or more parenthesis are not closed properly')
exp_list = filter_exp_list(math_exp_string)
stack = Stack()
current_node = Tree()
for token in exp_list:
if token == '(':
current_node.add_child()
stack.push(current_node)
current_node = current_node.get_newborn_child()
elif token == ')':
if stack.size():
current_node = stack.pop()
elif token in operator_map.keys():
if current_node.get_val():
if current_node.get_val() == token:
current_node.add_child()
stack.push(current_node)
current_node = current_node.get_newborn_child()
else:
parent = Tree(token)
parent.update_child(current_node)
parent.add_child()
stack.push(parent)
current_node = parent.get_newborn_child()
else:
current_node.set_val(token)
current_node.add_child()
stack.push(current_node)
current_node = current_node.get_newborn_child()
else:
try:
current_node.set_val(float(token))
except ValueError, e:
logging.info(e.message)
current_node.set_val(token)
current_node = stack.pop()
示例7: test_traversal
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test_traversal(mock_dfs):
mock_dfs.side_effect = dfs
root = Tree(1)
first_child = Tree(2)
root.add_child(first_child)
first_child.add_child(Tree(4))
root.add_child(Tree(3))
# Traversal order verified with print statements, since the function is
# only called once (no recursion).
assert mock_dfs(root, 4)
assert_equals(mock_dfs.call_count, 3)
示例8: FCMdata
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
#.........这里部分代码省略.........
def gate(self, g, chan=None):
"""return gated region of fcm data"""
return g.gate(self, chan)
def subsample(self, s, model='random', *args, **kwargs):
"""return subsampled/sliced fcm data"""
if isinstance(s, Subsample):
return s.subsample(self)
elif isinstance(s, slice):
r = Subsample(s)
return r.subsample(self)
else:
if model == 'random':
r = RandomSubsample(s)
elif model == 'anomaly':
r = AnomalySubsample(s, *args, **kwargs)
elif model == 'bias':
r = BiasSubsample(s, *args, **kwargs)
return r.subsample(self, *args, **kwargs)
def compensate(self, sidx=None, spill=None):
"""Compensate the fcm data"""
compensate(self, S=spill, markers=sidx)
return self
def get_cur_node(self):
""" get current node """
return self.current_node
def add_view(self, node):
"""add a new node to the view tree"""
self.tree.add_child(node.name, node)
return self
def summary(self):
"""returns summary of current view"""
pnts = self.view()
means = pnts.mean(0)
stds = pnts.std(0)
mins = pnts.min(0)
maxs = pnts.max(0)
medians = median(pnts, 0)
dim = pnts.shape[1]
summary = ''
for i in range(dim):
summary = summary + self.channels[i] + ":\n"
summary = summary + " max: " + str(maxs[i]) + "\n"
summary = summary + " mean: " + str(means[i]) + "\n"
summary = summary + " median: " + str(medians[i]) + "\n"
summary = summary + " min: " + str(mins[i]) + "\n"
summary = summary + " std: " + str(stds[i]) + "\n"
return summary
def boundary_events(self):
"""returns dictionary of fraction of events in first and last
channel for each channel"""
boundary_dict = {}
for k, chan in enumerate(self.channels):
col = self.view()[:, k]
boundary_dict[chan] = \
sum((col == min(col)) | (col == max(col))) / len(col)
return boundary_dict
def export(self, file_name):
"""
export out current view to a fcs file
"""
from fcm.io import export_fcs
export_fcs(file_name, self.view(), self.current_node.channels, self.notes.text)
def extract_channels(self, channels, keep=False):
"""
create a view without the specified channels or with if keep==True
"""
if isinstance(channels, basestring) or isinstance(channels, int):
channels = [channels]
for i, j in enumerate(channels):
if isinstance(j, str):
channels[i] = self.name_to_index(j)
if keep:
channels = [ i for i in range(len(self.channels)) if i not in channels]
d = DropChannel(channels)
d.drop(self)
return self
def add_channel(self, name, channel=None):
if channel is None:
channel = zeros((self.shape[0],1))
print channel.shape
node = AddChannel(channel, name)
node.add(self)
return self
示例9: test_add_child_returns_appended_node_instance
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test_add_child_returns_appended_node_instance():
t = Tree("a")
new_node = t.add_child("b")
assert_that(new_node, equal_to(t.children[0]))
示例10: test_not_in_tree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test_not_in_tree():
root = Tree(6)
root.add_child(Tree(2))
assert not dfs(root, 1)
示例11: test_non_first_child
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test_non_first_child():
root = Tree(6)
root.add_child(Tree(2))
root.add_child(Tree(1))
assert dfs(root, 1)
示例12: test_single_child
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test_single_child():
root = Tree(6)
root.add_child(Tree(2))
assert dfs(root, 2)
示例13: test2
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import add_child [as 别名]
def test2():
tree1 = Tree([1, 2, 3])
tree1.add_child([4, 5, 6], tree1.root)
tree2 = deepcopy(tree1)
tree2.root.data[1] = 20000
return tree1, tree2