how to compile this code

meInvent bbird jobmattcon at gmail.com
Tue Nov 1 21:13:55 EDT 2016


>>> class ChangeAddToMultiply(ast.NodeTransformer, ast2.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):
...             ast.Call(Name(id="op2", ctx=ast2.Load()), [node.left, node.right
], [])
...         return node
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ast2' is not defined
>>>
>>> code = inspect.getsourcelines(solve)
>>> tree = ast.parse(code)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\ast.py", line 37, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
TypeError: expected a readable buffer object
>>> tree2 = ast.parse("def op2(a,b): return a*b+a")
>>> tree = ChangeAddToMultiply().visit(tree,tree2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'tree' is not defined
>>> ast.fix_missing_locations(tree)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'tree' is not defined
>>> co = compile(tree, '<ast>', "exec")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'tree' is not defined
>>>
>>> exec(code)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: exec: arg 1 must be a string, file, or code object
>>> exec(co)

*********************************************

def op2(a,b): 
    return a*b+a 

class ChangeAddToMultiply(ast.NodeTransformer, ast2.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): 
            ast.Call(Name(id="op2", ctx=ast2.Load()), [node.left, node.right], []) 
        return node 


code = inspect.getsourcelines(solve) 
tree = ast.parse(code) 
tree2 = ast.parse("def op2(a,b): return a*b+a")
tree = ChangeAddToMultiply().visit(tree,tree2) 
ast.fix_missing_locations(tree) 
co = compile(tree, '<ast>', "exec") 

exec(code) 
exec(co) 



On Tuesday, November 1, 2016 at 9:04:43 PM UTC+8, Yann Kaiser wrote:
> You want to replace the `Add` ast with a `Call` ast rather than just
> calling your function.
> 
> Something like:
> 
>     if isinstance(node.op, ast.Add):
>         return ast.Call(some_ast_expression_that_will_evaluate_to_op,
> [node.left, node.right], [])
> 
> You'll have to replace some_ast_expression_... to something like
> Name(id="op2", ctx=ast.Load()) and inject op2 in the globals when you call
> `exec`.
> 
> On Tue, Nov 1, 2016, 06:36 meInvent bbird <jobmattcon at gmail.com> wrote:
> 
> > 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)
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> -- 
> Yann Kaiser
> kaiser.yann at gmail.com
> yann.kaiser at efrei.net
> +33 6 51 64 01 89
> https://github.com/epsy




More information about the Python-list mailing list