[Python-Dev] PEP: Extending the buffer protocol to share array information.
Chris Barker
Chris.Barker at noaa.gov
Wed Nov 1 20:20:47 CET 2006
Martin v. Löwis <martin <at> v.loewis.de> writes:
> Can you please give examples for real-world applications of this
> interface, preferably examples involving multiple
> independently-developed libraries?
OK -- here's one I haven't seen in this thread yet:
wxPython has a lot code to translate between various Python data types and wx
data types. An example is PointList Helper. This code examines the input
Python data, and translates it to a wxList of wxPoints. It is used in a bunch
of the drawing functions, for instance. It has some nifty optimizations so
that if a python list if (x,y) tuples is passed in, then the code uses
PyList_GetItem() to access the tuples, for instance.
If an Nx2 numpy array is passed in, it defaults to PySequence_GetItem() to get
the (x,y) pair, and then again to get the values, which are converted to
Python numbers, then checked and converted again to C ints.
The results is an awful lot of processing, even though the data in the numpy
array already exists in a C array that could be exactly the same as the wxList
of wxPoints (in fact, many of the drawing methods take a pointer to a
correctly formatted C array of data).
Right now, it is faster to convert your numpy array of points to a python list
of tuples first, then pass it in to wx.
However, were there a standard way to describe a buffer (pointer to a C array
of data), then the PointListHelper code could look to see if the data is
already correctly formated, and pass the pointer right through. If it was not
it could still do the translation (like from doubles to ints, for instance)
far more efficiently.
When I get the chance, I do intend to contribute code to support this in
wxPython, using the numpy array interface. However, wouldn't it be better for
it to support a generic interface that was in the standard lib, rather than
only numpy?
While /F suggested we get off the PIL bandwagon, I do have code that has to
pass data around between numpy, PIL and wx.Images ( and matplotlib AGG
buffers, and GDAL geo-referenced image buffers, and ...). Most do support the
current buffer protocol, so it can be done, but I'd be much happier if there
was a little more checking going on, rather than my python code having to make
sure the data is all arranged in memory the right way.
Oh, there is also the Python Cartographic Library, which can take a Python
list of tuples as coordinates, and to a Projection on them, but which can't
take a numpy array holding that same data.
-Chris
More information about the Python-Dev
mailing list