Boost documentation.
Jacek Generowicz
jmg at ecs.soton.ac.uk
Wed Mar 28 12:38:32 EST 2001
Jacek Generowicz <jmg at ecs.soton.ac.uk> writes:
> Are there any sources of documentation for the Boost Python Library
> besides the source examples, getting_started<1-5>.cpp ?
>
> How would I write a C++ function which receives a python list and
> converts it into a std::vector<int> for its own use ?
>
> Thanks,
>
> Jacek
Well, I've managed to get an attempt past the compiler now, so I'll
post it here for a critique.
It provides one function called `hmm', which takes a tuple (of
integers), and should print out those integers. Unfortunately it
thinks all the integers are zero (though it does get the correct
number of them).
When I try to do something similar with lists rather than tuples, the
get() method appears to be unavailable.
BTW, I'm using gcc version 2.95.2 on linux.
--------------------------------------------------------------
#include <vector>
#include <iterator>
#include <algorithm>
#include <boost/python/class_builder.hpp>
namespace python = boost::python;
namespace {
void hmm( std::vector<int> v ) {
std::copy( v.begin(), v.end(), std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
}
}
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
std::vector<int> from_python( PyObject * p, python::type< std::vector<int> > ) {
python::tuple tup
= python::tuple(python::ref(p, python::ref::increment_count));
std::vector<int> v( tup.size() );
for ( int i; i<tup.size(); i++ ) {
int j = from_python( tup[i].get(), python::type<int>() );
std::cout << j << std::endl;
v[i] = j;
}
return v;
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
BOOST_PYTHON_MODULE_INIT(converting)
{
try
{
python::module_builder this_module("converting");
this_module.def( hmm, "hmm" );
}
catch(...)
{
python::handle_exception();
}
}
More information about the Python-list
mailing list