Coverage for phml\misc\__init__.py: 100%
17 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-08 13:19 -0600
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-08 13:19 -0600
1"""phml.utils.misc
3Helpful utilities for different tasks that doesn't have a place in the other categories.
4"""
6from phml.nodes import Element, Root
8from .classes import *
9from .component import *
10from .heading import *
11from .inspect import *
13# __all__ = [
14# "depth",
15# "size",
16# "heading_rank",
17# "classnames",
18# "ClassList",
19# "inspect",
20# "normalize_indent",
21# ]
24def depth(node) -> int:
25 """Get the depth in the tree for a given node.
27 -1 means that you passed in the tree itself and you are at the
28 ast's root.
29 """
31 level = -1
32 while node.parent is not None:
33 level += 1
34 node = node.parent
36 return level
39def size(node: Root | Element) -> int:
40 """Get the number of nodes recursively."""
41 from phml import walk # pylint: disable=import-outside-toplevel
43 count = 0
45 for _ in walk(node):
46 count += 1
48 return count