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


Groups > comp.lang.python > #52686

Re: NodeTransformer: how to remove nodes?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'yet.': 0.04; '"""': 0.07; "'',": 0.07; '[0,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'posted': 0.15; 'ast': 0.16; 'behaviour.': 0.16; 'm\xc3\xbcller': 0.16; 'overriding': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:remove': 0.16; 'there?': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'source': 0.25; 'header:X-Complaints-To:1': 0.27; 'chris': 0.29; 'leave': 0.29; "i'm": 0.30; 'class': 0.32; 'skip:m 30': 0.32; 'maybe': 0.34; 'tool': 0.35; 'but': 0.35; 'there': 0.35; 'subject:?': 0.36; 'too': 0.37; 'list': 0.37; 'generic': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'mailing': 0.39; 'received:org': 0.40; 'skip:n 10': 0.64; 'total': 0.65; 'here': 0.66; 'subject:skip:N 10': 0.84; 'responses': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: NodeTransformer: how to remove nodes?
Date Mon, 19 Aug 2013 13:21:51 +0200
Organization None
References <CAM5eX4D1KB8dVgsR=6ZPjcuhwehN4UR0YQtvNgJ+m-Acs1VoDg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p5084b677.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.25.1376911264.19984.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376911264 news.xs4all.nl 15968 [2001:888:2000:d::a6]:38110
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52686

Show key headers only | View raw


Tobias Müller 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 = """
l = [0, 1, 2, 3]

total = 0

for i in l:
    total += i

print(total)
"""

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

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


Thread

Re: NodeTransformer: how to remove nodes? Peter Otten <__peter__@web.de> - 2013-08-19 13:21 +0200

csiph-web