Mixing python and C++ via std in and std out - c++

I have a problem where it is beneficial for me to be able to mix python code and C++ code, and I think that the task is simple enough that it could be done by simply initializing the C++ program from python, and then having the C++ program "wait" for python to give it some input via std in, and then have python "wait" for the C++ program do its computation and return it via std out etc.
I feel like this is either trivial or extremely extremely hard. My main problem is that each time I initialize the C++ code it takes an extremely long time, but that would only need to be done once if I can get this idea implemented. Any thoughts?

Look at the the Submodule library. You can use Submodule.popen() to create a process from python, using stdin=PIPE and stdout=PIPE. You can then read from the C++ program's stdout and write to its stdin.

Sounds like SWIG might be what you're looking for. Use it to generate an extension module for Python, then call your C++ methods from a Python script.

Related

Calling a C++ Functions through a Python Script

I have a scenario where I have some functions in C++ classes and I want to be able to call them using a python script. Let's say I have a function
void greet(_msg);
std::cout >> _msg >> std::endl;
I want to be able to call it trough a custom Python call and pass arguments to it, for example using
saySomething("Hello")
As a .py file I want it to call the greet function and pass "Hello" as an argument.
I know it's a subject that has been throughly discussed, and I've done a share of research on embedding python in C++, I've managed to read values from a python script using the standard Python/C API and run a function in Python from C++ and pass argument to it, but I can't seem to get my head around how to achieve this specific outcome.
I've had a look at ctypes and various wrappin libraries such as boost:python or swig, but I can't seem to understand to which degree they could help me achieve want I want.
Depending on which version of Python you are interested in, 2.x or 3.x,
read through the Extending and Embedding the Python Interpreter chapter for 2.x or 3.x. You are interested only in extending Python, so section the 1. Extending Python with C or C++ will provide you with complete explanation how to implement what you need in order to be able to call your functions implemented in C++ from Python script.
Certainly, there are numerous libraries and generators which allow you to wrap C/C++ APIs for Python (e.g. Boost.Python or SWIG), but your case sounds simple enough, that for the purpose of learning it is IMO better to get familiar with Python C API. Even if you use these tools, you will frequently have to get down to Python C API anyway or at least understand it.
I recently needed to do this very thing. Boost.Python does what we're looking for (and more) but personally (as much as I love Boost) I find it a little overkill to have to drag in half the Boost library to get one feature. SWIG also wasn't really an option for me as code generation always becomes a pain to maintain while class structures change (Don't get me wrong, these are BRILLIANT solutions!, just not what I was looking for).
So, the only thing left for me was to implement it from first principles (Python/C API). Hense, "ECS:Python" was born. ECS:Python (Embedded C++ Scripting with Python) is a simple C++ Python wrapper library I designed specifically for C++ developers. It allows you to expose objects from a C++ application to an embedded Python interpreter for interactive scripting, and it's very light-weight and easy to use.
Its free (BSD) and open source. If you're interested here it is:
http://sourceforge.net/projects/ecspython
You can use the weave.inline() function, which is part of the scipy package, to compile and execute C/C++ files and get their output from within your python script.

Tcl/Tk GUI interface for C++ library

I have a C++ library that we're using to override some functions for testing. However, we just have it set up to prompt from the command line.
I'm looking to create a GUI for it to use as the prompt rather than command line.
I've been looking into Tcl/Tk, but I'm not quite sure if it can do what I'd like. Is it possible to use the Tcl/Tk wrapper to stylize my library functions?
Can I just include Tcl/Tk in my C++ code somehow, so that I call those functions right inside my library?
Probably easiest to wrap your C++ functions with swig and call them from Tcl, see this intro, then you can use Tk to create a GUI for your input parameters and/or displaying your results.
The two mechanisms to consider for this are SWIG and critcl in C++ mode. The former is probably easier to get going with as you already have the C++ code, and the latter produces more natural (more “Tcl-ish”) language embeddings.
Once you've got your library connected up, the first thing to do is probably to write a little test suite (using tcltest, a standard package supplied with Tcl) so that you know that things are working. (That saves a lot of heartache and hair-tearing later on!) If your code is working fine, you'll probably have a good enough test suite within a day or two. Then hook it up to your GUI (Tk is indeed good for that), which can be written safe in the knowledge that it's using a business logic layer that's working fine. I encourage you to avoid putting any GUI code in your C++ code if you can; it's far better to produce a clean interface without entanglements. (OK, it's not always possible to avoid, especially if you're doing heavy visualization, but it's a lot more work…)

running c++ code from python

I want to execute a code helloword.cpp which takes in some argument from console parses those arguments and then prints "hello world" in the console.
Now, I want to parse these arguments from a python scripts parsearguments.py
So for example:
def parse_arguments:
...# some code
return arguments
Now, how do i communicate between python and c++.
I have been reading and see that cython, boost python are the options but I have a hard time finding the right simple hello world example.
Any suggestions will be appreciated.
Thanks
To execute C++ code in python, you could effectively use boost python, here is a tutorial:
http://www.boost.org/doc/libs/1_59_0/libs/python/doc/index.html
You write a kind of wrapper outside you C++ code.
If it is C code, python has internal library called ctypes.
In both case, you should compile the C/C++ code into shared library.
How about passing whatever text you generate with Python into the standard input of your C++ program? Basically, you have to use Python's subprocess module to fire up the C++ program and dump the text into its standard output.
In case that your C++ program is required to be running separately in the background, you could try another form of interprocess communication, like unix domain sockets.
Using boost::python is also an option, but it might be a little more difficult to deal with.
A couple of other options besides Boost.python are SIP and SWIG (Simplified Wrapper and Interface Generator). Like Boost, SIP and SWIG are open source.
SWIG is particularly powerful, but also a bit hairy. It provides support for interfacing C and C++ with a boatload of other languages including (not a complete list) Python, Perl, Lua, Tcl/Tk, Ocaml, Ruby, Java. One aspect of SWIG is that it parses your C++ headers. This has benefits and pitfalls. A benefit is that it does most of the work of generating the interfaces. A downside is that it doesn't handle some of the dark corners of C++ 2003, and it hasn't stepped up to C++11 at all. Another downside is that compilation of a large project becomes slow. Very, very slow.
Using boost.python sounds like a good solution for me. But depending on your C++ experience this can be quite tricky. A good point to start is here:
http://wiki.python.org/moin/boost.python
Boost.Python enables you to export C++ classes and member functions to Python to be able to use them from there.

Wrapping C++ OpenCV code for Python

I have a demo application that is written in Python. It uses a lot of existing C++ code (written by me) that relies on OpenCV for image processing. Currently, communication between Python and C++ is being done through file I/O and subprocess calls, which isn't very efficient. What is the best way to wrap the C++ code so that it can be called from Python?
There is too much C++ code to think about porting it to Python, so that's not really an option.
A long time ago, the Python OpenCV wrappers were written in SWIG, but it looks like the most recent version of the wrappers is completely different. Can anyone point me in the right direction?
There are two ways that you can make your python program interact directly with your C/C++ program:
Wrap your C/C++ code in a DLL with C-API only. Then, use ctypes
to call C-function within the DLL. The advantage of this way is that you don't need to include/link any other library.
Extend python by adding new python module. You may use boost python to easily create a python module. The advantage of this way is that you don't need to wrap your code to C-API.
Without knowing code complexity, variety of C++ code and style of it... I would recommend "Extending Python"
It's not an immediate solution (you should change the C++ code, prototype some new functions or add a simple wrapper layer in C). But, if you plan to do a complex project (and are also a bit worried on performance)... it seems the best way to do it.
Porting C++ code to Python seems a step backwards, doing new code in Python is ok (I'm a fan of it) but C++ will (often) be more efficient.
Edit: Take also a look on ctypes module. Maybe it suits your needs. If you are more comfortable doing the wrapping in python language, then it may be better. If you don't mind playing with the C code, then extend Python by doing a module with your existing code.

Testing a C++ library with Python

I have a libfoo.so library built from C++ code (compiled with gcc), and I would like to quickly test some of its exported classes (basically, instantiating a class then calling its methods to check the output).
While I could do that in C/C++ with a main file that links to the library in question and build my tests, but I think it would be much easier if it was possible to simply call Python from the command line and call the methods from there.
I know I can use CDLL from ctypes to load C-style libraries, but is there a similar functionality for C++ libraries and objects?
EDIT : Ideally, I do not want to modify the C++ code, I would need to use it as-is.
Honestly C++ is a bit messy. You could do something like create a pure C function which wraps the C++ functionality (which you then call from python) but at that point you might as well write your tests in C++. Unfortunately the only tool out there for this (that I know of) is SWIG.
It's sad that it's called the "simplified" wrapper and interface generator, because there's nothing simple about it. If you have VERY primitive data types in your signatures (like, JUST ints or maybe char*) it'll be a pretty quick job. Otherwise you have to tell swig how to marshal your data types between languages, and that gets REALLY ugly very quickly. Furthermore, after short time you realize you have to learn the CPython API in order to write your marshalling code.
And by that point you may as well write your own CPython wrapper without involving SWIG. You suddenly realize you've spent a good month learning a new API and feel horribly frustrated. If you're going to be doing this a lot, it's definitely worth your time. However if this is a one-time thing, just write your tests in C / C++.
(I'm speaking from experience here)
I agree with Chris answer. However, I want to point out that Cython supports C++ (with some limitations).