コード例 #1
0
ファイル: __init__.py プロジェクト: NiklasRosenstein/databind
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()
コード例 #2
0
ファイル: __init__.py プロジェクト: NiklasRosenstein/databind
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))
コード例 #3
0
ファイル: __init__.py プロジェクト: NiklasRosenstein/databind
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()