Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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; 'puts': 0.07; 'dan': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'subject:into': 0.09; 'url:github': 0.09; 'python': 0.11; 'ast': 0.16; 'etc.),': 0.16; 'expressions,': 0.16; 'for,': 0.16; 'message-id:@post.gmane.org': 0.16; 'nodes': 0.16; 'notation,': 0.16; 'parentheses': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'url:py': 0.16; 'module': 0.19; 'fit': 0.20; 'print': 0.22; 'header:User-Agent:1': 0.23; 'mathematical': 0.24; 'received:comcast.net': 0.24; "i've": 0.25; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'received:24': 0.27; 'chris': 0.29; 'array': 0.29; "i'm": 0.30; 'writes:': 0.31; 'file': 0.32; 'something': 0.35; 'good.': 0.35; 'but': 0.35; 'add': 0.35; 'charset:us-ascii': 0.36; 'too': 0.37; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; 'subject: / ': 0.60; 'free': 0.61; 'back': 0.62; 'name': 0.63; 'series': 0.66; 'analysis': 0.75; 'hanging': 0.84; 'url:master': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dan Goodman Subject: Re: Turning an AST node / subnodes into something human-readable Date: Wed, 19 Feb 2014 17:40:14 +0000 (UTC) References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 24.61.22.174 (Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0) 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: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392831639 news.xs4all.nl 2877 [2001:888:2000:d::a6]:39071 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66707 Chris Angelico gmail.com> writes: > 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? I did something like this, feel free to use my code: https://github.com/brian-team/brian2/blob/master/brian2/parsing/rendering.py At the moment, it only works for series of mathematical statements (no control conditions, loops, array notation, etc.), but it would be pretty easy to add those. It also puts too many parentheses in outputted expressions, e.g. 1+2+3 would come back as (1+2)+3, etc. Dan