c++python混合编程经验

上传人:第*** 文档编号:31321605 上传时间:2018-02-06 格式:DOC 页数:9 大小:58.50KB
返回 下载 相关 举报
c++python混合编程经验_第1页
第1页 / 共9页
c++python混合编程经验_第2页
第2页 / 共9页
c++python混合编程经验_第3页
第3页 / 共9页
c++python混合编程经验_第4页
第4页 / 共9页
c++python混合编程经验_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《c++python混合编程经验》由会员分享,可在线阅读,更多相关《c++python混合编程经验(9页珍藏版)》请在金锄头文库上搜索。

1、这个问题是在我尝试利用 pygraphviz 嵌入我的 C+代码绘制二叉树的时候发现的.找了半天资料,这里我把几种常用的 C+调用PYTHON 利用 boost.python 的方法作一个总结,希望能让别人少走弯路 ,因为有些内容还找不到中文文档,虽然都不难但是开始摸索还是费时间的. 我个人认为 boost.python 真的是非常的 COOL,基本上不需要去学习那个看了就头大用着也不方便的 python c api 了,唯一的缺点是目前相关的资料太少,甚至官网上也解释不够详细. 前面我写了一篇 python 嵌入 c+的入门文章包括安装和环境配置,介绍了如何利用 boost.python 方

2、便的传递 C+代码中的参数,调用 python 函数. boost.python 入门教程 -python 嵌入 c+这个 boost.python 官网上的 tourial 也有介绍. 首先第一种常用方式是 python:boost:exec#include using namespace boost:python;/引入 python 解释器Py_Initialize();/引入_main_ 作用域object main_module = import(_main_);object main_namespace = main_module.attr(_dict_);exec(print(H

3、ello world!), main_namespace);/exec + python command string + 作用域 第二种方法是利用 boost:python:object 对象第一种方法虽然很好但是不方便和 C+交互数据,如传递 C+中的数据作为参数给 python函数,以及 C+接受 python 执行函数后的返回值. 那么怎么用 object 调用 python 函数呢,很简单看下面的代码就了解了.我在 simple.py 中定义了函数def foo(int i = 3):return i + 2008 这样一个简单的函数, 通过将 simple.py 利用 boost:

4、python:exec_file 将其引入到_main_作用域,我们在_main_._dict_ 也就是 main_namespace 中取到命名为 foo 的函数对象,并将其转换成boost:python:object 使用 . 这里我们给这个 object同样命名为 foo,调用 foo(5)即调用 python 函数 foo(5)返回 2013,注意在 C+环境中要用extract将这个值取到. object simple = exec_file(simple.py,main_namespace, main_namespace);object foo = main_namespacefo

5、o;int val = extract(foo(5);cout template class object_operators : public def_visitorpublic:/ function call /object operator()() const;detail:args_proxy operator* () const; object operator()(detail:args_proxy const object operator()(detail:args_proxy const &args, detail:kwds_proxy const 这个正是我们所需要的,()

6、操作符重载,从而支持不定参数,上面红色的第一个函数,和不定参数+关键字参数,红色的第二个函数.Class template object_operators observer functionsobject operator()() const;template object operator()(A0 consttemplate object operator()(A0 const.template object operator()(A0 constEffects: call(object(*static_cast(this).ptr(), a1, a2,.aN) 这个对应普通的 pyth

7、on 函数调用object operator()(detail:args_proxy const Effects: call object with arguments given by the tuple args 对应不定参数的 python 函数调用object operator()(detail:args_proxy const &args, detail:kwds_proxy const Effects: call object with arguments given by the tuple args, and named arguments given by the dicti

8、onary kwds 对应不定参数+关键字参数 dict 的 python 函数的调用 OK,现在所有的 python 函数我们都可以在 c+中利用 boost:python:object 调用了!很 COOL 吧!看一个具体的例子: 在我试图在我的 C+程序中使用 pygraphviz 的时候,我需要调用下面这样一个 python函数.add_node(self, n, *attr) method of pygraphviz.agraph.AGraph instanceAdd a single node n.If n is not a string, conversion to a stri

9、ng will be attempted.String conversion will work if n has valid string representation(try str(n) if you are unsure). G=AGraph() G.add_node(a) G.nodes()a G.add_node(1) # will be converted to a string G.nodes()a, 1Attributes can be added to nodes on creation G.add_node(2,color=red)该参数列表由一个普通参数和一个关键字参数

10、构成,你可能会想上面的几种 operator()的重载函数中没有这种形式啊?没有关系,解决的办法是将普通参数在这里当作 tuple 看待 ,而且你只能这么做:)否则运行通不过的! 具体的办法是如果你的函数有 n 个普通参数在关键字参数前面那么你就生成并传递一个有 n 个元素组成的 tuple,如果是形如 foo(*kargs)这样的函数 ,那么你也需要先传递一个空 tuple, void sort(args_proxy const x.sort(*tuple(), *dict(make_tuple(make_tuple(reverse, true); / 等价于 Python 调用 x.sor

11、t(reverse=true) 好了下面看一下我调用 add_node 的代码,注意 add_node(1, color=red)表示生成一个node,它的关键字是 1,而颜色是红色,我也可能会调用 add _node(2, lable=abc)表示该节点关键字是 2,而它将会被输出显示的标志是 abc.void print(std:string result = huff_tree.dot) using namespace boost:python; /for tree printingPy_Initialize();object main_module = import(_main_);o

12、bject main_namespace = main_module.attr(_dict_);exec(import pygraphviz as pgv, main_namespace);exec(tree_graph = pgv.AGraph(directed=True,strict=True), main_namespace);object tree_graph = main_namespacetree_graph;tree_graph.attr(add_node)(*make_tuple(1), *dict(make_tuple(make_tuple(label, Jordan); e

13、xec(tree_graph.graph_attrepsilon=0.001, main_namespace);exec(tree_graph.layout(dot), main_namespace);exec(tree_graph.write(huff_tree.dot), main_namespace);恩, 看下生成的 huff_tree.dot 文件,node 的 key 是 1, label 是 Jordan,完全正确:)strict digraph graph bb=0,0,70,36,epsilon=0.001;node label=N;1 height=0.50,label=Jordan,pos=35,18,width=0.97;图片显示如下: 关于上面代码 dict(make_tuple(make_tuple()的解释:其实是这样的对这样一个 tuple(tuple,tuple) 如 (1,2),(3,4) 执行 dict 操作得到 1:2,3:4即 dict(make_tuple(make_tuple(1,2),make_tuple(3,4) = 1:2, 3:4 而上面的例子 women 其实就是 dict(make_tuple(make_tuple(1,2)这样里面只有一个tuple 的特例。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号