how to compile this code

meInvent bbird jobmattcon at gmail.com
Tue Nov 1 02:32:34 EDT 2016


would like to change Add operator to custom function op2 in solve function
and then this solve([x*y - 1, x + 2], x, y)
during solve, the parameters also change Add to custom function op2

import ast 
from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
import inspect

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): 
        print(dir(node))
        print(dir(node.left))
        if isinstance(node.op, ast.Add): 
            node.op = op2(node.left, node.right)
        return node 

code = inspect.getsourcelines(solve)
tree = ast.parse(code)
tree = ChangeAddToMultiply().visit(tree) 
ast.fix_missing_locations(tree) 
co = compile(tree, '<ast>', "exec") 

exec(code) 
exec(co) 



More information about the Python-list mailing list