osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Turning an AST node / subnodes into something human-readable


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