how to compile this code
Yann Kaiser
kaiser.yann at gmail.com
Tue Nov 1 09:04:16 EDT 2016
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