Re: how to evaluate a ast tree and change Add to Multiply ?

2016-10-25 Thread meInvent bbird
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):

Re: how to evaluate a ast tree and change Add to Multiply ?

2016-10-19 Thread Jerry Hill
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

Re: how to evaluate a ast tree and change Add to Multiply ?

2016-10-12 Thread meInvent bbird
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=[

Re: how to evaluate a ast tree and change Add to Multiply ?

2016-10-12 Thread meInvent bbird
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

Re: how to evaluate a ast tree and change Add to Multiply ?

2016-10-12 Thread Chris Angelico
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()

how to evaluate a ast tree and change Add to Multiply ?

2016-10-12 Thread meInvent bbird
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