Can I call Python-functions from """code"""?
Is there a way to cll my self-defined Python-functions from the embeded C++ code?? And if so, how??? Andreas Lorenz
"AL" == Andreas Lorenz <andreas.frankfurt@gmx.de> writes:
AL> Is there a way to cll my self-defined Python-functions from AL> the embeded C++ code?? Well, that is not possible currently and even if it were it would apparently be quite hard (or painful) to do. Of course, Eric will correct me if I am wrong about this. prabhu
Hey Andreas,
Is there a way to cll my self-defined Python-functions from the embeded C++ code??
You can. Since code is all C++ code, and your wanting to call a Python function, you have to do two things: 1. Pass the function you want to call as an argument to inline. 2. Call the function using the Python API. Take note that the call back into Python exacts a large performance toll, so you probably won't see much speed up.
And if so, how???
Here is an example from weave/examples/functional.py that is similar to Python's builtin map(). There is a 2nd "prettier" version in functional.py, but it is half the speed of the standard map() function. def c_list_map2(func,seq): """ Uses Python API more than CXX to implement a simple map-like function. It does not provide any error checking. """ code = """ Py::Tuple args(1); Py::List result(seq.length()); PyObject* item = NULL; PyObject* this_result = NULL; for(int i = 0; i < seq.length();i++) { item = PyList_GET_ITEM(seq.ptr(),i); PyTuple_SetItem(args.ptr(),0,item); this_result = PyEval_CallObject(func,args.ptr()); PyList_SetItem(result.ptr(),i,this_result); } return_val = Py::new_reference_to(result); """ return inline_tools.inline(code,['func','seq']) seq = ['aadasdf'] * 5 result = c_list_map2(len,seq) If you can re-write the function you need to call as a C function, then you can call it directly in C to get the speed back. There are many cases where this isn't feasible, but some where it is. Use the support_code key variable for this. I think there are some examples of this in weave/examples/fibonacci.py Hope that helps, eric
"eric" == "eric" <eric@scipy.org> writes: eric> Hey Andreas,
>> Is there a way to cll my self-defined Python-functions from the >> = eric> embeded C++=20 >> code?? eric> You can. Since code is all C++ code, and your wanting to eric> call a Python = function, you have to do two things: Umm, sorry folks for misleading you all. I must have misunderstood something when I talked with Eric. My bad. But I sure am glad that we can do this. It opens up so many possibilities. :) Thanks Eric! prabhu
No prob. The mechanism for doing it isn't as transparent or obvious as I'd like. Still, possible is better than impossible... eric. ----- Original Message ----- From: "Prabhu Ramachandran" <prabhu@aero.iitm.ernet.in> To: <scipy-dev@scipy.org> Sent: Thursday, January 24, 2002 9:50 PM Subject: Re: [SciPy-dev] Can I call Python-functions from """code"""?
"eric" == "eric" <eric@scipy.org> writes: eric> Hey Andreas,
>> Is there a way to cll my self-defined Python-functions from the >> = eric> embeded C++=20 >> code??
eric> You can. Since code is all C++ code, and your wanting to eric> call a Python = function, you have to do two things:
Umm, sorry folks for misleading you all. I must have misunderstood something when I talked with Eric. My bad. But I sure am glad that we can do this. It opens up so many possibilities. :)
Thanks Eric!
prabhu _______________________________________________ Scipy-dev mailing list Scipy-dev@scipy.net http://www.scipy.net/mailman/listinfo/scipy-dev
participants (3)
-
Andreas Lorenz
-
eric
-
Prabhu Ramachandran