Python anytree.Node() Examples
The following are 30
code examples of anytree.Node().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
anytree
, or try the search function
.
Example #1
Source File: checkerfactory.py From securityheaders with Apache License 2.0 | 7 votes |
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
Example #2
Source File: test_iterators.py From anytree with Apache License 2.0 | 6 votes |
def test_levelorder(): """LevelOrderIter.""" 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) eq_(list(LevelOrderIter(f)), [f, b, g, a, d, i, c, e, h]) eq_(list(LevelOrderIter(f, maxlevel=0)), []) eq_(list(LevelOrderIter(f, maxlevel=3)), [f, b, g, a, d, i]) eq_(list(LevelOrderIter(f, filter_=lambda n: n.name not in ('e', 'g'))), [f, b, a, d, i, c, h]) eq_(list(LevelOrderIter(f, stop=lambda n: n.name == 'd')), [f, b, g, a, i, h]) it = LevelOrderIter(f) eq_(next(it), f) eq_(next(it), b) eq_(list(it), [g, a, d, i, c, e, h])
Example #3
Source File: reinsurance_layer.py From OasisLMF with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #4
Source File: test_render.py From anytree with Apache License 2.0 | 6 votes |
def test_maxlevel(): root = anytree.Node("root", lines=["c0fe", "c0de"]) s0 = anytree.Node("sub0", parent=root, lines=["ha", "ba"]) s0b = anytree.Node("sub0B", parent=s0, lines=["1", "2", "3"]) s0a = anytree.Node("sub0A", parent=s0, lines=["a", "b"]) s1 = anytree.Node("sub1", parent=root, lines=["Z"]) r = anytree.RenderTree(root, maxlevel=2) result = [(pre, node) for pre, _, node in r] expected = [ (u'', root), (u'├── ', s0), (u'└── ', s1), ] print(expected) print(result) eq_(result, expected)
Example #5
Source File: test_search.py From anytree with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: pcap_layer_field.py From black-widow with GNU General Public License v3.0 | 6 votes |
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)
Example #7
Source File: reencode.py From Offensive-Security-Certified-Professional with MIT License | 6 votes |
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)))
Example #8
Source File: test_dotexporter.py From anytree with Apache License 2.0 | 6 votes |
def test_tree3(): """Escape.""" root = Node("root") s0 = Node("sub0", parent=root, edge=2) Node('sub"0"B', parent=s0, foo=4, edge=109) Node("sub'0'A", parent=s0, edge="") s1 = Node("sub\"1", parent=root, edge="") Node("sub1A", parent=s1, edge=7) Node("sub1B", parent=s1, edge=8) s1c = Node("sub1C", parent=s1, edge=22) Node(u"sub1Cä", parent=s1c, edge=42) def nodenamefunc(node): return '%s:%s' % (node.name, node.depth) def edgeattrfunc(node, child): return 'label="%s:%s"' % (DotExporter.esc(node.name), DotExporter.esc(child.name)) r = DotExporter(root, options=["rankdir=LR;"], nodenamefunc=nodenamefunc, nodeattrfunc=lambda node: "shape=box", edgeattrfunc=edgeattrfunc) r.to_dotfile(join(GENPATH, "tree3.dot")) assert cmp(join(GENPATH, "tree3.dot"), join(REFPATH, "tree3.dot"))
Example #9
Source File: burpContextAwareFuzzer.py From burpContextAwareFuzzer with GNU General Public License v3.0 | 6 votes |
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)))
Example #10
Source File: test_walker.py From anytree with Apache License 2.0 | 6 votes |
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"))
Example #11
Source File: test_dotexporter.py From anytree with Apache License 2.0 | 6 votes |
def test_tree2(): """Tree2.""" root = Node("root") s0 = Node("sub0", parent=root, edge=2) Node("sub0B", parent=s0, foo=4, edge=109) Node("sub0A", parent=s0, edge="") s1 = Node("sub1", parent=root, edge="") Node("sub1A", parent=s1, edge=7) Node("sub1B", parent=s1, edge=8) s1c = Node("sub1C", parent=s1, edge=22) Node("sub1Ca", parent=s1c, edge=42) def nodenamefunc(node): return '%s:%s' % (node.name, node.depth) def edgeattrfunc(node, child): return 'label="%s:%s"' % (node.name, child.name) r = DotExporter(root, options=["rankdir=LR;"], nodenamefunc=nodenamefunc, nodeattrfunc=lambda node: "shape=box", edgeattrfunc=edgeattrfunc) r.to_dotfile(join(GENPATH, "tree2.dot")) assert cmp(join(GENPATH, "tree2.dot"), join(REFPATH, "tree2.dot"))
Example #12
Source File: test_examples.py From anytree with Apache License 2.0 | 6 votes |
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'))")
Example #13
Source File: test_resolver.py From anytree with Apache License 2.0 | 6 votes |
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)
Example #14
Source File: test_cachedsearch.py From anytree with Apache License 2.0 | 6 votes |
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)
Example #15
Source File: test_iterators.py From anytree with Apache License 2.0 | 6 votes |
def test_postorder(): """PostOrderIter.""" 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) eq_(list(PostOrderIter(f)), [a, c, e, d, b, h, i, g, f]) eq_(list(PostOrderIter(f, maxlevel=0)), []) eq_(list(PostOrderIter(f, maxlevel=3)), [a, d, b, i, g, f]) eq_(list(PostOrderIter(f, filter_=lambda n: n.name not in ('e', 'g'))), [a, c, d, b, h, i, f]) eq_(list(PostOrderIter(f, stop=lambda n: n.name == 'd')), [a, b, h, i, g, f]) it = PostOrderIter(f) eq_(next(it), a) eq_(next(it), c) eq_(list(it), [e, d, b, h, i, g, f])
Example #16
Source File: test_cachedsearch.py From anytree with Apache License 2.0 | 6 votes |
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)
Example #17
Source File: test_iterators.py From anytree with Apache License 2.0 | 6 votes |
def test_preorder(): """PreOrderIter.""" 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) eq_(list(PreOrderIter(f)), [f, b, a, d, c, e, g, i, h]) eq_(list(PreOrderIter(f, maxlevel=0)), []) eq_(list(PreOrderIter(f, maxlevel=3)), [f, b, a, d, g, i]) eq_(list(PreOrderIter(f, filter_=lambda n: n.name not in ('e', 'g'))), [f, b, a, d, c, i, h]) eq_(list(PreOrderIter(f, stop=lambda n: n.name == 'd')), [f, b, a, g, i, h]) it = PreOrderIter(f) eq_(next(it), f) eq_(next(it), b) eq_(list(it), [a, d, c, e, g, i, h])
Example #18
Source File: test_node.py From anytree with Apache License 2.0 | 6 votes |
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))
Example #19
Source File: test_node.py From anytree with Apache License 2.0 | 6 votes |
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))
Example #20
Source File: test_node.py From anytree with Apache License 2.0 | 6 votes |
def test_eq_overwrite(): """Node with overwritten __eq__.""" class EqOverwrittingNode(NodeMixin): def __init__(self, a, b, parent=None): super(EqOverwrittingNode, self).__init__() self.a = a self.b = b self.parent = parent def __eq__(self, other): if isinstance(other, EqOverwrittingNode): return self.a == other.a and self.b == other.b else: return NotImplemented r = EqOverwrittingNode(0, 0) a = EqOverwrittingNode(1, 0, parent=r) b = EqOverwrittingNode(1, 0, parent=r) assert a.parent is r assert b.parent is r eq_(a.a, 1) eq_(a.b, 0) eq_(b.a, 1) eq_(b.b, 0)
Example #21
Source File: test_node.py From anytree with Apache License 2.0 | 6 votes |
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]))
Example #22
Source File: test_node.py From anytree with Apache License 2.0 | 6 votes |
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())
Example #23
Source File: test_node.py From anytree with Apache License 2.0 | 6 votes |
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)
Example #24
Source File: test_node.py From anytree with Apache License 2.0 | 6 votes |
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]))
Example #25
Source File: test_node.py From anytree with Apache License 2.0 | 6 votes |
def test_is_root(): """Node.is_root.""" 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_root, True) eq_(s0.is_root, False) eq_(s0b.is_root, False) eq_(s0a.is_root, False) eq_(s1.is_root, False) eq_(s1c.is_root, False) eq_(s1ca.is_root, False)
Example #26
Source File: test_node.py From anytree with Apache License 2.0 | 6 votes |
def test_height(): """Node.height.""" 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.height, 3) eq_(s0.height, 1) eq_(s0b.height, 0) eq_(s0a.height, 0) eq_(s1.height, 2) eq_(s1c.height, 1) eq_(s1ca.height, 0)
Example #27
Source File: test_node.py From anytree with Apache License 2.0 | 5 votes |
def test_hookups(): """Hookup attributes #29.""" class MyNode(Node): def _pre_attach(self, parent): eq_(str(self.parent), "None") eq_(self.children, tuple()) eq_(str(self.path), "(MyNode('/B'),)") def _post_attach(self, parent): eq_(str(self.parent), "MyNode('/A')") eq_(self.children, tuple()) eq_(str(self.path), "(MyNode('/A'), MyNode('/A/B'))") def _pre_detach(self, parent): eq_(str(self.parent), "MyNode('/A')") eq_(self.children, tuple()) eq_(str(self.path), "(MyNode('/A'), MyNode('/A/B'))") def _post_detach(self, parent): eq_(str(self.parent), "None") eq_(self.children, tuple()) eq_(str(self.path), "(MyNode('/B'),)") node_a = MyNode('A') node_b = MyNode('B', node_a) # attach B on A node_b.parent = None # detach B from A
Example #28
Source File: test_node.py From anytree with Apache License 2.0 | 5 votes |
def test_tuple(): """Tuple as parent.""" with assert_raises(TreeError, "Parent node (1, 0, 3) is not of type 'NodeMixin'."): Node((0, 1, 2), parent=(1, 0, 3))
Example #29
Source File: test_node.py From anytree with Apache License 2.0 | 5 votes |
def test_node_kwargs(): """Ticket #24.""" class MyNode(Node): def __init__(self, name, parent=None, **kwargs): super(MyNode, self).__init__(name, parent, **kwargs) def _post_attach(self, parent): print(self.my_attribute) node_a = MyNode('A') node_b = MyNode('B', node_a, my_attribute=True) eq_(repr(node_b), "MyNode('/A/B', my_attribute=True)")
Example #30
Source File: checkerfactory.py From securityheaders with Apache License 2.0 | 5 votes |
def __init__(self): self.clazzes = dict() path = securityheaders.checkers.__path__[0] root = Node("Checker") for loader, module_name, is_pkg in pkgutil.walk_packages(securityheaders.checkers.__path__): module = loader.find_module(module_name).load_module(module_name) for name, obj in inspect.getmembers(module): if hasattr(obj, "__name__") and obj.__name__ not in self.clazzes.keys() and inspect.isclass(obj) and issubclass(obj, Checker): self.clazzes[obj.__name__] = obj self.tree = self.__getnames_as_tree__(self.clazzes)