Files
pycapnp/examples/py_custom_message_builder.py
Brian Xu 76a41c8efe refine documentation for PyCustomMessageBuilder (#395)
* refine document for py custom message builder

* refine example

* refine example
2026-01-31 13:15:56 -08:00

66 lines
1.5 KiB
Python

#!/usr/bin/env python3
import capnp # noqa: F401
import addressbook_capnp
class Allocator:
def __init__(self):
self.cur_size = 0
self.last_size = 0
def __call__(self, minimum_size: int) -> bytearray:
actual_size = max(minimum_size, self.cur_size)
print(
f"minimum_size: {minimum_size}, last_size: {self.last_size}, "
f"actual_size: {actual_size}, cur_size: {self.cur_size}"
)
self.last_size = actual_size
self.cur_size += actual_size
WORD_SIZE = 8
byte_count = actual_size * WORD_SIZE
return bytearray(byte_count)
class MemoryViewAllocator:
def __init__(self):
self.buffers = []
def __call__(self, minimum_size: int) -> memoryview:
WORD_SIZE = 8
buffer = bytearray(minimum_size * WORD_SIZE)
self.buffers.append(buffer)
return memoryview(buffer)
person = addressbook_capnp.Person.new_message(allocate_seg_callable=Allocator())
person.init("extraData", 5)
print(person.extraData)
print(type(person.extraData))
print()
person.extraData = b"hello"
print(person.extraData)
print(type(person.extraData))
print()
person = person.as_reader()
print(person.extraData)
print(type(person.extraData))
print()
person = addressbook_capnp.Person.new_message(
allocate_seg_callable=MemoryViewAllocator()
)
person.init("extraData", 5)
print(person.extraData)
print(type(person.extraData))
print()
person.extraData = b"world"
print(person.extraData)
print(type(person.extraData))