如何组织 python / Boost Python 项目

发布于 2024-10-13 04:37:54 字数 133 浏览 8 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肥爪爪 2024-10-20 04:37:54

下文中,pif 表示 Python InterFace。首先,我有一个通用头文件,名为 conv_pif.hpp,其中包含 Boost 头文件和 C++ 标准库头文件等。然后,对于每个 boost python 模块,我都有一个 string_pif.cpp 形式的文件(此处对应于示例模块 genocpp),其中 string 大致对应于模块的名称。

****************************************************************************************
geno_pif.cpp
****************************************************************************************
#include "conv_pif.hpp"
#include <boost/python.hpp>
#include "geno.hpp"

void export_cppvec_conv();

void export_geno()
{
  boost::python::def("write_geno_table_affy6_to_file", write_geno_table_affy6_to_file);
}

BOOST_PYTHON_MODULE(genocpp)
{
  export_geno();
  export_cppvec_conv();
}
*****************************************************************************************

函数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的主体中定义。

******************************************
cppvec_conv_pif.cpp (extract)
******************************************
void export_cppvec_conv()
{
  boost::python::to_python_converter<vector<double>, cppvec_to_python_list<double> >();
  cppvec_from_python_list<double>();

  boost::python::to_python_converter<vector<int>, cppvec_to_python_list<int> >();
  cppvec_from_python_list<int>();

  boost::python::to_python_converter<vector<string>, cppvec_to_python_list<string> >();
  cppvec_from_python_list<string>();
}
******************************************

人们可以根据 genocpp 模块的需要添加任意数量的转换器。
当然,我已经在 geno.hpp 中获得了基因函数的标头。
最后,我有一个 Scons 文件,它将所有内容链接在一起

******************************************
Sconstruct
******************************************
#!/usr/bin/python

import commands, glob, os

# Common file, for both executables and Python Interface
common_files = """geno print"""

def pyversion():
    pystr = commands.getoutput('python -V')
    version = pystr.split(' ')[1]
    major, minor = version.split('.')[:2]
    return major + '.' + minor

common_base = Split(common_files)
common = [f + ".cpp" for f in common_base]

# For Python interface only
pif_conv = Split("cppvec_conv cppmap_conv cppset_conv")
pif_conv_files = [t+"_pif.cpp" for t in pif_conv]

pif = Split("geno")
pif_files = [t+"_pif.cpp" for t in pif]

# Boost Python Environment
boost_python_env = Environment(
    CPPPATH=["/usr/include/python"+pyversion(), "."],
    CXXFLAGS='-ftemplate-depth-100 -fPIC -Wall -Werror -pedantic -pipe -O3 -ffast-math -march=opteron',
    #CXXFLAGS='-ftemplate-depth-100 -fPIC -Wall -pedantic -O0 -g',
    CPPDEFINES=['BOOST_PYTHON_DYNAMIC_LIB'],
    LIBPATH=["/usr/lib/python"+pyversion()+"/config"],
    LIBS=["python"+pyversion(), "m", "boost_python"],
    SHLIBPREFIX="", #gets rid of lib prefix
    SHOBJSUFFIX = ".bpo"
)

boost_python_env.SharedLibrary(target='genocpp', source = common + pif_conv_files + pif_files)

。在这种情况下,只有一个模块,因此 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.

****************************************************************************************
geno_pif.cpp
****************************************************************************************
#include "conv_pif.hpp"
#include <boost/python.hpp>
#include "geno.hpp"

void export_cppvec_conv();

void export_geno()
{
  boost::python::def("write_geno_table_affy6_to_file", write_geno_table_affy6_to_file);
}

BOOST_PYTHON_MODULE(genocpp)
{
  export_geno();
  export_cppvec_conv();
}
*****************************************************************************************

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.

******************************************
cppvec_conv_pif.cpp (extract)
******************************************
void export_cppvec_conv()
{
  boost::python::to_python_converter<vector<double>, cppvec_to_python_list<double> >();
  cppvec_from_python_list<double>();

  boost::python::to_python_converter<vector<int>, cppvec_to_python_list<int> >();
  cppvec_from_python_list<int>();

  boost::python::to_python_converter<vector<string>, cppvec_to_python_list<string> >();
  cppvec_from_python_list<string>();
}
******************************************

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

******************************************
Sconstruct
******************************************
#!/usr/bin/python

import commands, glob, os

# Common file, for both executables and Python Interface
common_files = """geno print"""

def pyversion():
    pystr = commands.getoutput('python -V')
    version = pystr.split(' ')[1]
    major, minor = version.split('.')[:2]
    return major + '.' + minor

common_base = Split(common_files)
common = [f + ".cpp" for f in common_base]

# For Python interface only
pif_conv = Split("cppvec_conv cppmap_conv cppset_conv")
pif_conv_files = [t+"_pif.cpp" for t in pif_conv]

pif = Split("geno")
pif_files = [t+"_pif.cpp" for t in pif]

# Boost Python Environment
boost_python_env = Environment(
    CPPPATH=["/usr/include/python"+pyversion(), "."],
    CXXFLAGS='-ftemplate-depth-100 -fPIC -Wall -Werror -pedantic -pipe -O3 -ffast-math -march=opteron',
    #CXXFLAGS='-ftemplate-depth-100 -fPIC -Wall -pedantic -O0 -g',
    CPPDEFINES=['BOOST_PYTHON_DYNAMIC_LIB'],
    LIBPATH=["/usr/lib/python"+pyversion()+"/config"],
    LIBS=["python"+pyversion(), "m", "boost_python"],
    SHLIBPREFIX="", #gets rid of lib prefix
    SHOBJSUFFIX = ".bpo"
)

boost_python_env.SharedLibrary(target='genocpp', source = common + pif_conv_files + pif_files)

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

£烟消云散 2024-10-20 04:37:54

我不能就此给你直接的建议,但是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文