Turning an AST node / subnodes into something human-readable

Irmen de Jong irmen.NOSPAM at xs4all.nl
Wed Feb 19 03:05:18 EST 2014


On 19-2-2014 4:58, Chris Angelico wrote:

> but what I'd really like to do is get something that looks
> approximately like "x[1]". Is there an easy way to do that? Its str
> and repr aren't useful, and I can't see a "reconstitute" method on the
> node, nor a function in ast itself for the job. In theory I could
> write one, but it'd need to understand every node type, so it seems
> the most logical place would be on the node itself - maybe in __str__.
> 
> Is there anything nice and easy? I don't care if it's not perfect, as
> long as it's more readable than ast.dump(). :)
> 

Maybe this https://pypi.python.org/pypi/astor can do what you want?
(found it by following a few links starting from
http://stackoverflow.com/questions/768634/python-parse-a-py-file-read-the-ast-modify-it-then-write-back-the-modified)

Irmen



More information about the Python-list mailing list