From the c-api, I would like to call a python function by name. I would then be calling the function with a list of python objects as arguments.
It is not clear to me in the Python documentation how I would get a "Callable" object from the main python interpreter.
Any help appreciated in:
Getting the address from the function
Calling the function with my PythonObject's as arguments.
I'm using Python 2.x series for my development.
Basically, you use the Python C API to get the module the function is contained in, then query the module dictionary for the function. That's more or less the same what the Python runtime does internally when your Python code invokes a function from somewhere.
Relevant functions from the API to look at are PyImport_ImportModule, PyModule_GetDict, PyDict_GetItem and the PyObject_CallXXX family of functions.
Related
I am facing some issue with python to java.
I have my python library (test_lib) which have functions (addition, multiply) now I want to use test_lib function in java program but without using (test_lib) in java program. I don't want to show or include function definition in java program. I know if I add python file that have a function I can call in java program but then the user knows the function definition.
Has anyone ever tried python to java. Like with a high level approach?
Thank you.
I'm trying to extract all the "runnable" code given a function in a module. Right now I'm only able to extract the functions in any imported module that are called inside the starting function. However, some modules have "outside" expressions (i.e. some variables defined globally in the module, or functions called in the same level).
With inspect and dis I did the work to extract the functions, but, is there any way of extracting the "non-function" of a module?
If anybody wonders what am I doing, is a packer for python. What I want to achieve is that this tools only packs the required code given a starting function.
Also, if is there something already out there that does what I'm trying to achieve, I'd like to know.
importing a module as a code object
You can use importlib's get_code() method which returns the code object of the module and then you can modify or create a new code object extracting the required parts. exec method can be used for executing code object.
Alternatively, using the built-in compile function, you can directly compile source code into byte code then follow the same procedure as mentioned above.
References:
Modifying python bytecode
assembling-python-module-on-fly-dynamic-import
I'm trying to use the scipy.optimize.differential_evolution (python) from MATLAB environment.
So far, I am able to call the differential_evolution function. The only problem is that it apprently cannot receive MATLAB function handles as an argument.
I get the following error:
Error using py.scipy.optimize.differential_evolution Handle to MATLAB
function '#(x)x(1).^2.*x(2).^2' is not supported. Use a handle to a
Python function.
Is there some neat way or function to "convert" a MATLAB function handle into a python function, so that I could use the neat optimization function from scipy?
I suspect that what you want can't directly be done.
Firstly, anything in the python interface will give you that error if you pass it a MATLAB anonymous function:
>> py.print(#(x) x)
Error using py.print
Handle to MATLAB function '#(x)x' is not supported. Use a handle to a Python function.
This very strongly suggests that once you have a MATLAB anonymous function you can't pass it to python.
Also note that while the lack of MATLAB function handle support is not explicitly mentioned among the limitations, the section of the documentation detailing supported data types makes this remark:
MATLAB Input Argument Type — Scalar Values Only
function handle #py.module.function, to Python functions only
The distinction for Python functions is in line with the fact that even the simplest Python functions refuse to accept MATLAB function handles.
We could try converting your MATLAB anonymous function to a python one, but I have to note upfront that it's messy and I'd avoid doing this if I were you. Since lambdas aren't directly exposed by MATLAB's Python API:
>> py.lambda
Unable to resolve the name py.lambda.
we have to resort to calling Python's eval using a python lambda (because exec also doesn't seem to be exposed):
>> py_f = py.eval('lambda x: x**2', py.dict());
>> py_f(3)
ans =
9
(Kudos to #yuyichao for fixing this snippet of mine by pointing out the missing globals dict that needs to be passed.)
However, a straightforward question is: do you really need a MATLAB anonymous function? You could just as well use a proper python function (or lambda) and pass possible other arguments to the underlying scipy.optimize function as args. You could define your custom function in a python file and import that from MATLAB and use the corresponding function handle. This would probably be the straightforward way.
I need to test the feasibility of calling Python member functions (running in one process)from within C++. This is for testing interfacing C++ to an existing Python application. I need to minimize the modifications to the Python code as that's run by a separate team. Therefore I do not have control of when the Python objects are created on the C++ side. For my test I'd like to try and:
See if I can determine how many instances of a specified Python class have been created
If that number is > 0, then I would like to test calling a member function on one of the instantiated Python objects from C++
I can do a simple call from C++ to a global, non member Python function, but can't figure out how to do the above 2 steps from the C++ side.
I'd also like to try and do this without pulling in the Boost Python interop. library (but will if that's the only way this can be achieved).
Thanks if anyone can advise.
I am creating a method object from string using "exec()". when I do dir(), I am able to see that the methods have been created in the current module. I need the method objects directly from exec() since there are lot of methods with same names which are currently getting replaced.