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


Groups > comp.lang.python > #52796

Re: NodeTransformer: how to remove nodes?

References <CAM5eX4D1KB8dVgsR=6ZPjcuhwehN4UR0YQtvNgJ+m-Acs1VoDg@mail.gmail.com> <kusv2c$q1f$1@ger.gmane.org>
Date 2013-08-21 16:46 +0200
Subject Re: NodeTransformer: how to remove nodes?
From Tobias Müller <to.mueller13@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.106.1377118100.19984.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

Thanks Chris and Peter.

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



2013/8/19 Peter Otten <__peter__@web.de>

> 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)
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Re: NodeTransformer: how to remove nodes? Tobias Müller <to.mueller13@gmail.com> - 2013-08-21 16:46 +0200

csiph-web