def to_bytes(value: T, type_: Type[T]=None, registry: Registry = None) -> bytes: type_ = type_ or type(value) return Context.new(registry or globals()['registry'], type_, value).from_python()
def calc_size(type_: Type[T], registry: Registry = None) -> int: context = Context.new(registry or globals()['registry'], type_, None) converter = context.get_converter() assert isinstance(converter, _BinaryConverter) format_parts, _ = zip(*converter.get_format_parts(context)) return struct.calcsize(''.join(format_parts))
def from_bytes(type_: Type[T], data: bytes, registry: Registry = None) -> T: stream = BufferedBinaryStream(io.BytesIO(data)) return Context.new(registry or globals()['registry'], type_, stream).to_python()