Calling C++ function from python script
The module which i am creating is like Part A: 1. It does some processing by using python code. 2. The result of this python code execution is written to a text file. [This part is already compelete]] Part B: 1. I read a text file which is outputted by above python script in a C++ program 2. and again output of this c++ code is text file [This part is as well complete with graph data structure construction part involved, which i feel can be done in better way in C++ than in python] Now i want to integrate this flow. The communication between part A and part B is by call to a function present in C++ How should i do that? Kindly suggest some ways. Regards Pankaj -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling C++ function from python script
Pankaj wrote: The module which i am creating is like Part A: 1. It does some processing by using python code. 2. The result of this python code execution is written to a text file. [This part is already compelete]] Part B: 1. I read a text file which is outputted by above python script in a C++ program 2. and again output of this c++ code is text file [This part is as well complete with graph data structure construction part involved, which i feel can be done in better way in C++ than in python] Now i want to integrate this flow. The communication between part A and part B is by call to a function present in C++ How should i do that? Kindly suggest some ways. Regards Pankaj If the bulk of your code is in Python, write an extension module in C++. You can use Swig or Boost here. If the bulk of your code is in C++, embed Python. The general rule is extend, not embed Python documentation http://docs.python.org/ext/ext.html Unless you know C/C++ well, these are not easy topics. Boost http://www.boost.org/libs/python/doc/tutorial/doc/html/index.html Swig, SIP are other good alternatives. All the above are relatively easy. If your C++ can be simplified to C, Pyrex is remarkably easy to write extensions in. In this case you will import/include a C header containing your function and write a Pyrex wrapper around it rather than directly using Pyrex as a language. If you are on MS Windows, you can always write a COM server in either language (COM in C++ is not easy unless if you are using something like C++ Builder). Or you can simply use popen from Python or use pipes or just read stdout from either side. As you can see, there are a number of options. -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling C++ function from python script
In my case, i want to use python script as parent script and call C++ function from within the python script. If this is the case, then what am i doing? 1. extending 2. embedding. ? Ravi Teja wrote: Pankaj wrote: The module which i am creating is like Part A: 1. It does some processing by using python code. 2. The result of this python code execution is written to a text file. [This part is already compelete]] Part B: 1. I read a text file which is outputted by above python script in a C++ program 2. and again output of this c++ code is text file [This part is as well complete with graph data structure construction part involved, which i feel can be done in better way in C++ than in python] Now i want to integrate this flow. The communication between part A and part B is by call to a function present in C++ How should i do that? Kindly suggest some ways. Regards Pankaj If the bulk of your code is in Python, write an extension module in C++. You can use Swig or Boost here. If the bulk of your code is in C++, embed Python. The general rule is extend, not embed Python documentation http://docs.python.org/ext/ext.html Unless you know C/C++ well, these are not easy topics. Boost http://www.boost.org/libs/python/doc/tutorial/doc/html/index.html Swig, SIP are other good alternatives. All the above are relatively easy. If your C++ can be simplified to C, Pyrex is remarkably easy to write extensions in. In this case you will import/include a C header containing your function and write a Pyrex wrapper around it rather than directly using Pyrex as a language. If you are on MS Windows, you can always write a COM server in either language (COM in C++ is not easy unless if you are using something like C++ Builder). Or you can simply use popen from Python or use pipes or just read stdout from either side. As you can see, there are a number of options. -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling C++ function from python script
Extending -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling C++ function from python script
The examples given are too complicated. So, if it can be explained using my sample example. Would be thankful for that. /* 1.c File **/ func( char a[10] ) { int i; for( i =0; i 10; i++) printf(\n array element is: %c, a[i]); } /* 1.py File **/ f = open( x.txt, r) while 1: l = f.readline( ) = Here i want to call func() as func( l) -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling C++ function from python script
Right. They are complicated(Assuming that you are reading Python API docs), especially if you have not done a substantial amount of C. You spent 8 minutes trying to understand them. What did you expect? People spend days sorting out the API issues. If you are in a hurry, Pyrex is easiest approch to write Python extensions. Try that instead. http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ Another approach... create a dll and call it from ctypes. Nothing to learn (assuming you understand C calling conventions) -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling C++ function from python script
See, i tell u, nothing is difficult in this world. I achieved the thing which i was asking for. Thanks for the advice. I used this paper: http://www.swig.org/tutorial.html for creating python modules from C code. Which is what i needed. In order to interface both things, we need convert atleast one thing to other so i choosed to convert C code to python module. The steps which i followed were: These steps were to create a python module from C code. So a mandatory condition for .c file is : it should not have main function, and any variable in any function called from main function should be declared global. 1. Creating wrapper from .i : swig -python TestCase.i (where TestCase.i is interface file containing declarations of functions and variables) 2. Creating .o's : gcc -c TestCase.c TestCase_wrap.c (TestCase_wrap.c is file genereted by swig and is a wrapper for creating a python module) 3. Shared Library: ld -shared TestCase.o TestCase_wrap.o TestCase.so Module was inserted as: 1. export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH 2. python Tc.py ( In this python file i did: import TestCase and then used it as TestCase.main_module() ** So, we don't need to write any code to do this. -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling C++ function from python script
One small thing was incorrect there. gcc -c TestCase.c TestCase_wrap.c -I /usr/include/python2.2/ This include path is required for Python.h file -- http://mail.python.org/mailman/listinfo/python-list