phml.utilities.misc
Helpful utilities for different tasks that doesn't have a place in the other categories.
1"""phml.utilities.misc 2 3Helpful utilities for different tasks that doesn't have a place in the other categories. 4""" 5 6from phml.nodes import Parent 7 8from .classes import * 9from .heading import * 10 11 12def depth(node) -> int: 13 """Get the depth in the tree for a given node. 14 15 -1 means that you passed in the tree itself and you are at the 16 ast's root. 17 """ 18 19 level = -1 20 while node.parent is not None: 21 level += 1 22 node = node.parent 23 24 return level 25 26 27def size(node: Parent) -> int: 28 """Get the number of nodes recursively.""" 29 from phml.utilities import walk # pylint: disable=import-outside-toplevel 30 31 count = 0 32 33 for _ in walk(node): 34 count += 1 35 36 return count
def
depth(node) -> int:
13def depth(node) -> int: 14 """Get the depth in the tree for a given node. 15 16 -1 means that you passed in the tree itself and you are at the 17 ast's root. 18 """ 19 20 level = -1 21 while node.parent is not None: 22 level += 1 23 node = node.parent 24 25 return level
Get the depth in the tree for a given node.
-1 means that you passed in the tree itself and you are at the ast's root.
28def size(node: Parent) -> int: 29 """Get the number of nodes recursively.""" 30 from phml.utilities import walk # pylint: disable=import-outside-toplevel 31 32 count = 0 33 34 for _ in walk(node): 35 count += 1 36 37 return count
Get the number of nodes recursively.