Coverage for phml\utilities\misc\__init__.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-06 16:37 -0500

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