MCPcopy Create free account
hub / github.com/ActiveState/code / rec_node

Function rec_node

recipes/Python/533146_AST_Pretty_Printer/recipe-533146.py:19–44  ·  view source on GitHub ↗

Recurse through a node, pretty-printing it.

(node, level, indent, write)

Source from the content-addressed store, hash-verified

17 stream.write('\n')
18
19def rec_node(node, level, indent, write):
20 "Recurse through a node, pretty-printing it."
21 pfx = indent * level
22 if isinstance(node, Node):
23 write(pfx)
24 write(node.__class__.__name__)
25 write('(')
26
27 if any(isinstance(child, Node) for child in node.getChildren()):
28 for i, child in enumerate(node.getChildren()):
29 if i != 0:
30 write(',')
31 write('\n')
32 rec_node(child, level+1, indent, write)
33 write('\n')
34 write(pfx)
35 else:
36 # None of the children as nodes, simply join their repr on a single
37 # line.
38 write(', '.join(repr(child) for child in node.getChildren()))
39
40 write(')')
41
42 else:
43 write(pfx)
44 write(repr(node))
45
46
47def main():

Callers 1

printAstFunction · 0.70

Calls 5

writeFunction · 0.50
anyFunction · 0.50
enumerateFunction · 0.50
getChildrenMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected