本文整理汇总了Python中tree.Tree.addChild方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.addChild方法的具体用法?Python Tree.addChild怎么用?Python Tree.addChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.addChild方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: antlrTree2Tree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import addChild [as 别名]
def antlrTree2Tree(antlrTree):
t = Tree(antlrTree.type, antlrTree.text, antlrTree.line, antlrTree.charPositionInLine)
for i in range(antlrTree.getChildCount()):
subT = antlrTree2Tree(antlrTree.getChild(i))
t.addChild(subT)
return t
示例2: TestTree
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import addChild [as 别名]
class TestTree(unittest.TestCase):
def setUp(self):
self.root = Tree(NAME)
self.tree2 = None
self.tree3 = None
self.tree4 = None
def _AddChild(self, name):
new_tree = Tree(name)
self.root.addChild(new_tree)
return new_tree
def _createComplexTree(self):
"""
Creates the following tree
NAME1->NAME2->NAME4
NAME1->NAME3
"""
self.tree2 = self._AddChild(NAME2)
self.tree3 = self._AddChild(NAME3)
self.tree4 = Tree(NAME4)
self.tree2.addChild(self.tree4)
def testConstructor(self):
if IGNORE_TEST:
return
self.assertEqual(self.root._name, NAME)
self.assertEqual(len(self.root._children), 0)
def testAddChild(self):
if IGNORE_TEST:
return
new_tree = self._AddChild(NAME2)
self.assertEqual(len(self.root._children), 1)
self.assertEqual(self.root._children[0], new_tree)
newer_tree = self._AddChild(NAME3)
self.assertEqual(len(self.root._children), 2)
self.assertEqual(self.root._children[1], newer_tree)
def testAddChildComplex(self):
if IGNORE_TEST:
return
self._createComplexTree()
self.assertTrue(self.tree4 in self.tree2.getChildren())
def testRemoveChildSimple(self):
if IGNORE_TEST:
return
new_tree = self._AddChild(NAME2)
new_tree.removeTree()
self.assertIsNone(new_tree._parent)
self.assertEqual(len(self.root._children), 0)
def testRemoveChildComplex(self):
if IGNORE_TEST:
return
self._createComplexTree()
self.tree4.removeTree()
self.assertIsNone(self.tree4._parent)
self.assertEqual(len(self.tree2._children), 0)
self.assertEqual(len(self.root._children), 2)
def testGetRoot(self):
if IGNORE_TEST:
return
self._createComplexTree()
self.assertEqual(self.tree2._children[0], self.tree4)
root = self.tree4.getRoot()
self.assertEqual(root, self.root)
root = self.root.getRoot()
self.assertEqual(root, self.root)
root = self.tree3.getRoot()
self.assertEqual(root, self.root)
def testGetChildrenFromRoot(self):
if IGNORE_TEST:
return
self._createComplexTree()
children = self.tree2.getChildren(is_from_root=False)
self.assertEqual(children, [self.tree4])
children = self.tree4.getChildren(is_from_root=True,
is_recursive=True)
self.assertEqual(len(children), 3)
self.assertFalse(self.root in children)
self.assertTrue(self.tree2 in children)
self.assertTrue(self.tree4 in children)
def testGetChildrenFromSelf(self):
if IGNORE_TEST:
return
self._createComplexTree()
children = self.tree2.getChildren(is_from_root=False)
grandchildren = self.tree4.getChildren(is_from_root=False)
self.assertEqual(len(children), 1)
self.assertEqual(len(grandchildren), 0)
self.assertTrue(self.tree4 in children)
def testFindPathFromRoot(self):
#.........这里部分代码省略.........