Pass data from Python to C++

2008-05-15 Thread brad
I have some c++ binaries that do rather intense number computations. 
They do it well and rather quickly compared to other languages (not just 
Python). An example:



[EMAIL PROTECTED]:~/$ date  ./compute.cpp.o  1_million.txt  /dev/null  
date
Thu May 15 13:08:28 EDT 2008
Thu May 15 13:08:31 EDT 2008
[EMAIL PROTECTED]:~/$ date  python compute.py  1_million.txt  /dev/null  
date
Thu May 15 13:08:38 EDT 2008
Thu May 15 13:14:50 EDT 2008


In this case, c++ does one million things in 3 seconds that Python takes 
more than 6 minutes to do. The one million is a minimum. At times the 
computations are in the billions. This is why c++ was chosen.


However, other components can be written in a more user friendly, more 
easily maintained language. We've chosen Python for this. The main 
question now is how to pass the computationally heavy info to c++ from 
within Pyhton. os.system is not ideal. Just wondering how other folks do 
this? I have source to some of the c++ code, but some of it is in binary 
from only. It can take stdin or arguments.


Thanks for any tips,

Brad

--
http://mail.python.org/mailman/listinfo/python-list


Re: Pass data from Python to C++

2008-05-15 Thread Gary Herron

brad wrote:
I have some c++ binaries that do rather intense number computations. 
They do it well and rather quickly compared to other languages (not 
just Python). An example:



[EMAIL PROTECTED]:~/$ date  ./compute.cpp.o  1_million.txt  /dev/null  
date
Thu May 15 13:08:28 EDT 2008
Thu May 15 13:08:31 EDT 2008
[EMAIL PROTECTED]:~/$ date  python compute.py  1_million.txt  /dev/null  
date

Thu May 15 13:08:38 EDT 2008
Thu May 15 13:14:50 EDT 2008


In this case, c++ does one million things in 3 seconds that Python 
takes more than 6 minutes to do. The one million is a minimum. At 
times the computations are in the billions. This is why c++ was chosen.


However, other components can be written in a more user friendly, more 
easily maintained language. We've chosen Python for this. The main 
question now is how to pass the computationally heavy info to c++ from 
within Pyhton. os.system is not ideal. Just wondering how other folks 
do this? I have source to some of the c++ code, but some of it is in 
binary from only. It can take stdin or arguments.


Thanks for any tips,

Brad

--
http://mail.python.org/mailman/listinfo/python-list



There are lots of ways to do this.Lots of the modules you use are 
written in C and callable from Python. (Including sys, os, socket, PIL, 
numpy, all graphics and GUI modules, ...)  And that's exactly what you 
want here -- a module that you can import into Python which gives you 
the ability to make calls into your C++ code.   This is often called 
*wrapping* your C++ library.


This is no small task, and it depends heavily on the size/complexity of 
the API you wish to wrap, and whether it's C (easier) or C++(harder).   
However, there are *lots* of tools to help.  I'd start by looking here:

 http://wiki.python.org/moin/AdvocacyWritingTasks/GlueLanguage

Good luck,

Gary Herron


--
http://mail.python.org/mailman/listinfo/python-list


Re: Pass data from Python to C++

2008-05-15 Thread Russell E. Owen
In article [EMAIL PROTECTED], brad [EMAIL PROTECTED] 
wrote:

 I have some c++ binaries that do rather intense number computations. 
 They do it well and rather quickly compared to other languages (not just 
 Python). ...
 
 However, other components can be written in a more user friendly, more 
 easily maintained language. We've chosen Python for this. The main 
 question now is how to pass the computationally heavy info to c++ from 
 within Pyhton. os.system is not ideal. Just wondering how other folks do 
 this? I have source to some of the c++ code, but some of it is in binary 
 from only. It can take stdin or arguments.

You say you have binary only, but I hope you have the header files as 
well. If so I would try SWIG first since it is mature and handles C++ 
quite well. If you don't have headers then I have no idea if it's even 
possible.

(For plain C I would start with ctypes, but that doesn't cover this 
case. There are many other options including boost and Pyrex, but I've 
not used those.)

-- Russell
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pass data from Python to C++

2008-05-15 Thread Christian Heimes
brad schrieb:
 However, other components can be written in a more user friendly, more
 easily maintained language. We've chosen Python for this. The main
 question now is how to pass the computationally heavy info to c++ from
 within Pyhton. os.system is not ideal. Just wondering how other folks do
 this? I have source to some of the c++ code, but some of it is in binary
 from only. It can take stdin or arguments.


You can either embed the C++ system as a shared library in Python or you
can embed the Python interpreter in your C++ app. Either way you should
use Python's buffer interface to access the data of your library. The
buffer interfaces allows you to create a read only or read/write view on
your data without copying the data.

Christian

--
http://mail.python.org/mailman/listinfo/python-list