如何组织 python / Boost Python 项目
我有一个 python 项目,我想使用 Boost::Python 与一些 C++ 库交互。我想知道其他人如何在同一项目中组织他们的 python/boost::python/C++ 代码。
我所说的组织是指文件/目录结构、构建过程等。
I have a python project, to which I would like to interface with some C++ libraries using Boost::Python. I would like to know how others go about organising their python/boost::python/C++ code within the same project.
By organisation I mean in terms of file/directory structure, build procedures etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下文中,pif 表示 Python InterFace。首先,我有一个通用头文件,名为 conv_pif.hpp,其中包含 Boost 头文件和 C++ 标准库头文件等。然后,对于每个 boost python 模块,我都有一个 string_pif.cpp 形式的文件(此处对应于示例模块 genocpp),其中 string 大致对应于模块的名称。
函数export_cppvec_conv 对应于C++ 向量与Python 列表之间的(模板化)转换器。我在文件 cppvec_conv_pif.cpp 中有实际的转换器。特别是,它定义了export_cppvec_conv,它使用模板实例化,因此我可以不用将其包含在geno_pif.cpp中。为了便于说明,export_cppvec_conv的内容如下,其中cppvec_to_python_list和cppvec_from_python_list在cppvec_conv_pif.cpp的主体中定义。
人们可以根据 genocpp 模块的需要添加任意数量的转换器。
当然,我已经在 geno.hpp 中获得了基因函数的标头。
最后,我有一个 Scons 文件,它将所有内容链接在一起
。在这种情况下,只有一个模块,因此 pif_files 只有 geno_pif.cpp。否则,我会只选择我想要的模块。嗯,也许最简单的方法就是在某个地方上传一个工作示例。如果有人对更多细节感兴趣,我想我可以编辑这个?
问候,法希姆
In what follows, pif denotes Python InterFace. First I've got a generic header file, called conv_pif.hpp, which has Boost headers and C++ Std Library headers and such. Then for each boost python module, I have a file (here corresponding to the example module genocpp) of the form string_pif.cpp, where string corresponds roughly to the name of the module.
The function export_cppvec_conv corresponds to a (templated) converter to/from C++ vectors to python lists. I have the actual converters in the file cppvec_conv_pif.cpp. In particular, this defines export_cppvec_conv, which uses template instantatiation, so I can get away without including it in geno_pif.cpp. For illustration, the contents of export_cppvec_conv are as follows, where cppvec_to_python_list and cppvec_from_python_list are defined in the body of cppvec_conv_pif.cpp.
One can add as many converters as needed for the genocpp module.
Then of course I've got the headers for the geno functions in geno.hpp.
Finally, I have a Scons file which links everything together
In this case, there is only one module, so pif_files just has geno_pif.cpp. Otherwise, I would select just those I want for the module. Hmm, maybe it would be easiest to just upload a working example somewhere. If anyone is interested in more detail, I guess I could edit this?
Regards, Faheem
我不能就此给你直接的建议,但是 Gentoo 的一个名为 paludis 的包管理器可以做到这一点,并且从什么开始我知道,它的开发人员非常有能力,因此它的 来源 可以作为一个很好的例子如何做到这一点。
不过,我个人不建议使用 Boost Python。据说与 cython、SWIG 或 SIP 等其他绑定工具相比,它非常慢且消耗内存。
I can't give you direct advice on this, but a package manager for Gentoo called paludis does this, and from what I know, its developers are very capable, so its sources might serve as a good example on how to do this.
I personally would however recommend against Boost Python. It is said to be very slow and memory-consuming compared to other binding tools such as cython, SWIG or SIP.