Turning an AST node / subnodes into something human-readable

C

Chris Angelico

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?

Take this cut-down example:

module = ast.parse("x[1] = 345+456")
assign = list(ast.walk(module))[1]
destination = assign.targets[0]

At this point, destination is the subtree representing what's being
assigned to. I can get a verbose dump of that:
Subscript(value=Name(id='x', ctx=Load()), slice=Index(value=Num(n=1)),
ctx=Store())

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(). :)

ChrisA
 
I

Irmen de Jong

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/...he-ast-modify-it-then-write-back-the-modified)

Irmen
 
C

Chris Angelico

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/...he-ast-modify-it-then-write-back-the-modified)

Hmm. I saw a few (things like codegen), but was hoping to stick to the
standard library - introducing a dependency in a small script just for
the sake of tidy output is a bit messy. Oh well. Some things just
aren't as ideal as I'd like. Thanks Irmen!

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top