Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66693 > unrolled thread

Re: Turning an AST node / subnodes into something human-readable

Started byMark Lawrence <breamoreboy@yahoo.co.uk>
First post2014-02-19 15:04 +0000
Last post2014-02-19 15:04 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Turning an AST node / subnodes into something human-readable Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-19 15:04 +0000

#66693 — Re: Turning an AST node / subnodes into something human-readable

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-02-19 15:04 +0000
SubjectRe: Turning an AST node / subnodes into something human-readable
Message-ID<mailman.7151.1392822245.18130.python-list@python.org>
On 19/02/2014 03:58, Chris Angelico wrote:
> 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:
>
>>>> print(ast.dump(destination))
> 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
>

http://alexleone.blogspot.co.uk/2010/01/python-ast-pretty-printer.html ?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web