Searching

Node Searching.

anytree.search.findall(node, filter_=None, stop=None, maxlevel=None, mincount=None, maxcount=None)[source]

Search nodes matching filter_ but stop at maxlevel or stop.

Return tuple with matching nodes.

Parameters:

node – top node, start searching.

Keyword Arguments:
 
  • filter – function called with every node as argument, node is returned if True.
  • stop – stop iteration at node if stop function returns True for node.
  • maxlevel (int) – maximum decending in the node hierarchy.
  • mincount (int) – minimum number of nodes.
  • maxcount (int) – maximum number of nodes.

Example tree:

>>> from anytree import Node, RenderTree, AsciiStyle
>>> 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)
>>> print(RenderTree(f, style=AsciiStyle()).by_attr())
f
|-- b
|   |-- a
|   +-- d
|       |-- c
|       +-- e
+-- g
    +-- i
        +-- h
>>> findall(f, filter_=lambda node: node.name in ("a", "b"))
(Node('/f/b'), Node('/f/b/a'))
>>> findall(f, filter_=lambda node: d in node.path)
(Node('/f/b/d'), Node('/f/b/d/c'), Node('/f/b/d/e'))

The number of matches can be limited:

>>> findall(f, filter_=lambda node: d in node.path, mincount=4)  
Traceback (most recent call last):
  ...
anytree.search.CountError: Expecting at least 4 elements, but found 3. ... Node('/f/b/d/e'))
>>> findall(f, filter_=lambda node: d in node.path, maxcount=2)  
Traceback (most recent call last):
  ...
anytree.search.CountError: Expecting 2 elements at maximum, but found 3. ... Node('/f/b/d/e'))
anytree.search.findall_by_attr(node, value, name='name', maxlevel=None, mincount=None, maxcount=None)[source]

Search nodes with attribute name having value but stop at maxlevel.

Return tuple with matching nodes.

Parameters:
  • node – top node, start searching.
  • value – value which need to match
Keyword Arguments:
 
  • name (str) – attribute name need to match
  • maxlevel (int) – maximum decending in the node hierarchy.
  • mincount (int) – minimum number of nodes.
  • maxcount (int) – maximum number of nodes.

Example tree:

>>> from anytree import Node, RenderTree, AsciiStyle
>>> 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)
>>> print(RenderTree(f, style=AsciiStyle()).by_attr())
f
|-- b
|   |-- a
|   +-- d
|       |-- c
|       +-- e
+-- g
    +-- i
        +-- h
>>> findall_by_attr(f, "d")
(Node('/f/b/d'),)
anytree.search.find(node, filter_=None, stop=None, maxlevel=None)[source]

Search for single node matching filter_ but stop at maxlevel or stop.

Return matching node.

Parameters:

node – top node, start searching.

Keyword Arguments:
 
  • filter – function called with every node as argument, node is returned if True.
  • stop – stop iteration at node if stop function returns True for node.
  • maxlevel (int) – maximum decending in the node hierarchy.

Example tree:

>>> from anytree import Node, RenderTree, AsciiStyle
>>> 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)
>>> print(RenderTree(f, style=AsciiStyle()).by_attr())
f
|-- b
|   |-- a
|   +-- d
|       |-- c
|       +-- e
+-- g
    +-- i
        +-- h
>>> find(f, lambda node: node.name == "d")
Node('/f/b/d')
>>> find(f, lambda node: node.name == "z")
>>> find(f, lambda node: b in node.path)  
Traceback (most recent call last):
    ...
anytree.search.CountError: Expecting 1 elements at maximum, but found 5. (Node('/f/b')... Node('/f/b/d/e'))
anytree.search.find_by_attr(node, value, name='name', maxlevel=None)[source]

Search for single node with attribute name having value but stop at maxlevel.

Return tuple with matching nodes.

Parameters:
  • node – top node, start searching.
  • value – value which need to match
Keyword Arguments:
 
  • name (str) – attribute name need to match
  • maxlevel (int) – maximum decending in the node hierarchy.

Example tree:

>>> from anytree import Node, RenderTree, AsciiStyle
>>> f = Node("f")
>>> b = Node("b", parent=f)
>>> a = Node("a", parent=b)
>>> d = Node("d", parent=b)
>>> c = Node("c", parent=d, foo=4)
>>> e = Node("e", parent=d)
>>> g = Node("g", parent=f)
>>> i = Node("i", parent=g)
>>> h = Node("h", parent=i)
>>> print(RenderTree(f, style=AsciiStyle()).by_attr())
f
|-- b
|   |-- a
|   +-- d
|       |-- c
|       +-- e
+-- g
    +-- i
        +-- h
>>> find_by_attr(f, "d")
Node('/f/b/d')
>>> find_by_attr(f, name="foo", value=4)
Node('/f/b/d/c', foo=4)
>>> find_by_attr(f, name="foo", value=8)
exception anytree.search.CountError(msg, result)[source]

Bases: exceptions.RuntimeError

Error raised on mincount or maxcount mismatch.