[C++-sig] Re: How to export arrays from C(++) to python using boo st

Raoul Gough RaoulGough at yahoo.co.uk
Wed Feb 11 15:38:35 CET 2004

"Shukla, Nitin (Export)" <nitin.shukla at pw.utc.com> writes:

> I have still not figured out how to go about exporting arrays. Most 
> of the examples I have seen so far shows export of STL like vectors 
> etc. What I am looking is to do something simple like this :
> //C++  Code
> class myarrayclass {
> 	int   iarr[20];
> 	float farr[40];
> };
> Then from python I will do only the following:
>>>> from some_name import myarrayclass
>>>> obj = myarrayclass()
>>>> obj.iarr[3]
> 5
>>>> obj.iarr[3] = 10
>>>> obj.iarr[3]
> 10
>>>> obj.farr[5]
> 23.126
>>>> obj.farr[5] = 2.146
>>>> obj.farr[5]
> 2.146
> That's all I want to do from python. If somebody has worked out 
> similar code (complete and working) and willing to guide me will 
> be useful. Nevertheless, if the there are any tutorials on net 
> which will suit the above requirement, will be pretty useful for 
> me.
> If the above requirement can to be implemented only by "indexing_suite 
> class", can someone list down how and what steps needs to be followed.

Well, given the *new* indexing suite (i.e. indexing_v2) you would have
something along the lines of:

using namespace boost::python;

typedef indexing::iterator_range<int *> integer_array;
typedef indexing::iterator_range<float *> float_array;

class myarrayclass {
  int   iarr[20];
  float farr[40];
   integer_array get_int_array () {
    return indexing::make_iterator_range (iarr);

  float_array get_float_array () {
    return indexing::make_iterator_range (farr);

// ...
  class_<integer_array> ("integer_array")
    .def (indexing::container_suite<integer_array>());

  class_<float_array> ("float_array")
    .def (indexing::container_suite<float_array>());

  class_<myarrayclass> ("myarrayclass")
    .def ("get_int_array", &myarrayclass::get_int_array)
    .def ("get_float_array", &myarrayclass::get_float_array)

[untested, but should more or less work]. Minimal help on obtaining
indexing_v2 is available from http://home.clara.net/raoulgough/boost/
Sorry it's not easier at the moment, I'm currently trying to get it
ready for introduction to the CVS mainline...

If you manage that, the file libs/python/test/test_array_ext.cpp has a
simple working example.

Raoul Gough.
export LESS='-X'

More information about the Cplusplus-sig mailing list