connecting c/c++ and python - c++

What I am trying to do is that I want to read a file using python, and then with the data in the file, create a variable in c/c++(I don't want to read var from the file :) ).
Is this possible?
If this is possible, then how would you do it?
Thank you guys!

Maybe Boost.Python can help.
You could expose a C++ function to your Python script. Something like that:
void do_sth_with_processed_data(const std::string& data)
{
// …
}
BOOST_PYTHON_MODULE(do_sth)
{
def("do_sth_with_processed_data", do_sth_with_processed_data);
}
In your Python script you now could have:
import do_sth
// …
do_sth_with_processed_data(my_processed_data) // this calls the c++ function

Yes. Open the first file in Python, process it and save the results to a second file.
Then open the second file in your C or C++ program and use the data.

Swig can generate a Python interface to C or C++ code for you automatically. Since it wraps constructors you could read the data in Python and then pass it (with a little care) to the constructor of a C++ class for example.

Related

How to add member to embedded class in run time by boost python?

I'm using boost python to bind c++ and python.
I want to make custom data type in run time.
For example this is excel file which include my new custom type infos.
Rectangle.xls
And i will read this excel file first and make new Rectangle type.
And i want to add this custom type to with c++ boost python in run time.
In c++ side, BOOST_PYTHON_MODULE macro will make functions.
BOOST_PYTHON_MODULE(HelloModule)
{
python::class_<Temp> temp;
temp.def(init<int>());
}
And i can bind like this.
PyImport_AppendInittab("HelloModule", &initHelloModule);
Boost python will bind this at run time but it can not edit at run time.
It can edit only when the coding time.
Is there have any boost python interface or way to make or import custom type in runtime?

Python command for Extraction of c++ object

Can someone please tell me python command for extraction of c++ objects in python. i am able to expose this by using boost python but do not know the access command in python. the sample code shows how to expsose c++ objects for python.
BOOST_PYTHON_MODULE(Test)
{
object x_class(
class_<CTest>("CTest"))
;
object x_obj = x_class();
CTest& test_obj = extract<CTest&>(x_obj);
}
Thanks for the Help.

expose C++ functions in python and embed python in C++

My app have some events, each event can have some actions. These actions is implemented in C++. I want to expose those core functions to python and use python to write the action. The advantage is I can modify actions without recompile. For example:
CppClass o;
// --- this is a action----
o.f1();
o.f2();
// ------------------------
use python to script the action:
def action1(o):
o.f1()
o.f2()
In c++, use interpreter to run this script, find action1 and call it with a PyObject which convert from c++ object. Actually, I have not expose f1() & f2() to python, I just use python to regroup the definition of action, all function is running by c++ binary code. Notice that I have not to give a definition of f1() & f2() in python.
The problem is: how I expose global functions? such as:
def action2():
gf1()
gf2()
boost::python can expose function, but it is different, it need compile a DLL file and the main() is belong to python script. Of course I can make global functions to be a class static member, but I just want to know. Notice that I HAVE TO give a definition of gf1() & gf2() in python.
Jython can do this easily: just import Xxx in python code and call Xxx.gf1() is ok. But in cython, how I define gf1() in python? This is a kind of extension, but extension requires Xxx be compiled ahead. It seems only way is make gf() into a class?
Solved. boost::python's doc is really poor...
For example: expose function
void ff(int x, int y)
{
std::cout<<x<<" "<<y<<std::endl;
}
to python:
import hello
def foo(x, y)
hello.ff(x, y)
you need to expose it as a module:
BOOST_PYTHON_MODULE(hello)
{
boost::python::def("ff", ff, boost::python::arg("x"), boost::python::arg("y"));
}
But this still is not a 'global function', so expose it to python's main scope:
BOOST_PYTHON_MODULE(__main__)
{
boost::python::def("ff", ff, boost::python::arg("x"), boost::python::arg("y"));
}
then you can write:
def foo(x, y)
ff(x, y)
You might also want to have a look at Cython.
Cython's main function is to translate (a subset of) Python code to C
or C++ code to build native code Python extensions. As a consequence,
it allows interfacing C/C++ code with a very terse and Python-ish
syntax.
Cython's user guide provides a
good example of how to call a simple C++ class from Python:
http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#declaring-a-c-class-interface
In addition to creating extensions, Cython can also generate C/C++
code that embeds the Python interpreter, so you do not need to build
and ship an external DLL. See details at: http://wiki.cython.org/EmbeddingCython
ffpython is c++ lib that wrap python 2.x api. see code repo
call python function: ffpython.call("fftest", "test_stl", a1, a2, a3);
reg c++ class:
ffpython.reg_class<foo_t, PYCTOR(int)>("foo_t")
.reg(&foo_t::get_value, "get_value")
.reg(&foo_t::set_value, "set_value")
.reg(&foo_t::test_stl, "test_stl")
.reg_property(&foo_t::m_value, "m_value");

pass callback from python to c++ using boost::python

I want to pass callback from my python code to c++
I want my code look something like this:
In C++ :
typedef void (*MyCallback_t) (CallbackInfo);
class MyClass
{...
void setcallback(MyCallback_t cb);
...
}
And to use it in python :
import mylib
def myCallback(mylib_CallbackInfo):
...
t = mylib.MyClass()
t.setcallback(myCallback)
I saw some topics near my problem but couldn't solve it
For example here :
Realtime processing and callbacks with Python and C++ there is advice to use boost::python and warning about GLI but no examples.
And here
How to call a python function from a foreign language thread (C++) there is no full description with python code part and with "BOOST_PYTHON_MODULE" part
I also found link to use py_boost_function.hpp for example in Boost python howto but it didn't compile and actualy I couldn't understand how to use it.
Ok, I'm still trying to figure this out too, but here's whats working for me so far:
#this is the variable that will hold a reference to the python function
PyObject *py_callback;
#the following function will invoked from python to populate the call back reference
PyObject *set_py_callback(PyObject *callable)
{
py_callback = callable; /* Remember new callback */
return Py_None;
}
...
#Initialize and acquire the global interpreter lock
PyEval_InitThreads();
#Ensure that the current thread is ready to call the Python C API
PyGILState_STATE state = PyGILState_Ensure();
#invoke the python function
boost::python::call<void>(py_callback);
#release the global interpreter lock so other threads can resume execution
PyGILState_Release(state);
The python function is invoked from C++, and executes as expected.
These test files from the boost::python source code repository contains great examples about how to pass callbacks from Python into C++:
https://github.com/boostorg/python/blob/develop/test/callbacks.cpp
https://github.com/boostorg/python/blob/develop/test/callbacks.py

How does one get the instance of a Ruby class running in the current RB file? (Embedding Ruby in C++)

I have embedded Ruby inside my C++ application. I have generated the bindings using SWIG.
Basically, I run the ruby file and then Ruby takes over and calls my C++ class.
Based on my previous question, I would like to get the current instance of the class that is defined in the ruby file back to the C++ class so that I may execute instance methods.
I execute the ruby file as follows:
rb_eval_string_protect(<ruby script string>, &status );
rb_funcall(Qnil, rb_intern("main"), 0);
The global main method in the script creates an instance of the defined class in the file. That's the instance I am after.
If I have to, I will add a parameter or another function to pass the instance back, however, I'm not sure how to define that in C++ so that when SWIG generates the binding, it all works ok.
Any help would be appreciated.
Previous Question: Calling Ruby class methods from C++
The C api for ruby does its best to preserve ruby's functional nature, so rb_eval_string_protect() returns the VALUE of the last line of the script given, and rb_funcall() returns the VALUE of the last line of the method invoked.
So the trick is really to think of it as how would you get that instance value in pure ruby? If it's just the return value of main, like
# I'm a ruby script!
main_retval = main()
Then capturing the return value in C is similar:
// I'm some C (or C++) code
VALUE main_retval;
// ...
rb_eval_string_protect("...", &status);
main_retval = rb_funcall(Qnil, rb_intern("main"), 0);
And would give you a reference to the ruby object returned by main.
You can use this object as normal, calling methods and the like
VALUE main_retval_as_string = rb_funcall(main_retval, rb_intern("to_s"), 0);