py_adapter

Round-trip serialization/deserialization of any Python object to/from any serialization format including Avro and JSON.

to_basic_type(obj: ~typing.Any, *, datetime_type: ~typing.Type = <class 'datetime.datetime'>, json_type: ~typing.Type = <class 'str'>) None | bool | str | int | float | datetime | date | List[None | bool | str | int | float | datetime | date | List[Basic] | Dict[str, Basic]] | Dict[str, None | bool | str | int | float | datetime | date | List[Basic] | Dict[str, Basic]][source]

Convert an object into a data structure using “basic” types only as a pre-serialization step.

Parameters:
  • obj – The object to convert

  • datetime_type – The type to convert datetime objects to. Supported types include int (timestamp), str (ISO-format), and datetime.datetime (no conversion).

  • json_type – The type to convert dataclass “JSON” dict fields to. Fields are identified by having custom meta data {"py_adapter": {"logical_type": "json" }}. If set to str (default), field values are serialized as JSON strings. Set to dict for no conversion.

from_basic_type(basic_obj: None | bool | str | int | float | datetime | date | List[None | bool | str | int | float | datetime | date | List[Basic] | Dict[str, Basic]] | Dict[str, None | bool | str | int | float | datetime | date | List[Basic] | Dict[str, Basic]], py_type: Type[Obj]) Obj[source]

Convert a data structure with “basic” types only into a Python object of a given type

Parameters:
  • basic_obj – Any valid data structure that can be used to create an instance of py_type

  • py_type – The Python class to create an instance from

serialize(obj: Any, *, format: str, writer_schema: bytes = b'') bytes[source]

Serialize an object using a serialization format supported by py-adapter

Parameters:
  • obj – Python object to serialize

  • format – Serialization format as supported by a py-adapter plugin, e.g. JSON.

  • writer_schema – Data schema to serialize the data with, as JSON bytes.

serialize_to_stream(obj: Any, stream: BinaryIO, *, format: str, writer_schema: bytes = b'') None[source]

Serialize an object to a file-like object using a serialization format supported by py-adapter

Parameters:
  • obj – Python object to serialize

  • stream – File like object to write the serialized data into

  • format – Serialization format as supported by a py-adapter plugin, e.g. JSON.

  • writer_schema – Data schema to serialize the data with, as JSON bytes.

serialize_many(objs: Iterable[Any], *, format: str, writer_schema: bytes = b'') bytes[source]

Serialize multiple objects using a serialization format supported by py-adapter

Parameters:
  • objs – Python objects to serialize

  • format – Serialization format as supported by a py-adapter plugin, e.g. JSON.

  • writer_schema – Data schema to serialize the data with, as JSON bytes.

serialize_many_to_stream(objs: Iterable[Any], stream: BinaryIO, *, format: str, writer_schema: bytes = b'') None[source]

Serialize multiple objects to a file-like object using a serialization format supported by py-adapter

Parameters:
  • objs – Python objects to serialize

  • stream – File like object to write the serialized data into

  • format – Serialization format as supported by a py-adapter plugin, e.g. JSON.

  • writer_schema – Data schema to serialize the data with, as JSON bytes.

deserialize(data: bytes, py_type: Type[Obj], *, format: str, writer_schema: bytes = b'', reader_schema: bytes = b'') Obj[source]

Deserialize bytes as a Python object of a given type from a serialization format supported by py-adapter

Parameters:
  • data – Serialized data

  • py_type – The Python class to create an instance from

  • format – Serialization format as supported by a py-adapter plugin, e.g. JSON.

  • writer_schema – Data schema used to serialize the data with, as JSON bytes.

  • reader_schema – Data schema to deserialize the data with, as JSON bytes. The reader schema should be compatible with the writer schema.

deserialize_from_stream(stream: BinaryIO, py_type: Type[Obj], *, format: str, writer_schema: bytes = b'', reader_schema: bytes = b'') Obj[source]

Deserialize a file-like object as a Python object of a given type from a serialization format supported by py-adapter

Parameters:
  • stream – File-like object to deserialize

  • py_type – The Python class to create an instance from

  • format – Serialization format as supported by a py-adapter plugin, e.g. JSON.

  • writer_schema – Data schema used to serialize the data with, as JSON bytes.

  • reader_schema – Data schema to deserialize the data with, as JSON bytes. The reader schema should be compatible with the writer schema.

deserialize_many(data: bytes, py_type: Type[Obj], *, format: str, writer_schema: bytes = b'', reader_schema: bytes = b'') Iterator[Obj][source]

Deserialize bytes as an iterator over Python objects of a given type from a serialization format supported by py-adapter

Parameters:
  • data – Serialized data

  • py_type – The Python class to create an instance from

  • format – Serialization format as supported by a py-adapter plugin, e.g. JSON.

  • writer_schema – Data schema used to serialize the data with, as JSON bytes.

  • reader_schema – Data schema to deserialize the data with, as JSON bytes. The reader schema should be compatible with the writer schema.

deserialize_many_from_stream(stream: BinaryIO, py_type: Type[Obj], *, format: str, writer_schema: bytes = b'', reader_schema: bytes = b'') Iterator[Obj][source]

Deserialize a file-like object as an iterator over Python objects of a given type from a serialization format supported by py-adapter

Parameters:
  • stream – File-like object to deserialize

  • py_type – The Python class to create an instance from

  • format – Serialization format as supported by a py-adapter plugin, e.g. JSON.

  • writer_schema – Data schema used to serialize the data with, as JSON bytes.

  • reader_schema – Data schema to deserialize the data with, as JSON bytes. The reader schema should be compatible with the writer schema.

exception DataTypeError(data: Any, schema: Schema)[source]

Bases: TypeError

Data not compatible with the schema error