本文整理汇总了Python中treelib.Tree.parent方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.parent方法的具体用法?Python Tree.parent怎么用?Python Tree.parent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类treelib.Tree
的用法示例。
在下文中一共展示了Tree.parent方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_modify_node_identifier_root
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import parent [as 别名]
def test_modify_node_identifier_root(self):
tree = Tree()
tree.create_node("Harry", "harry")
tree.create_node("Jane", "jane", parent="harry")
tree.update_node(tree['harry'].identifier, identifier='xyz', tag='XYZ')
self.assertTrue(tree.root == 'xyz')
self.assertTrue(tree['xyz'].tag == 'XYZ')
self.assertEqual(tree.parent('jane').identifier, 'xyz')
示例2: StateMachine
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import parent [as 别名]
class StateMachine(object):
"""A class to track information about a state machine"""
def __init__(self, name):
self.name = name
self.events = {}
self.effects = {}
self.state_tree = Tree()
self.current_state = None
# Add the Root state automatically
self.add_state('Root')
def add_state(self, name):
assert isinstance(name, str)
state_node = Node(identifier=name, data=State(name))
if self.current_state is None:
self.state_tree.add_node(state_node)
self.current_state = state_node.data
else:
self.state_tree.add_node(state_node, self.current_state.name)
def add_event(self, ev):
assert isinstance(ev, Event)
self.events[ev.name] = ev
def add_effect(self, eff):
assert isinstance(eff, Effect)
self.effects[eff.name] = eff
def enter_state(self, state):
self.current_state = state
def exit_state(self, state):
self.current_state = self.state_tree.parent(state.name).data
def get_state_by_name(self, state_name):
return self.state_tree.get_node(state_name).data
示例3: crossOver
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import parent [as 别名]
def crossOver(individualA, individualB):
tree = None
while tree is None or tree.depth(tree.get_node(tree.root)) > TREE_MAX_DEPTH:
treeA = Tree(tree = individualA.tree, deep=True)
treeB = Tree(tree = individualB.tree, deep=True)
regenerate_ids(treeA)
regenerate_ids(treeB)
removedNode = random.choice(treeA.all_nodes())
addedNode = random.choice(treeB.all_nodes())
addedSubtree = Tree(tree = treeB.subtree(addedNode.identifier), deep=True)
if treeA.root == removedNode.identifier:
tree = addedSubtree
else:
parent = treeA.parent(removedNode.identifier)
treeA.remove_subtree(removedNode.identifier)
treeA.paste(parent.identifier, addedSubtree)
tree = treeA
return Individual(tree)
示例4: test_subtree
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import parent [as 别名]
def test_subtree(self):
subtree_copy = Tree(self.tree.subtree("jane"), deep=True)
self.assertEqual(subtree_copy.parent("jane") is None, True)
subtree_copy["jane"].tag = "Sweeti"
self.assertEqual(self.tree["jane"].tag == "Jane", True)
self.assertEqual(subtree_copy.level("diane"), 1)
self.assertEqual(subtree_copy.level("jane"), 0)
self.assertEqual(self.tree.level("jane"), 1)