Skip to content

Exposing underlying data to the buffer protocol in Python #640

@jonathf

Description

@jonathf

One of the nice things with making a Python wrapper to (clean) C++ code is that any data that lies evenly spaced contiguous in memory can be hooked into the numpy interface for advanced construction, slicing, dicing and lots of manipulation.
I've completed an interface for it for the Amount class now that I am happy with which then utilizes the protocol both for conversion to and from numpy.

To be able to do this, I needed:

  1. meta-information about the data like type, shape, padding, strides, etc.
  2. a pointer to the first element of the data.

For the most part, (1) is solvable on my end, at least for the structures that I have looked at so far. But (2) requires code changes.
What I need for Amount and proably a few classes is ways to get the pointer to the underlying data.

For the current implementation of Amount in amount.h I've just added:

  Capacity* get_data(){
      return elems.data();
  }

And that is it. I can do my magic.

Does this sound like a reasonable plan?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions