Expose C ++ functions in python and embed python in C ++

There are several events in my application, each event can have some actions. These actions are implemented in C ++. I want to show these basic functions in python and use python to write an action. The advantage is that I can change actions without recompiling. For instance:

CppClass o; // --- this is a action---- o.f1(); o.f2(); // ------------------------ 

use python for script action:

 def action1(o): o.f1() o.f2() 

In C ++, use an interpreter to run this script, find action1 and call it with PyObject, which converts from a C ++ object. In fact, I do not disclose f1 () and f2 () in python, I just use python to rearrange the definition of the action, the whole function is executed by C ++ binary code. Note that I should not give a definition of f1 () and f2 () in python.

The problem is this: how do I expose global functions? eg:

 def action2(): gf1() gf2() 

boost :: python can expose a function, but it is different, it needs to compile a DLL file, and main () belongs to a python script. Of course, I can make global functions a static member of the class, but I just want to know. Note that I have to give the definition of gf1 () and gf2 () in python.

Jython can do this easily: just import Xxx into python code and call Xxx.gf1 () in order. But in cython, how do I define gf1 () in python? This is a kind of extension, but the extension requires that Xxx be compiled in advance. It seems the only way is to make gf () into a class?

+6
source share
3 answers

solved. boost :: python doc is really bad ...

For example: set function

 void ff(int x, int y) { std::cout<<x<<" "<<y<<std::endl; } 

for 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 is still not a "global function", so expose it in the main area of ​​python:

 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) 
+6
source

You can also see Cython .

The main function of Cython is to translate (a subset) of Python code into C or C ++ to create your own Python extensions. As a result, it allows you to pair C / C ++ code with very concise and Python-ish syntax.

The Cython 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-ac-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 create and send an external DLL. For more details see: http://wiki.cython.org/EmbeddingCython

+1
source

ffpython is a C ++ lib that portes the python 2.x api. view repo code

python function call: 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"); 
+1
source

Source: https://habr.com/ru/post/907190/


All Articles