[C++-sig] Boost-python wrapping a vector of vectors?
Tim Couper
tim at tizmoi.net
Thu Jan 7 11:21:11 CET 2010
Ah I understand .. so will reply to my own posting for others new to pb ...
C++ constructors with one argument behave differently I understand, so
taking a 2 argument case:
class A
{
public
A(int i, const std::vector<std::vector double >>& my_array);
};
In the BOOST_PYTHON_MODULE(foo) ..
define a type using typedef:
typedef std::vector<std::vector<double> > double_trouble;
and add the python wrappers we're going to need - one for wrapping a
vector of doubles, and one for the vector of vectors ..
class_<std::vector<std::vector<double> > > ("v2_double")
.def(vector_indexing_suite<std::vector<std::vector<double> > >());
class_<std::vector<double> > ("v_double")
.def(vector_indexing_suite<std::vector<double> >());
now define the constructor like Troy said ..
class_<duh::A>("A")
.def(init<int, double_trouble >())
;
bjam this lot and we then can write out nosetest tests:
def test_v_double():
v = foo.v_double()
v[:] = [1.4,2.5]
assert_equals(len(v),2)
assert_almost_equal(v[0],1.4)
def test_v2_double():
v2 = foo.v2_double()
v1a = foo.v_double()
v2a = foo.v_double()
v1a[:] = [1.4, 2.5, 5.6]
v2a[:] = [3.4, 2.8, 7.8, 12.5]
v2[:] = [v1a,v2a]
assert_equals(len(v2),2)
assert_almost_equal(v2[0][0],1.4)
def test_A():
v2 = foo.v2_double()
v1a = foo.v_double()
v2a = foo.v_double()
v1a[:] = [1.4, 2.5, 5.6]
v2a[:] = [3.4, 2.8, 7.8, 12.5]
v2[:] = [v1a,v2a]
res = foo.A(1, v2)
assert isinstance(res, foo.A)
and there we have it ..
I'll be back ... :-)
Tim
On 07/01/2010 09:21, Tim Couper wrote:
> Troy
>
> Thanks for the prompt reply .. I'm a python expert and a C++ novice
> ... Apologies for not getting this ...
>
> I've seen I can define a python mapping using the vector_indexing suite
>
> class_< std::vector< std::vector<double> > >("vector_double2")
> .def(vector_indexing_suite<std::vector< std::vector<double> > >
>
> but I can't see how to get that information into the init argument in
> the C++
>
> class_<A>("A")
> .def(init<vector_double2>( ))
>
> as vector_double2 isn't known when the c++ compiles, so this clearly
> isn't the "type" argument to which you refer. Clearly there's
> something I'm missing. How do I define a type? Should I be using the
> named constructors/factories described in
> http://wiki.python.org/moin/boost.python/HowTo? If so how?
>
> Thanks again
>
> Tim
>
>
>
>
>
>
> On 06/01/2010 23:16, troy d. straszheim wrote:
>> Tim Couper wrote:
>>>
>>> I'm trying to boost-python a vector-of-vectors, like
>>>
>>> class A
>>> {
>>> public
>>> A(const std::vector<std::vector double >>& my_array);
>>> };
>>>
>>> and would intuitively write the wrapper:
>>>
>>> BOOST_PYTHON_MODULE(foo)
>>> {
>>> using namespace boost::python
>>>
>>> class_<A>("A")
>>> .def(init(std::vector<std::vector<double> >())
>>> ;
>>>
>>> but get the error "a call to a constructor cannot appear in a
>>> constant expression"
>>
>> Syntax error, init takes a type argument: init<T>()
>>
>> -t
>>
>> _______________________________________________
>> Cplusplus-sig mailing list
>> Cplusplus-sig at python.org
>> http://mail.python.org/mailman/listinfo/cplusplus-sig
>>
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG -www.avg.com
>> Version: 9.0.725 / Virus Database: 270.14.127/2603 - Release Date: 01/06/10 07:35:00
>>
>>
>
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 9.0.725 / Virus Database: 270.14.128/2604 - Release Date: 01/06/10 19:35:00
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20100107/d132b969/attachment-0001.htm>
More information about the Cplusplus-sig
mailing list