Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66664 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2014-02-19 14:58 +1100 |
| Last post | 2014-02-19 19:44 +1100 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
Turning an AST node / subnodes into something human-readable Chris Angelico <rosuav@gmail.com> - 2014-02-19 14:58 +1100
Re: Turning an AST node / subnodes into something human-readable Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2014-02-19 09:05 +0100
Re: Turning an AST node / subnodes into something human-readable Chris Angelico <rosuav@gmail.com> - 2014-02-19 19:44 +1100
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-19 14:58 +1100 |
| Subject | Turning an AST node / subnodes into something human-readable |
| Message-ID | <mailman.7134.1392782314.18130.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Irmen de Jong <irmen.NOSPAM@xs4all.nl> |
|---|---|
| Date | 2014-02-19 09:05 +0100 |
| Message-ID | <530465bd$0$2971$e4fe514c@news.xs4all.nl> |
| In reply to | #66664 |
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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-19 19:44 +1100 |
| Message-ID | <mailman.7140.1392799493.18130.python-list@python.org> |
| In reply to | #66673 |
On Wed, Feb 19, 2014 at 7:05 PM, Irmen de Jong <irmen.NOSPAM@xs4all.nl> wrote: > 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) > 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web