用boost.python暴露std :: vector <double>

dev*_*vin 4 c++ python boost-python

我编写了一些生成std :: vector的C++代码.

我还有一个操作一些数据的python脚本,现在,我正在这样声明(如下).

import numpy
x = numpy.random.randn(1000)
y = numpy.random.randn(1000)
Run Code Online (Sandbox Code Playgroud)

我可以运行脚本.从我的C++代码:

    using namespace boost::python;
    try{
            Py_Initialize();
            object main = import("__main__");
            object global(main.attr("__dict__"));
            object result = exec_file("scatterPlot.py", global, global);
            Py_Finalize();
    }
    catch(error_already_set){
            PyErr_Print();
    }

    return;
Run Code Online (Sandbox Code Playgroud)

我不知道如何将我的C++数据转换为python.我有很多,但似乎没有任何确定性.

我有我的C++

BOOST_PYTHON_MODULE(vector_indexing_suite_ext){
        boost::python::class_<std::vector<double> >("PyVec")
        .def(boost::python::vector_indexing_suite<std::vector<double> >());
}
Run Code Online (Sandbox Code Playgroud)

这似乎有效,但据我所知,它只为我的python脚本提供了一个类"PyVec",但不提供我需要的数据.我错了吗?

我还看到其他人在python邮件列表中使用boost :: shared_ptr.

我也发现了这个例子但发现它令人困惑.

我可以想到几种方法

  1. 将一些东西传递给boost::python::exec_file方法
  2. 使用 boost_indexing_suite_ext
  3. Uinsg boost::shared_ptr

哪种方法最容易开始?我觉得没办法.

这里有一些我看过的链接: 来自 python网站 的boost网站上的另一个邮件列表线程

更新:

这适用于传递int给我的python代码,如下所示

int main(){
        int five_squared=0;
        int a =3;
        try {   
                Py_Initialize();
                object main_module = import("__main__");
                object main_namespace = main_module.attr("__dict__");
                main_namespace["var"]=a;
                object ignored = exec("result = 5 ** var", main_namespace);
                five_squared = extract<int>(main_namespace["result"]);
        } catch( error_already_set ) {
                PyErr_Print();
        }
        std::cout << five_squared << std::endl;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是我想传递一个向量,当我尝试以类似的方式执行此操作时,我得到了这个错误

TypeError:找不到C++类型的to_python(by-value)转换器:std :: vector>

所以,显然我需要告诉python如何处理std :: vector.我认为这段代码可以帮助你.

BOOST_PYTHON_MODULE(vector_indexing_suite_ext){
        boost::python::class_<std::vector<double> >("PyVec")
        .def(boost::python::vector_indexing_suite<std::vector<double> >());
}
Run Code Online (Sandbox Code Playgroud)

但由于std :: vector很常见,所以必须有一种定义的方法来做到这一点......对吗?

int*_*jay 11

以下代码适用于我(Python 2.6,Boost 1.39).这与您的代码几乎相同,除了没有BOOST_PYTHON_MODULE行本身(但是使用class_了向量的定义).BOOST_PYTHON_MODULE只需在创建扩展模块时使用.

#include <iostream>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
using namespace boost::python;
using namespace std;

int main()
{
    vector<double> vec;
    vec.push_back(1.2);
    vec.push_back(3.4);
    try {   
            Py_Initialize();

            boost::python::class_<std::vector<double> >("PyVec")
            .def(boost::python::vector_indexing_suite<std::vector<double> >());

            object main_module = import("__main__");
            object globals = main_module.attr("__dict__");
            globals["var"]=vec;
            object ignored = exec("result = sum(var)", globals, globals);
            double result = extract<double>(globals["result"]);
            std::cout << result << std::endl;
    } catch( error_already_set ) {
            PyErr_Print();
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)