Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'that?': 0.05; 'assign': 0.07; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'type,': 0.09; 'python': 0.11; 'language.': 0.14; 'useful,': 0.14; 'ast': 0.16; 'dump': 0.16; 'for,': 0.16; 'node,': 0.16; 'nodes': 0.16; 'readable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'repr': 0.16; 'language': 0.16; 'wrote:': 0.18; 'module': 0.19; 'fit': 0.20; 'seems': 0.21; 'print': 0.22; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'logical': 0.24; 'looks': 0.24; '---': 0.24; "i've": 0.25; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'chris': 0.29; "i'm": 0.30; '>>>>': 0.31; '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; 'there': 0.35; 'really': 0.36; 'representing': 0.36; 'method': 0.36; 'being': 0.38; 'to:addr :python-list': 0.38; 'anything': 0.39; 'url:01': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'subject: / ': 0.60; 'most': 0.60; 'free': 0.61; 'viruses': 0.61; 'name': 0.63; 'protection': 0.63; 'our': 0.64; 'more': 0.64; 'url:blogspot': 0.65; 'url:co': 0.67; 'antivirus': 0.68; 'analysis': 0.75; 'hanging': 0.84; "it'd": 0.84; 'destination': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Turning an AST node / subnodes into something human-readable Date: Wed, 19 Feb 2014 15:04:29 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-19-140.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: X-Antivirus: avast! (VPS 140219-0, 19/02/2014), Outbound message X-Antivirus-Status: Clean X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392822246 news.xs4all.nl 2978 [2001:888:2000:d::a6]:48912 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66693 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