Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'yet.': 0.04; '"""': 0.07; "'',": 0.07; '[0,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'posted': 0.15; 'ast': 0.16; 'behaviour.': 0.16; 'overriding': 0.16; 'subject:remove': 0.16; 'there?': 0.16; 'to:addr:web.de': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'replace': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'source': 0.25; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'leave': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'went': 0.31; "skip:' 10": 0.31; 'along.': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'url:python': 0.33; 'maybe': 0.34; 'tool': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'url:listinfo': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'too': 0.37; 'list': 0.37; 'skip:& 10': 0.38; 'generic': 0.38; 'issue': 0.38; 'mailing': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'further': 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'total': 0.65; 'here': 0.66; 'otten': 0.84; 'subject:skip:N 10': 0.84; 'responses': 0.93 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:to :cc:content-type; bh=hPX9rUeNF7axA7MiYvTlV4ph1Gvfd2JjiUeQ7Ear4+0=; b=i2f3BHGeefX9i1NyJZeEryz8oNNE2L/KJz/grz855ec7AJGHjnmoVjnT95NRB/B7Dy mSqxx5+TikFZBglZkOAMx0X0KQRWRL0F4Rqhk+jYI3OoP+AXw9XqHz6zdYVemxw4P9C+ LcaDMy8058sRMigfxlLiGJBgMUgD55VX8A9yAte1bs7GcMWWGWocVhwTdZOQ71F+3ImR wzVScnmCX1XEMLXhOz3GIp8gQzamN8Lnye1ah+qchPQIvI3zkS45yLDP3O/C9qI6BHNZ kfFybwG2XTqNwM091hejDnWOon5Ta5Om4vFA14INLU9tUJ7ecQUr7yu7l4z+5jGisH6h 0S7Q== MIME-Version: 1.0 X-Received: by 10.15.35.196 with SMTP id g44mr11088438eev.18.1377096399901; Wed, 21 Aug 2013 07:46:39 -0700 (PDT) In-Reply-To: References: Date: Wed, 21 Aug 2013 16:46:39 +0200 Subject: Re: NodeTransformer: how to remove nodes? From: =?ISO-8859-1?Q?Tobias_M=FCller?= To: Peter Otten <__peter__@web.de> Content-Type: multipart/alternative; boundary=089e0160c1a0bcf82b04e4763dc3 X-Mailman-Approved-At: Wed, 21 Aug 2013 22:48:19 +0200 Cc: python-list@python.org 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: 133 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377118100 news.xs4all.nl 15939 [2001:888:2000:d::a6]:48687 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52796 --089e0160c1a0bcf82b04e4763dc3 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thanks Chris and Peter. I already went further along. No more issues so far. 2013/8/19 Peter Otten <__peter__@web.de> > Tobias M=FCller wrote: > > > I'm facing an issue with NodeTransformer, a tool used for Python AST > > manipulations. > > > > Last week I posted on stackoverflow.com, but there are no responses yet= . > > Maybe someone reading the mailing list can have a look and leave me a > > response here or over there? > > > > > http://stackoverflow.com/questions/18275662/python-nodetransformer-how-to= - > remove-nodes > > As Chris says, you are overriding too much of the generic behaviour. Here > is > a working demo: > > import ast > > class MyTransformer(ast.NodeTransformer): > def visit_For(self, node): > """ > For nodes: replace with nothing > """ > print("removing a For node") > return None > > > source =3D """ > l =3D [0, 1, 2, 3] > > total =3D 0 > > for i in l: > total +=3D i > > print(total) > """ > > transformer =3D MyTransformer() > module =3D ast.parse(source) > transformer.visit(module) > codeobj =3D compile(module, '', 'exec') > exec(codeobj) > > > -- > http://mail.python.org/mailman/listinfo/python-list > --089e0160c1a0bcf82b04e4763dc3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

Thanks Chris and Peter.=A0

I already went further along. No more issues so far.=A0



2013/8/19 Peter Otten <__peter__@web.de>
Tobias M=FCller wrote:

> I'm facing an issue with NodeTransformer, a tool used for Python A= ST
> manipulations.
>
> Last week I posted on stackoverflow.com, but there are no responses yet.
> Maybe someone reading the mailing list can have a look and leave me a<= br> > response here or over there?
>
> http://stackoverflow.com/questions/18275662= /python-nodetransformer-how-to-
remove-nodes


As Chris says, you are overriding too much of the generic behav= iour. Here is
a working demo:

import ast

class MyTransformer(ast.NodeTransformer):
=A0 =A0 def visit_For(self, node):
=A0 =A0 =A0 =A0 """
=A0 =A0 =A0 =A0 For nodes: replace with nothing
=A0 =A0 =A0 =A0 """
=A0 =A0 =A0 =A0 print("removing a For node")
=A0 =A0 =A0 =A0 return None


source =3D """
l =3D [0, 1, 2, 3]

total =3D 0

for i in l:
=A0 =A0 total +=3D i

print(total)
"""

transformer =3D MyTransformer()
module =3D ast.parse(source)
transformer.visit(module)
codeobj =3D compile(module, '<string>', 'exec')
exec(codeobj)


--
http://mail.python.org/mailman/listinfo/python-list

--089e0160c1a0bcf82b04e4763dc3--