本文整理汇总了Python中anytree.Node方法的典型用法代码示例。如果您正苦于以下问题:Python anytree.Node方法的具体用法?Python anytree.Node怎么用?Python anytree.Node使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类anytree
的用法示例。
在下文中一共展示了anytree.Node方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __getnames_as_tree__
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def __getnames_as_tree__(self, clazzes):
root = Node("Checker")
resultnodes = dict()
worklist = list()
clazz = clazzes['Checker']
setattr(root,"clazz", clazz)
worklist.append(root)
while len(worklist) > 0:
parent = worklist.pop()
subclasses = [sub for sub in parent.clazz.__subclasses__() if parent.clazz in sub.__bases__]
subclasses = [sub for sub in subclasses if sub in clazzes.values()]
for child in subclasses:
childpath = "/".join([f.name for f in parent.path]) + "/" + child.__name__
found = False
for c in parent.children:
if childpath == "/".join([f.name for f in c.path]):
found = True
if not found:
childnode = Node(child.__name__, parent)
setattr(childnode,"clazz", child)
worklist.append(childnode)
return root
示例2: __init__
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def __init__(self, layer_field: LayerField = None, sanitized_name: str = None, parent=None, children=None):
"""
:param layer_field: The LayerField of this Node
:type parent: PcapLayerField
:param children: The children of this Node
"""
kwargs = {}
if layer_field is not None:
kwargs.update({
'field': layer_field
})
name = layer_field.name
self.is_main = False
else:
name = 'root'
self.is_main = True
self.field = layer_field
self.sanitized_name = sanitized_name
super().__init__(name, parent, children, **kwargs)
示例3: process
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def process(self, data):
root = anytree.Node('None', decoded = data)
prev = root
for (name, curr, branch) in self.generateEncodingTree(data):
ReEncoder.log('[*] Generator returned: ("{}", "{}", {})'.format(
name, curr[:20], str(branch)
))
currNode = anytree.Node(name, parent = prev, decoded = curr)
if branch:
pass
else:
prev = currNode
for pre, fill, node in anytree.RenderTree(root):
if node.name != 'None':
ReEncoder.log("%s%s (%s)" % (pre, node.name, node.decoded[:20].decode('ascii', 'ignore')))
self.encodings = self.getWinningDecodePath(root)
ReEncoder.log('[+] Selected encodings: {}'.format(str(self.encodings)))
示例4: _add_node
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def _add_node(
self,
description, parent, level_id, agg_id,
portfolio_number=oed.NOT_SET_ID, account_number=oed.NOT_SET_ID,
policy_number=oed.NOT_SET_ID, location_number=oed.NOT_SET_ID,
location_group=oed.NOT_SET_ID
):
node = anytree.Node(
description,
parent=parent,
level_id=level_id,
agg_id=agg_id,
portfolio_number=str(portfolio_number),
account_number=str(account_number),
policy_number=str(policy_number),
location_group=str(location_group),
location_number=str(location_number)
)
return node
示例5: test_walker
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_walker():
"""walk test."""
f = Node("f")
b = Node("b", parent=f)
a = Node("a", parent=b)
d = Node("d", parent=b)
c = Node("c", parent=d)
e = Node("e", parent=d)
g = Node("g", parent=f)
i = Node("i", parent=g)
h = Node("h", parent=i)
w = Walker()
eq_(w.walk(f, f), ((), f, ()))
eq_(w.walk(f, b), ((), f, (b,)))
eq_(w.walk(b, f), ((b,), f, ()))
eq_(w.walk(a, f), ((a, b), f, ()))
eq_(w.walk(h, e), ((h, i, g), f, (b, d, e)))
eq_(w.walk(d, e), ((), d, (e,)))
with assert_raises(WalkError, "Node('/a') and Node('/b') are not part of the same tree."):
w.walk(Node("a"), Node("b"))
示例6: test_stackoverflow
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_stackoverflow():
"""Example from stackoverflow."""
udo = Node("Udo")
marc = Node("Marc", parent=udo)
Node("Lian", parent=marc)
dan = Node("Dan", parent=udo)
Node("Jet", parent=dan)
Node("Jan", parent=dan)
joe = Node("Joe", parent=dan)
eq_(str(udo), "Node('/Udo')")
eq_(str(joe), "Node('/Udo/Dan/Joe')")
eq_(["%s%s" % (pre, node.name) for pre, fill, node in RenderTree(udo)], [
u"Udo",
u"├── Marc",
u"│ └── Lian",
u"└── Dan",
u" ├── Jet",
u" ├── Jan",
u" └── Joe",
])
eq_(str(dan.children),
"(Node('/Udo/Dan/Jet'), Node('/Udo/Dan/Jan'), Node('/Udo/Dan/Joe'))")
示例7: test_glob_cache
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_glob_cache():
"""Wildcard Cache."""
root = at.Node("root")
sub0 = at.Node("sub0", parent=root)
sub1 = at.Node("sub1", parent=root)
r = at.Resolver()
# strip down cache size
at.resolver._MAXCACHE = 2
at.Resolver._match_cache.clear()
eq_(len(at.Resolver._match_cache), 0)
eq_(r.glob(root, "sub0"), [sub0])
eq_(len(at.Resolver._match_cache), 1)
eq_(r.glob(root, "sub1"), [sub1])
eq_(len(at.Resolver._match_cache), 2)
eq_(r.glob(root, "sub*"), [sub0, sub1])
eq_(len(at.Resolver._match_cache), 1)
示例8: test_findall
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_findall():
f = Node("f")
b = Node("b", parent=f)
a = Node("a", parent=b)
d = Node("d", parent=b)
c = Node("c", parent=d)
e = Node("e", parent=d)
eq_(findall(f, filter_=lambda node: node.name in ("a", "b")), (b, a))
eq_(findall(f, filter_=lambda node: d in node.path), (d, c, e))
with assert_raises(CountError, (
"Expecting at least 4 elements, but found 3. "
"(Node('/f/b/d'), Node('/f/b/d/c'), Node('/f/b/d/e'))")):
findall(f, filter_=lambda node: d in node.path, mincount=4)
with assert_raises(CountError, (
"Expecting 2 elements at maximum, but found 3. "
"(Node('/f/b/d'), Node('/f/b/d/c'), Node('/f/b/d/e'))")):
findall(f, filter_=lambda node: d in node.path, maxcount=2)
示例9: test_find
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_find():
f = Node("f")
b = Node("b", parent=f)
Node("a", parent=b)
d = Node("d", parent=b)
Node("c", parent=d)
Node("e", parent=d)
g = Node("g", parent=f)
i = Node("i", parent=g)
Node("h", parent=i)
eq_(find(f, lambda n: n.name == "d"), d)
eq_(find(f, lambda n: n.name == "z"), None)
with assert_raises(CountError, (
"Expecting 1 elements at maximum, but found 5. "
"(Node('/f/b'), Node('/f/b/a'), Node('/f/b/d'), Node('/f/b/d/c'), Node('/f/b/d/e'))")):
find(f, lambda n: b in n.path)
示例10: test_detach_children
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_detach_children():
root = Node("root")
s0 = Node("sub0", parent=root)
s0b = Node("sub0B", parent=s0)
s0a = Node("sub0A", parent=s0)
s1 = Node("sub1", parent=root)
s1a = Node("sub1A", parent=s1)
s1b = Node("sub1B", parent=s1)
s1c = Node("sub1C", parent=s1)
s1ca = Node("sub1Ca", parent=s1c)
eq_(root.descendants, (s0, s0b, s0a, s1, s1a, s1b, s1c, s1ca))
del s0.children
eq_(root.descendants, (s0, s1, s1a, s1b, s1c, s1ca))
del s1.children
eq_(root.descendants, (s0, s1))
示例11: test_children_setter_large
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_children_setter_large():
root = Node("root")
s0 = Node("sub0")
s0b = Node("sub0B")
s0a = Node("sub0A")
s1 = Node("sub1")
s1a = Node("sub1A")
s1b = Node("sub1B")
s1c = Node("sub1C")
s1ca = Node("sub1Ca")
root.children = [s0, s1]
eq_(root.descendants, (s0, s1))
s0.children = [s0a, s0b]
eq_(root.descendants, (s0, s0a, s0b, s1))
s1.children = [s1a, s1b, s1c]
eq_(root.descendants, (s0, s0a, s0b, s1, s1a, s1b, s1c))
with assert_raises(TypeError, "'Node' object is not iterable"):
s1.children = s1ca
eq_(root.descendants, (s0, s0a, s0b, s1, s1a, s1b, s1c))
示例12: test_ancestors
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_ancestors():
"""Node.ancestors."""
root = Node("root")
s0 = Node("sub0", parent=root)
s0b = Node("sub0B", parent=s0)
s0a = Node("sub0A", parent=s0)
s1 = Node("sub1", parent=root)
s1c = Node("sub1C", parent=s1)
s1ca = Node("sub1Ca", parent=s1c)
eq_(root.ancestors, tuple())
eq_(s0.ancestors, tuple([root]))
eq_(s0b.ancestors, tuple([root, s0]))
eq_(s0a.ancestors, tuple([root, s0]))
eq_(s1ca.ancestors, tuple([root, s1, s1c]))
# deprecated typo
eq_(s1ca.anchestors, tuple([root, s1, s1c]))
示例13: test_siblings
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_siblings():
"""Node.siblings."""
root = Node("root")
s0 = Node("sub0", parent=root)
s0b = Node("sub0B", parent=s0)
s0a = Node("sub0A", parent=s0)
s1 = Node("sub1", parent=root)
s1c = Node("sub1C", parent=s1)
s1ca = Node("sub1Ca", parent=s1c)
eq_(root.siblings, tuple())
eq_(s0.siblings, tuple([s1]))
eq_(s0b.siblings, tuple([s0a]))
eq_(s0a.siblings, tuple([s0b]))
eq_(s1.siblings, tuple([s0]))
eq_(s1c.siblings, tuple())
eq_(s1ca.siblings, tuple())
示例14: test_is_leaf
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_is_leaf():
"""Node.is_leaf."""
root = Node("root")
s0 = Node("sub0", parent=root)
s0b = Node("sub0B", parent=s0)
s0a = Node("sub0A", parent=s0)
s1 = Node("sub1", parent=root)
s1c = Node("sub1C", parent=s1)
s1ca = Node("sub1Ca", parent=s1c)
eq_(root.is_leaf, False)
eq_(s0.is_leaf, False)
eq_(s0b.is_leaf, True)
eq_(s0a.is_leaf, True)
eq_(s1.is_leaf, False)
eq_(s1c.is_leaf, False)
eq_(s1ca.is_leaf, True)
示例15: test_leaves
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import Node [as 别名]
def test_leaves():
"""Node.leaves."""
root = Node("root")
s0 = Node("sub0", parent=root)
s0b = Node("sub0B", parent=s0)
s0a = Node("sub0A", parent=s0)
s1 = Node("sub1", parent=root)
s1c = Node("sub1C", parent=s1)
s1ca = Node("sub1Ca", parent=s1c)
eq_(root.leaves, tuple([s0b, s0a, s1ca]))
eq_(s0.leaves, tuple([s0b, s0a]))
eq_(s0b.leaves, tuple([s0b]))
eq_(s0a.leaves, tuple([s0a]))
eq_(s1.leaves, tuple([s1ca]))
eq_(s1c.leaves, tuple([s1ca]))
eq_(s1ca.leaves, tuple([s1ca]))