ffpython is a c++ lib,which is to simplify task that embed python and extend python. For example, call python function, register c++ function to python, register c++ class to python. Only one implement c++ header file.
- easier to embed python script
- easier to call python function
- easier to set or get var in python script
- easier to extend python with c++ static function
- easier to extend python with c++ class. C++ class Once registed, python can use it like builtin type.
- when python exception throw, ffpython will wrap it as a std exception which includes python traceback info.
- python2.7 python3, win / linux
printf("sys.version=%s\n", ffpython.getVar<std::string>("sys", "version").c_str());
ffpython.setVar("fftest", "global_var", "OhNice");
printf("fftest.global_var=%s\n", ffpython.getVar<std::string>("fftest", "global_var").c_str());int a1 = 100; float a2 = 3.14f; std::string a3 = "OhWell";
ffpython.call<void>("fftest", "testBase", a1, a2, a3);call python function, Support all STL type as arg or return value. Nine args can be supported. Vector and List for tuple and list in python,map for dict in python.
std::vector<int> a1;a1.push_back(100);a1.push_back(200);
std::list<std::string> a2; a2.push_back("Oh");a2.push_back("Nice");
std::vector<std::list<std::string> > a3;a3.push_back(a2);
ffpython.call<bool>("fftest", "testStl", a1, a2, a3);classFoo
{
public:Foo(int v_) :nValue(v_)
{
printf("%s\n", __FUNCTION__);
}
virtual~Foo()
{
printf("%s\n", __FUNCTION__);
}
intgetValue() { return nValue; }
voidsetValue(int v_) { nValue = v_; }
voidtestStl(std::map<std::string, std::list<int> >& v_)
{
printf("%s\n", __FUNCTION__);
}
int nValue;
};
classDumy : publicFoo
{
public:Dumy(int v_) :Foo(v_)
{
printf("%s\n", __FUNCTION__);
}
~Dumy()
{
printf("%s\n", __FUNCTION__);
}
voiddump()
{
printf("%s\n", __FUNCTION__);
}
};
static Foo* objTest(Dumy* p)
{
printf("%s\n", __FUNCTION__);
return p;
}
ffpython.call<void>("fftest", "testRegisterInheritClass");C++ object pointer can be as a arg to python, and object can be access as a instance of builtin type in python.
Dumy tmp_foo(2013);
std::vector<Dumy*> vt;
vt.push_back(&tmp_foo);
ffpython.call<void>("fftest", "testCppObjToPy", &tmp_foo);
printf("testCppObjToPy changed nValue=%d\n", tmp_foo.nValue);
ffpython.call<void>("fftest", "testCppObjToPy2", vt);staticintprintVal(int a1, float a2, const std::string& a3, const std::vector<double>& a4)
{
printf("%s[%d,%g,%s,%d]\n", __FUNCTION__, a1, a2, a3.c_str(), (int)a4.size());
return0;
}
structOpsTest
{
static std::list<int> returnStl()
{
std::list<int> ret;ret.push_back(1024);
printf("%s\n", __FUNCTION__);
return ret;
}
};
std::string testRegFunction(FFPython& ffpython)
{
ffpython.regFunc(&printVal, "printVal")
.regFunc(&OpsTest::returnStl, "returnStl");
ffpython.regClass<Foo(int)>("Foo")
.regMethod(&Foo::getValue, "getValue")
.regMethod(&Foo::setValue, "setValue")
.regMethod(&Foo::testStl, "testStl")
.regField(&Foo::nValue, "nValue");
ffpython.regClass<Dumy(int)>("Dumy", "Foo")
.regMethod(&Dumy::dump, "dump");
ffpython.regFunc(objTest, "objTest");
return"cppext";
}
deftestBase(a1, a2, a3):
print('testBase', a1, a2, a3)
return0deftestStl(a1, a2, a3):
print('testStl', a1, a2, a3)
returnTruedeftest_returnStl():
print('test_returnStl')
#map<string, list<vector<int> > >ret= {'Oh':[[111,222], [333, 444] ] }
returnretdeftestRegFunction():
importffpythonffpython.printVal(123, 45.6 , "----789---", [3.14])
ret=ffpython.returnStl()
print('testRegFunction', ret)
deftestRegisterBaseClass():
importffpythonfoo=ffpython.Foo(20130426)
print("testRegisterBaseClass get_val:", foo.getValue())
foo.setValue(778899)
print("testRegisterBaseClass get_val:", foo.getValue(), foo.nValue)
foo.testStl({"key": [11,22,33] })
print('testRegisterBaseClass testRegisterBaseClass', foo)
deftestRegisterInheritClass():
importffpythondumy=ffpython.Dumy(20130426)
print("testRegisterInheritClass get_val:", dumy.getValue())
dumy.setValue(778899)
print("testRegisterInheritClass get_val:", dumy.getValue(), dumy.nValue)
dumy.testStl({"key": [11,22,33] })
dumy.dump()
print('testRegisterInheritClass', dumy)
deftestCppObjToPy_ext(foo):
print('testCppObjToPy_ext', len(foo))
forkinrange(0, len(foo)):
print('testCppObjToPy_ext', k, foo[k].nValue)
deftestCppObjToPy(foo):
importffpythonprint("testCppObjToPy get_val:", foo.getValue())
foo.setValue(778899)
print("testCppObjToPy get_val:", foo.getValue(), foo.nValue)
foo.testStl({"key": [11,22,33] })
foo.nValue=100print('testCppObjToPy testRegisterBaseClass', foo)
deftestCppObjToPy2(dumyList):
dumy=dumyList[0]
importffpythonprint("testCppObjToPy get_val:", dumy.getValue())
dumy.setValue(778899)
print("testCppObjToPy get_val:", dumy.getValue(), dumy.nValue)
dumy.testStl({"key": [11,22,33] })
dumy.dump()
ffpython.objTest(dumy)
print('testCppObjToPy', dumy)
returndumyclassPyClass:
def__init__(self):
print('PyClass init....')
defsayHi(self, a1, a2):
print('sayHi..', a1, a2)
deftestCppObjReturnPyObj():
importffpythonreturnPyClass()
deftestCppObjReturnPyLambda():
deftestLambda(a1):
print('testLambda....', a1)
returntestLambda- ffpython Only One implement head file, it is easy to itegrate to project.
- ffpython is simplely wrap for python api, so it is efficient.