2011-01-23 43 views
2

我有一個python項目,我想使用Boost :: Python與一些C++庫進行交互。我想知道其他人如何在同一個項目中組織它們的python/boost :: python/C++代碼。如何組織python/Boost Python項目

通過組織我的意思是在文件/目錄結構方面,建立和程序等

回答

0

我不能給你在這個方向的建議,但爲Gentoo的軟件包管理器叫做paludis做到這一點,從我知道,它的開發人員非常有能力,所以其sources可能是一個很好的例子,如何做到這一點。

但是我個人會推薦Boost Python。與其他綁定工具(如cython,SWIG或SIP)相比,它被認爲速度非常慢並且非常耗費內存。

+0

boost.python創建非常快速的綁定,但這不是問題的主題。 – 2011-01-23 16:33:25

1

在下文中,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++向量蟒列表。我在文件cppvec_conv_pif.cpp中有實際的轉換器。特別是,它定義了export_cppvec_conv,它使用模板instantatiation,所以我可以不用將它包含在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中的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。否則,我會選擇我想要的模塊。嗯,也許最簡單的方法就是在某個地方上傳一個工作示例。如果有人對更多細節感興趣,我想我可以編輯它?

Regards,Faheem