【问题标题】:How to organise python / Boost Python projects如何组织 python / Boost Python 项目
【发布时间】:2011-01-23 16:12:53
【问题描述】:

我有一个 python 项目,我想使用 Boost::Python 与一些 C++ 库进行交互。我想知道其他人如何在同一个项目中组织他们的 python/boost::python/C++ 代码。

我的意思是文件/目录结构、构建过程等方面的组织。

【问题讨论】:

    标签: c++ python organization boost-python


    【解决方案1】:

    在下文中,pif 表示 Python 接口。首先,我有一个通用头文件,称为 conv_pif.hpp,它具有 Boost 头文件和 C++ Std 库头文件等。然后对于每个 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的body中。

    ******************************************
    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 中得到了 geno 函数的标题。 最后,我有一个将所有内容链接在一起的 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。否则,我只会选择我想要的模块。嗯,也许在某处上传一个工作示例是最简单的。如果有人对更多细节感兴趣,我想我可以编辑这个?

    问候,法希姆

    【讨论】:

      【解决方案2】:

      我不能就此给你直接的建议,但是一个名为 paludis 的 Gentoo 包管理器可以做到这一点,据我所知,它的开发人员非常有能力,所以它的 sources 可能是一个很好的例子如何做到这一点。

      我个人建议不要使用 Boost Python。据说与 cython、SWIG 或 SIP 等其他绑定工具相比,它非常缓慢且消耗内存。

      【讨论】:

      • boost.python 创建非常快的绑定,但这不是问题的主题。
      猜你喜欢
      • 2011-07-06
      • 2012-10-21
      • 1970-01-01
      • 2010-09-28
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-14
      相关资源
      最近更新 更多