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


Groups > comp.lang.python > #52686 > unrolled thread

Re: NodeTransformer: how to remove nodes?

Started byPeter Otten <__peter__@web.de>
First post2013-08-19 13:21 +0200
Last post2013-08-19 13:21 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#52686 — Re: NodeTransformer: how to remove nodes?

FromPeter Otten <__peter__@web.de>
Date2013-08-19 13:21 +0200
SubjectRe: NodeTransformer: how to remove nodes?
Message-ID<mailman.25.1376911264.19984.python-list@python.org>
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)

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web