[Numpy-discussion] Best way to expose std::vector to be used with numpy

Daniele Nicolodi daniele at grinta.net
Mon Oct 13 07:35:18 EDT 2014


I have a C++ application that collects float, int or complex data in a
possibly quite large std::vector. The application has some SWIG
generated python wrappers that expose this vector to python. However,
the standard way in which SWIG exposes the data is to create a touple
and pass this to python, where it is very often converted to a numpy
array for processing. Of course this is not efficient.

I would like therefore to generate a smarter python interface. In python
3 I would without doubt go for implementing the buffer protocol, which
enables seamless interfacing with numpy. However, I need to support also
python 2 and there the buffer protocol is not as nice.

What is the best thing to do to expose the data buffer to python 2,
without introducing a dependency on numpy in the application?

Bonus points if the proposed solution comes with a pointer on how to
implement in in SWIG (which I don't know much and feel a bit too magic
for my taste).

Thank you!


More information about the NumPy-Discussion mailing list