使用 boost python 元组,如何循环元组项?

Posted

技术标签:

【中文标题】使用 boost python 元组,如何循环元组项?【英文标题】:With boost python tuple, how to loop over tuple items? 【发布时间】:2020-01-27 21:26:47 【问题描述】:

>> 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;
      |   ~~~~~~~~~~~~~~~~^~~~~~~~~~~~

所以问题出在 https://www.boost.org/doc/libs/1_38_0/libs/python/doc/v2/tuple.html

【问题讨论】:

可能是因为它是一个模板?这个问题可能会有所帮助:***.com/questions/1198260/… 在我看来,std::tuple 是一个模板,但是 boost::python::tuple 是一个常规类:boost.org/doc/libs/1_38_0/libs/python/doc/v2/tuple.html。不确定 boost.fusion 在这种情况下如何提供帮助... “无法编译”?请edit显示实际的错误信息 【参考方案1】:

解决方案:

代码必须使用lenextract

>> 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
>>> 

【讨论】:

以上是关于使用 boost python 元组,如何循环元组项?的主要内容,如果未能解决你的问题,请参考以下文章

在for循环python中打印元组

在 Python 中使用 for 循环计算元组列表的平均值

如何在python字典中访问/断言元组键值

Python基础(切片,list循环,元组)

python 第三章 元祖,字典,集合,while循环

boost元组数组