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

Function rec_node

recipes/Python/533145_AST_Pretty_Printer/recipe-533145.py:15–40  ·  view source on GitHub ↗

Recurse through a node, pretty-printing it.

(node, level, indent, write)

Source from the content-addressed store, hash-verified

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

Callers 1

pprintAstFunction · 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