Discussion:
Exposing std::pair to Python
A B
2009-04-16 00:50:13 UTC
Permalink
Hi,

I am wondering if anyone has a working example of exposing std::pair
as a Python tuple or list. Technically, I need to expose a vector of
tuples and the following works

class_<std::vector<std::pair<std::string, double> > >("VectorPair")
.def(vector_indexing_suite<std::vector<std::pair<std::string, double> > >())
;

but I am not sure how then to expose the std::pair. I am guessing I
should use boost/python/tuple.hpp or list.hpp but can't figure out
the syntax.

Thanks in advance.
Michał Nowotka
2009-04-16 23:18:14 UTC
Permalink
I think I can partially answer your question. Although I don't know
how to expose std::pair using indexing suite, thanks to Roman
Yakovenko and his Py++ package, I can show you working example of
exporting pair using different approach:

This is *.h file with class containing variable of type std::pair<int,
int> to be exposed (this file is also available here:
http://pygccxml.svn.sourceforge.net/viewvc/pygccxml?view=rev):

// Copyright 2004-2008 Roman Yakovenko.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef __std_pair_to_be_exported_hpp__
#define __std_pair_to_be_exported_hpp__

#include <utility>

struct tester_t{
tester_t(int a, int b){
pair_.first = a;
pair_.second = b;
}

int compute(){return pair_.first + pair_.second;}

std::pair<int, int> pair_;
};
#endif//__std_pair_to_be_exported_hpp__




And here is wrapper code generated by Py++:



// This file has been generated by Py++.

// Copyright 2004-2008 Roman Yakovenko.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include "boost/python.hpp"

#include "std_pair_to_be_exported.hpp"

namespace bp = boost::python;

BOOST_PYTHON_MODULE(std_pair){
bp::class_< std::pair< int, int > >(
"pair_less__int_comma__int__greater_", "documentation", bp::init<
("documentation") )
.def( bp::init< int const &, int const & >(( bp::arg("__a"),
bp::arg("__b") ), "documentation") )
.def_readwrite( "first", &std::pair< int, int >::first,
"documentation" )
.def_readwrite( "second", &std::pair< int, int >::second,
"documentation" );

bp::class_< tester_t >( "tester_t", "documentation", bp::init<
int, int >(( bp::arg("a"), bp::arg("b") ), "documentation") )
.def(
"compute"
, (int ( ::tester_t::* )( ) )( &::tester_t::compute )
, "documentation" )
.def_readwrite( "pair_", &tester_t::pair_, "documentation" );
}



You should notice that in python:
pairA = module.pair_less__int_comma__int__greater_(2,3)
pairB = module.pair_less__int_comma__int__greater_(2,3)
pairA.first == pairB.first
True
pairA.second == pairB.second
True
BUT:

pairA == pairB
False
I'm also interested in exposing std::pair using indexing suite because
it perhaps make statement pairA == pairB become true.
Especially if that code would be auto-generated by Py++ just like
other STL containers it could be very useful. Roman Yakovenko wrote
there is a chance this functionality would be added in next release of
Py++.

Also you can ask this question on Cplusplus-sig mailing list:
http://mail.python.org/mailman/listinfo/cplusplus-sig

If you find accurate answer to your question, please let me know.
--
Regards

Michał Nowotka
A B
2009-04-18 00:20:28 UTC
Permalink
Michal,
Thanks for the response. I was able to locate the following post
(http://www.nabble.com/Exposing-members-of-members-td21545250.html)
and following sample code mentioned in the post
(http://cci.lbl.gov/cctbx_sources/boost_adaptbx/) that were very
helpful. In fact, exactly what I was looking for. Good luck.
Post by Michał Nowotka
I think I can partially answer your question. Although I don't know
how to expose std::pair using indexing suite, thanks to Roman
Yakovenko and his Py++ package, I can show you working example of
This is *.h file with class containing variable of type std::pair<int,
// Copyright 2004-2008 Roman Yakovenko.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef __std_pair_to_be_exported_hpp__
#define __std_pair_to_be_exported_hpp__
#include <utility>
struct tester_t{
   tester_t(int a, int b){
      pair_.first = a;
      pair_.second = b;
   }
   int compute(){return pair_.first + pair_.second;}
   std::pair<int, int> pair_;
};
#endif//__std_pair_to_be_exported_hpp__
// This file has been generated by Py++.
// Copyright 2004-2008 Roman Yakovenko.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "boost/python.hpp"
#include "std_pair_to_be_exported.hpp"
namespace bp = boost::python;
BOOST_PYTHON_MODULE(std_pair){
   bp::class_< std::pair< int, int > >(
"pair_less__int_comma__int__greater_", "documentation", bp::init<
("documentation") )
       .def( bp::init< int const &, int const & >(( bp::arg("__a"),
bp::arg("__b") ), "documentation") )
       .def_readwrite( "first", &std::pair< int, int >::first,
"documentation" )
       .def_readwrite( "second", &std::pair< int, int >::second,
"documentation" );
   bp::class_< tester_t >( "tester_t", "documentation", bp::init<
int, int >(( bp::arg("a"), bp::arg("b") ), "documentation") )
       .def(
           "compute"
           , (int ( ::tester_t::* )(  ) )( &::tester_t::compute )
           , "documentation" )
       .def_readwrite( "pair_", &tester_t::pair_, "documentation" );
}
pairA = module.pair_less__int_comma__int__greater_(2,3)
pairB = module.pair_less__int_comma__int__greater_(2,3)
pairA.first == pairB.first
True
pairA.second == pairB.second
True
pairA == pairB
False
I'm also interested in exposing std::pair using indexing suite because
it perhaps make statement pairA == pairB become true.
Especially if that code would be auto-generated by Py++ just like
there is a chance this functionality would be added in next release of
Py++.
http://mail.python.org/mailman/listinfo/cplusplus-sig
If you find accurate answer to your question, please let me know.
--
Regards
Michał Nowotka
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Loading...