CXX/Array.h's Py::Array samples or documentation?

Phlip phlip_cpp at my-deja.com
Mon Jan 8 22:01:58 EST 2001


Phil Austin wrote:

> Although I'm sure there are better ways to do this,
> here's a hacked version of CXX_Array.h that initializes a
> multi-dimensional Py::Array using an stl vector of dimensions
> (search on the string "fromVector" to see the changes)
 
>       PyObject* fromVector(const std::vector<int>& dimens, const
>       PyArray_TYPES& t){
> int ndims=dimens.size();
> return (PyObject*) PyArray_FromDims(ndims, (int*) dimens.begin(), t);
>       }

Instantly after posting (don't it always go like that?) I discovered the 
'reshape' function.

I promise I'm soaking in this stuff as fast as possible ;-)

The reshaper lead to a comment in the CXX source saying like "one could 
copy the source to reshape out of Numpy and exploit it here."

That lead to this attempt (those offended by non-Python code may wish to 
stand upside down with their head in a bucket):

        static void
copyData (
        Py::Array & ma,
        vector<vector< string > > & database, 
        int maxFields
        )
{

#if 1

        Py::Sequence shape (Py::Int (2)); // <-- pow
        shape[0] = Py::Int (int (database.size()));
        shape[1] = Py::Int (maxFields);
        PyArray_Reshape ((PyArrayObject*)ma.ptr(), shape.ptr());

#else

        int zone[] = { database.size(), maxFields };
        Py::Object mo ((PyObject*)PyArray_FromDims
                                (2, zone, PyArray_OBJECT)
                        );
        ma = mo;

#endif

}

The first stab's based on the inferred activity inside 'array_reshape()'. 
And it fails on the first line; trying to do the equivalent of a Python

        shape = (2.4)

The second does not crash, but I have not tested the return value or proven 
I can then stuff 'database's contents in.

How could the first one work simpler?

And, in the second one, will the 'ma = mo' line do a deep copy such t hat 
the Python-side owner of 'ma' will see it reshaped and full of goodies?

-- 
Phlip
http://c2.com/cgi/wiki?PhlIp



More information about the Python-list mailing list