5

I have read through the pycapnp quickstart and docs, and I see no mention for how to read and write basic list objects (like a list of floats).

In particular, I am aware that if I write a custom type in a capnp file, then I can read an object of that type using something like

addressbook_capnp.Person.from_bytes(encoded_message)

What I would like to know is the syntax for reading a list of floats like this. I imagine that it would look something like

capnp.List.Float64.from_bytes(encoded_message)

But I simply can't find the correct syntax. Furthermore, since Capn Proto does not export aliases from the .capnp file, I cannot just define a list of floats there (or so it seems).

Can somebody please tell me the correct syntax for reading, writing, and creating lists of basic objects in pycapnp?


EDIT:

For example, in C++ I can create a list object (without a custom type) like this:

::capnp::MallocMessageBuilder message; 
auto capnp_point_cloud = message.initRoot<::capnp::List<Val>>(num_axes);
for (size_t i = 0; i < num_axes; ++i) { 
    capnp_point_cloud.set(i, array[i]);
}
bremen_matt
  • 6,902
  • 7
  • 42
  • 90
  • There is no capnp file. – bremen_matt Feb 14 '22 at 12:35
  • With a capnp file in place, the machinery is clear to me. Furthermore, in C++, it is relatively easy to construct a raw Capnp List of some type. I do not understand how to do this in Python – bremen_matt Feb 14 '22 at 12:36
  • Say a list of float 64 with the values 0,1,2,3. Is that what you mean? – bremen_matt Feb 14 '22 at 12:37
  • The issue is that I do not see how to even instantiate a raw Capnp List type in python with the custom capnp file – bremen_matt Feb 14 '22 at 12:39
  • Ok so if I understand correctly, you want to generate a raw capnp list type in Python, create a message with an arbitrary list, then deserialize it in Python. – Jacques Gaudin Feb 14 '22 at 12:41
  • Yes, that is it. In C++, I know how to do this. I cannot figure this out in python though, and I cannot find anything in the docs – bremen_matt Feb 14 '22 at 12:47
  • There are many times when we just need to send a simple list, but all of the machinery is already in place for CapnProto messages of other types – bremen_matt Feb 14 '22 at 14:48
  • To be clear, my preference would be to have a type alias for a list, but CapnProto does not support this: https://capnproto.org/roadmap.html – bremen_matt Feb 14 '22 at 14:49
  • It looks to me like pycapnp is built around the concept of struct schemas and although using it with raw types may be possible, you would have to use internal / undocumented parts of the implementation. I was looking at another implementation [`capnpy`](https://capnpy.readthedocs.io/en/latest/index.html) but it seems that you would have the same issue. – Jacques Gaudin Feb 14 '22 at 16:51
  • Maybe writing a C++ module for Python would work but it sounds like a big endeavor to solve your problem. – Jacques Gaudin Feb 14 '22 at 16:56
  • I could certainly pybind my own implementation, but this just seems like such an obvious thing, that I thought that there must be something that exists already. – bremen_matt Feb 14 '22 at 16:59
  • It might be worth putting the question to the author directly, see what is their take on it. – Jacques Gaudin Feb 14 '22 at 17:03

0 Answers0