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

1"""phml.utils.misc 

2 

3Helpful utilities for different tasks that doesn't have a place in the other categories. 

4""" 

5 

6from phml.nodes import Element, Root 

7 

8from .classes import * 

9from .component import * 

10from .heading import * 

11from .inspect import * 

12 

13# __all__ = [ 

14# "depth", 

15# "size", 

16# "heading_rank", 

17# "classnames", 

18# "ClassList", 

19# "inspect", 

20# "normalize_indent", 

21# ] 

22 

23 

24def depth(node) -> int: 

25 """Get the depth in the tree for a given node. 

26 

27 -1 means that you passed in the tree itself and you are at the 

28 ast's root. 

29 """ 

30 

31 level = -1 

32 while node.parent is not None: 

33 level += 1 

34 node = node.parent 

35 

36 return level 

37 

38 

39def size(node: Root | Element) -> int: 

40 """Get the number of nodes recursively.""" 

41 from phml import walk # pylint: disable=import-outside-toplevel 

42 

43 count = 0 

44 

45 for _ in walk(node): 

46 count += 1 

47 

48 return count