Turning an AST node / subnodes into something human-readable

Dan Goodman dg.gmane at thesamovar.net
Wed Feb 19 12:40:14 EST 2014


Chris Angelico <rosuav <at> gmail.com> writes:
> I'm working with the ast module to do some analysis on Python
> codebases, and once I've found what I'm looking for, I want to print
> something out. The file name I'm hanging onto externally, so that
> works; and the nodes all have a lineno. So far so good. But how do I
> "reconstitute" a subtree into something fit for human consumption?

I did something like this, feel free to use my code:

https://github.com/brian-team/brian2/blob/master/brian2/parsing/rendering.py

At the moment, it only works for series of mathematical statements (no
control conditions, loops, array notation, etc.), but it would be pretty
easy to add those. It also puts too many parentheses in outputted
expressions, e.g. 1+2+3 would come back as (1+2)+3, etc.

Dan




More information about the Python-list mailing list