python - 使用 boost python 元组,如何循环元组项?
问题描述
使用 boost python 元组,如何循环元组项?
>> more dummy.cpp
#include <iostream>
#include <boost/python.hpp>
void doStuffs(boost::python::tuple & t) {
std::cout << "tuple " << std::endl;
//for (auto i : t) std::cout << i << std::endl; // How to loop over tuple items : does NOT compile ?
}
BOOST_PYTHON_MODULE(dummy) {
Py_Initialize();
def("doStuffs", doStuffs);
}
>> make
g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp -lboost_python -lpython2.7
我得到:
>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22)
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dummy
>>> dummy.doStuffs((1, 2., 'a'))
tuple
我需要得到的地方:
>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22)
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dummy
>>> dummy.doStuffs((1, 2., 'a'))
tuple
1
2.
a
在 C++ 中,如何遍历元组项并打印它们?
更新
令人惊讶的是,它编译:
>> more dummy.cpp
#include <iostream>
#include <boost/python.hpp>
void doStuffs(boost::python::tuple & t) {
std::cout << "tuple " << std::endl;
auto t0 = t[0];
//std::cout << t0 << std::endl;
//for (auto i : t) std::cout << i << std::endl; // How to loop over tuple items : does NOT compile ?
}
BOOST_PYTHON_MODULE(dummy) {
Py_Initialize();
def("doStuffs", doStuffs);
}
>> make
g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp -lboost_python -lpython2.7
但这不会:
>> more dummy.cpp
#include <iostream>
#include <boost/python.hpp>
void doStuffs(boost::python::tuple & t) {
std::cout << "tuple " << std::endl;
auto t0 = t[0];
std::cout << t0 << std::endl;
//for (auto i : t) std::cout << i << std::endl; // How to loop over tuple items : does NOT compile ?
}
BOOST_PYTHON_MODULE(dummy) {
Py_Initialize();
def("doStuffs", doStuffs);
}
>> make
g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp -lboost_python -lpython2.7
dummy.cpp: In function ‘void doStuffs(boost::python::tuple&)’:
dummy.cpp:8:16: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
8 | std::cout << t0 << std::endl;
In file included from /usr/include/boost/python/proxy.hpp:9,
from /usr/include/boost/python/object_attributes.hpp:10,
from /usr/include/boost/python/object.hpp:10,
from /usr/include/boost/python/class.hpp:15,
from /usr/include/boost/python.hpp:18,
from dummy.cpp:3:
/usr/include/boost/python/object_operators.hpp:105:1: note: candidate 1: ‘typename boost::python::api::enable_binary<L, R, boost::python::api::object>::type boost::python::api::operator<<(const L&, const R&) [with L = std::basic_ostream<char>; R = boost::python::api::proxy<boost::python::api::item_policies>; typename boost::python::api::enable_binary<L, R, boost::python::api::object>::type = boost::python::api::object]’
105 | BOOST_PYTHON_BINARY_OPERATOR(<<)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/iostream:39,
from dummy.cpp:1:
/usr/include/c++/9/ostream:174:7: note: candidate 2: ‘std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]’
174 | operator<<(bool __n)
| ^~~~~~~~
dummy.cpp:8:19: error: no match for ‘operator<<’ (operand types are ‘boost::iterators::enabled<true>::base<boost::python::api::object>::type’ {aka ‘boost::python::api::object’} and ‘<unresolved overloaded function type>’)
8 | std::cout << t0 << std::endl;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~
所以问题出在 << 运算符上?boost::python::tuple 上缺少强制转换运算符?似乎这里没有定义运算符https://www.boost.org/doc/libs/1_38_0/libs/python/doc/v2/tuple.html
解决方案
解决方案 :
代码必须使用len
and extract
:
>> more dummy.cpp
#include <iostream>
#include <boost/python.hpp>
void doStuffs(boost::python::tuple & t) {
std::cout << "tuple " << std::endl;
for (size_t i = 0; i < len(t); i++) {
std::string s = boost::python::extract<std::string>(boost::python::str(t[i]));
std::cout << s << std::endl;
}
}
BOOST_PYTHON_MODULE(dummy) {
def("doStuffs", doStuffs);
}
编译:
>> make
g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp -lboost_python
现在回到python:
>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22)
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dummy
>>> dummy.doStuffs((1, 1., 'a'))
tuple
1
1.0
a
>>>
推荐阅读
- c# - 如何在 Span 上进行操作
在平行下? - r - 如何使用R将单元格中以逗号分隔的字符移动到数据框中的不同行中
- c# - 显示新表单时隐藏表单,关闭前一个表单时再次显示
- python - 为什么 PostgreSQL 抱怨这个 UPDATE 语句的语法?
- node.js - 机器人离开时从 json 文件中删除项目
- tensorflow - keras multi_gpu_model saved_model 无法在 TF2 代码中加载模型
- c - 使用函数为结构变量分配内存
- angular - 如何在没有订阅的情况下接收套接字数据?
- python - 从惯性/传感器框架到车身框架的转换
- amazon-web-services - 无需授权(登录)即可访问的 AWS Amplify Graph API