Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing underlying data to the buffer protocol in Python #640

Closed
jonathf opened this issue Jan 2, 2022 · 3 comments
Closed

Exposing underlying data to the buffer protocol in Python #640

jonathf opened this issue Jan 2, 2022 · 3 comments

Comments

@jonathf
Copy link
Collaborator

jonathf commented Jan 2, 2022

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?

@jcoupey
Copy link
Collaborator

jcoupey commented Jan 4, 2022

It looks like the kind of stuff we can definitely live with if it helps downstream for the python bindings.

If we start adding stuff that is not used internally at all, my only concern is that we loose track of the reason they're here in the first place (and maybe remove them by mistake later on). So maybe we should add comments to this end.

Another option would be to place those behind a compilation flag, pretty much like what we currently do with USE_LIBOSRM. In that case the behavior would be totally unchanged when running make, but you'd get everything you need by running something like make -D PYTHON_BINDINGS.

@jonathf
Copy link
Collaborator Author

jonathf commented Jan 4, 2022

Great!
Either way works. I'm leaning towards compilation flags as they are more obvious.

@jcoupey jcoupey added this to the v1.12.0 milestone Jan 6, 2022
@jcoupey
Copy link
Collaborator

jcoupey commented Jan 6, 2022

Done in #642.

@jcoupey jcoupey closed this as completed Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants