设为首页 收藏本站
查看: 681|回复: 0

[经验分享] 使用ffpython嵌入和扩展python

[复制链接]

尚未签到

发表于 2015-4-22 09:12:02 | 显示全部楼层 |阅读模式
ffpython
  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.

Project Goals


  • 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.

Supported Python versions


  • python2.5 python2.6 python2.7, win / linux
  • python3.x is being developed, but unfortunately, python3.x api is so different to python2.x, even diffent between python3.2 and python3.3, Headache!!

Embed Python script in C++

Get / Set varialbe in python script/module





printf("sys.version=%s\n", ffpython.get_global_var("sys", "version").c_str());
ffpython.set_global_var("fftest", "global_var", "OhNice");
printf("fftest.global_var=%s\n", ffpython.get_global_var("fftest", "global_var").c_str());
  

call python function, Support all base type as arg or return value. Nine args can be supported.





int a1 = 100; float a2 = 3.14f; string a3 = "OhWell";
ffpython.call("fftest", "test_base", 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.





vector a1;a1.push_back(100);a1.push_back(200);
list a2; a2.push_back("Oh");a2.push_back("Nice");
vector a3;a3.push_back(a2);
ffpython.call("fftest", "test_stl", a1, a2, a3);
typedef map ret_t;
ret_t val = ffpython.call("fftest", "test_return_stl");
  

Extend Python

register c++ static function, all base type supported. Arg num can be nine.





static int print_val(int a1, float a2, const string& a3, const vector& a4)
{
printf("%s[%d,%f,%s,%d]\n", __FUNCTION__, a1, a2, a3.c_str(), a4.size());
return 0;
}
struct ops_t
{
static list return_stl()
{
list ret;ret.push_back(1024);
printf("%s\n", __FUNCTION__);
return ret;
}
};
void test_reg_function()
{
ffpython_t ffpython;//("ext1");
ffpython.reg(&print_val, "print_val")
.reg(&ops_t::return_stl, "return_stl");
ffpython.init("ext1");
ffpython.call("fftest", "test_reg_function");
}
  

register c++ class, python can use it just like builtin types.





class foo_t
{
public:
foo_t(int v_):m_value(v_)
{
printf("%s\n", __FUNCTION__);
}
virtual ~foo_t()
{
printf("%s\n", __FUNCTION__);
}
int get_value() const { return m_value; }
void set_value(int v_) { m_value = v_; }
void test_stl(map& v_)
{
printf("%s\n", __FUNCTION__);
}
int m_value;
};
class dumy_t: public foo_t
{
public:
dumy_t(int v_):foo_t(v_)
{
printf("%s\n", __FUNCTION__);
}
~dumy_t()
{
printf("%s\n", __FUNCTION__);
}
void dump()
{
printf("%s\n", __FUNCTION__);
}
};

static foo_t* obj_test(dumy_t* p)
{
printf("%s\n", __FUNCTION__);
return p;
}
void test_register_base_class(ffpython_t& ffpython)
{
ffpython.reg_class("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");
ffpython.reg_class("dumy_t", "dumy_t class inherit foo_t ctor ", "foo_t")
.reg(&dumy_t::dump, "dump");
ffpython.reg(obj_test, "obj_test");
ffpython.init("ext2");
ffpython.call("fftest", "test_register_base_class");
};
  

Register c++ class which inherit a class having been registered.





ffpython.call("fftest", "test_register_inherit_class");
  

C++ object pointer can be as a arg to python, and object can be access as a instance of builtin type in python.





void test_cpp_obj_to_py(ffpython_t& ffpython)
{
foo_t tmp_foo(2013);
ffpython.call("fftest", "test_cpp_obj_to_py", &tmp_foo);
}
void test_cpp_obj_py_obj(ffpython_t& ffpython)
{
dumy_t tmp_foo(2013);
foo_t* p = ffpython.call("fftest", "test_cpp_obj_py_obj", &tmp_foo);
}
  

Python test script





def test_base(a1, a2, a3):
print('test_base', a1, a2, a3)
return 0
def test_stl(a1, a2, a3):
print('test_stl', a1, a2, a3)
return True
def test_return_stl():
print('test_return_stl')
#map
ret = {'Oh':[[111,222], [333, 444] ] }
return ret
def test_reg_function():
import ext1
ext1.print_val(123, 45.6 , "----789---", [3.14])
ret = ext1.return_stl()
print('test_reg_function', ret)
def test_register_base_class():
import ext2
foo = ext2.foo_t(20130426)
print("test_register_base_class get_val:", foo.get_value())
foo.set_value(778899)
print("test_register_base_class get_val:", foo.get_value(), foo.m_value)
foo.test_stl({"key": [11,22,33] })
print('test_register_base_class test_register_base_class', foo)
def test_register_inherit_class():
import ext2
dumy = ext2.dumy_t(20130426)
print("test_register_inherit_class get_val:", dumy.get_value())
dumy.set_value(778899)
print("test_register_inherit_class get_val:", dumy.get_value(), dumy.m_value)
dumy.test_stl({"key": [11,22,33] })
dumy.dump()
print('test_register_inherit_class', dumy)
def test_cpp_obj_to_py(foo):
import ext2
print("test_cpp_obj_to_py get_val:", foo.get_value())
foo.set_value(778899)
print("test_cpp_obj_to_py get_val:", foo.get_value(), foo.m_value)
foo.test_stl({"key": [11,22,33] })
print('test_cpp_obj_to_py test_register_base_class', foo)
def test_cpp_obj_py_obj(dumy):
import ext2
print("test_cpp_obj_py_obj get_val:", dumy.get_value())
dumy.set_value(778899)
print("test_cpp_obj_py_obj get_val:", dumy.get_value(), dumy.m_value)
dumy.test_stl({"key": [11,22,33] })
dumy.dump()
ext2.obj_test(dumy)
print('test_cpp_obj_py_obj', dumy)
return dumy
  

Summary


  • ffpython Only One implement head file, it is easy to itegrate to project.
  • ffpython is simplely wrap for python api, so it is efficient.
  • source code https://github.com/fanchy/ffpython

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59477-1-1.html 上篇帖子: Python为什么要self 下篇帖子: Python实现DES加密算法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表