繁体   English   中英

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

[英]exposing std::vector<double> with boost.python

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

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

import numpy
x = numpy.random.randn(1000)
y = numpy.random.randn(1000)

我可以运行脚本。 从我的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;

我不知道如何将我的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> >());
}

这似乎有效,但据我所知,它只为我的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;
}

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

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> >());
}

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

以下代码适用于我(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;
}

我不确定我是否理解正确。 导出可以保存std::vector<double>的类“PyVec”后,可以导出任何以向量作为输入或返回类型的c ++函数。 所以当然你可以在c ++中填充你的向量,并使用接口类型“PyVec”在Python中访问这些数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM