Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pycapnp: imported type is not visible

Imported type is not visible when using pycapnp.

I have 2 files, first is vec1.capnp

@0xf449cc59f43990cf;
struct Vec {
  x @0: Float64;
}

and the 2nd is vec2.capnp

@0xf449cc59f43990c0;
using Vec = import "vec1.capnp".Vec;
struct Vec2 {
  x @0: Float64;
  y @1: Vec;
}

When creating a new message with

import capnp
capnp.remove_import_hook()
schema = capnp.load('vec2.capnp')
instance = schema.Vec2.new_message()
print(instance)

the result is

(x = 0)

y is missing. Vec is also missing when listing all types of the schema.

like image 222
Thomas Avatar asked Dec 28 '25 14:12

Thomas


1 Answers

The attribute y is there, in the instance, and you can find it in the instance class if calling dir(instance). Also it will appear in the print as soon as you'll call y at least once. I guess capnp is doing lazy initialisation for custom types, thus you won't see attribute until you'll try to access it first.

import capnp

capnp.remove_import_hook()
schema = capnp.load('vec2.capnp')
instance = schema.Vec2.new_message()
instance.y
print(instance)
# Outputs: (x = 0, y = (x = 0))

or

instance = schema.Vec2.new_message(y = {})
print(instance)
# Outputs: (x = 0, y = (x = 0))
like image 50
Johnny Cheesecutter Avatar answered Dec 31 '25 18:12

Johnny Cheesecutter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!