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


Groups > comp.lang.python > #66664

Turning an AST node / subnodes into something human-readable

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'that?': 0.05; 'assign': 0.07; 'subject:into': 0.09; 'type,': 0.09; 'python': 0.11; 'useful,': 0.14; 'ast': 0.16; 'dump': 0.16; 'for,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'node,': 0.16; 'nodes': 0.16; 'readable': 0.16; 'repr': 0.16; 'module': 0.19; 'fit': 0.20; 'seems': 0.21; '>>>': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; "aren't": 0.24; 'logical': 0.24; 'looks': 0.24; "i've": 0.25; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'node': 0.31; 'file': 0.32; 'maybe': 0.34; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'something': 0.35; 'good.': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'representing': 0.36; 'method': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'easy': 0.60; 'subject: / ': 0.60; 'most': 0.60; 'name': 0.63; 'more': 0.64; 'analysis': 0.75; 'hanging': 0.84; "it'd": 0.84; 'destination': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=yYtJP2v3IE8pdYhVKfT8BkV7XWN+chh/f8t8jsHeRPs=; b=rZIIknUHL9XUs7QWroZb/xUr/p8jPkJJfCtkAxxikjlve6ZLPQl0ZilnzXxRa+inky s9jIRRpy6ulWt7bxM1bP50ktWGRa3YppGRQqvo6Zk2Ly083sTRh+gYzS4py9iZILuAi+ ZnZBFb6Lh84ik1OzGlL3Ly3FuRgzUivgpYCYjeLS/t/XSqLkotWjn/Vnueg43tqC/hwO VCB5Riixj+Fm8ySRH0kUj3qwRuZxoqPGH7c/qqUA5ow0fYmoI8zeMdy4i4LSwRZPrbYP Yoz5vBgFMCcKnNbqHOE+VHuWtEHqsk+wOCa8zW9w7DPTyFo+1/SCuH0HBB0HeHKTitMg a2xw==
MIME-Version 1.0
X-Received by 10.66.25.241 with SMTP id f17mr37104982pag.127.1392782310099; Tue, 18 Feb 2014 19:58:30 -0800 (PST)
Date Wed, 19 Feb 2014 14:58:30 +1100
Subject Turning an AST node / subnodes into something human-readable
From Chris Angelico <rosuav@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7134.1392782314.18130.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392782314 news.xs4all.nl 2839 [2001:888:2000:d::a6]:48421
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66664

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web