Hi Jerry,
how about custom function?
i change to node.op = ast.op2()
import ast
def op2(a,b):
return a*b+a
class ChangeAddToMultiply(ast.NodeTransformer):
"""Wraps all integers in a call to Integer()"""
def visit_BinOp(self, node):
if isinstance(node.op, ast.Add):
On Wed, Oct 12, 2016 at 5:55 AM, meInvent bbird wrote:
> i just expect to
> rewrite + become multiply
> by edit the example in the link provided
This seems to work. You need to define visit_BinOp instead of
visit_Num (since you want to mess with the binary operations, not the
numbers). Then,in
i just expect to
rewrite + become multiply
by edit the example in the link provided
but search no examples about this,
feel unknown about args and keywords etc,
do not know how to write this
ast.Call(func=ast.Name(id='Add', ctx=ast.Load()),
args=[node], keywords=[
i use example here
http://greentreesnakes.readthedocs.io/en/latest/examples.html
On Wednesday, October 12, 2016 at 5:47:12 PM UTC+8, Chris Angelico wrote:
> On Wed, Oct 12, 2016 at 8:32 PM, meInvent bbird wrote:
> > class ChangeAddtoMultiply(ast.NodeTransformer):
> > """Wraps all intege
On Wed, Oct 12, 2016 at 8:32 PM, meInvent bbird wrote:
> class ChangeAddtoMultiply(ast.NodeTransformer):
> """Wraps all integers in a call to Integer()"""
> def visit_Num(self, node):
> if isinstance(node.n, int):
> return ast.Call(func=ast.Name(id='Add', ctx=ast.Load()
import sys
import parser
import ast
from collections import deque
class ChangeAddtoMultiply(ast.NodeTransformer):
"""Wraps all integers in a call to Integer()"""
def visit_Num(self, node):
if isinstance(node.n, int):
return ast.Call(func=ast.Name(id='Add', ctx=ast.Load