Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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; 'url:pypi': 0.03; 'output': 0.05; 'that?': 0.05; 'dependency': 0.09; 'sake': 0.09; 'subject:into': 0.09; 'type,': 0.09; 'cc:addr:python-list': 0.11; 'useful,': 0.14; 'ast': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hmm.': 0.16; 'node,': 0.16; 'readable': 0.16; 'repr': 0.16; 'wrote:': 0.18; 'library': 0.18; 'wed,': 0.18; 'bit': 0.19; 'seems': 0.21; 'feb': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'logical': 0.24; 'stick': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'ideal': 0.29; 'message-id:@mail.gmail.com': 0.30; 'node': 0.31; 'url:python': 0.33; 'maybe': 0.34; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'something': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'introducing': 0.36; 'method': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'starting': 0.37; 'pm,': 0.38; 'anything': 0.39; 'itself': 0.39; 'easy': 0.60; 'subject: / ': 0.60; 'most': 0.60; 'more': 0.64; 'hoping': 0.75; 'saw': 0.77; "it'd": 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=YaiYk78rWZUEJUWEB07GGhwqb/Fo+ILmwJuATRHHdGo=; b=U+AjcvxrgX3JvagdW7J0KqyEmpLAO9emGLTapPE2OuEdeeDgA15UkgOXtuNTK76pgj MXGZQIyPWyuROqMK+tF8lMEPApokf8VDrE+mZ0SY2wMHB1KZY55uM6uhhKyZgExxByja N97oUMN7EEVZaGUuhTA3ZW9dpA6OWhlXERfhnsyoLpjARdLrcK+TE2/xETLt5waWLG2a JR/iVCfr5affOkGWIyvsSIBn+3R33i1zgbMgeuDW4fJt2SiAcB1EjxTc5zLUyE6Nccd2 oRzoDqw0gesuOE8sX73X8sYhpdmUFC1zbDujSliwNZHE2R5bOVl/uHoed8F2gNdyF+Jb 76Vg== MIME-Version: 1.0 X-Received: by 10.68.108.194 with SMTP id hm2mr38570089pbb.22.1392799489045; Wed, 19 Feb 2014 00:44:49 -0800 (PST) In-Reply-To: <530465bd$0$2971$e4fe514c@news.xs4all.nl> References: <530465bd$0$2971$e4fe514c@news.xs4all.nl> Date: Wed, 19 Feb 2014 19:44:48 +1100 Subject: Re: Turning an AST node / subnodes into something human-readable From: Chris Angelico Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392799493 news.xs4all.nl 2952 [2001:888:2000:d::a6]:44368 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66675 On Wed, Feb 19, 2014 at 7:05 PM, Irmen de Jong 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