MCPcopy Create free account
hub / github.com/datacamp/pythonwhat / Tree

Class Tree

pythonwhat/probe.py:45–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43
44
45class Tree(object):
46 def __init__(self):
47 """
48 Represent a tree of nodes, by holding the currently active node.
49
50 This class is necessary to put sub-tests onto a graph, because they may
51 exist inside of function calls. By getting the currently active node
52 from the tree, Probe instances may add subtests as children on that node,
53 and update it when recursing over sub-tests.
54
55 """
56 self.root = Node(name="root")
57 self.crnt_node = self.root
58
59 @classmethod
60 def str_branch(cls, node, str_func=lambda s: str(s)):
61 # dict(getattr(s.data.get("bound_args", {}), 'arguments', {}))
62 f = node.data.get("func")
63 this_node = (
64 " " * node.depth
65 + "("
66 + getattr(f, "__name__", node.name)
67 + str_func(node)
68 + ")\n"
69 )
70 return this_node + "".join(
71 map(lambda x: cls.str_branch(x, str_func), node.child_list)
72 )
73
74 def __str__(self):
75 return self.str_branch(self.crnt_node)
76
77 def descend(self, node=None):
78 node = self.crnt_node if node is None else node
79 children = map(self.descend, node.child_list)
80 base = [node] if node.name != "root" else []
81 return sum(children, base)
82
83 def __iter__(self):
84 for ii in self.descend(self.crnt_node):
85 yield ii
86
87
88class Node(object):

Callers 1

build_probe_contextFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected