Skip to content

Commit

Permalink
Add serialize_many function
Browse files Browse the repository at this point in the history
  • Loading branch information
faph committed Sep 16, 2023
1 parent 174d28f commit ca9f688
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
31 changes: 28 additions & 3 deletions src/py_adapter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@
import inspect
import logging
import uuid
from typing import Any, Callable, Dict, List, Optional, Type, TypeVar, Union, cast
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Sequence,
Type,
TypeVar,
Union,
cast,
)

import avro.schema
import dateutil.parser
Expand Down Expand Up @@ -93,7 +104,7 @@ def serialize(obj: Any, *, format: str, writer_schema: bytes = b"") -> bytes:
Serialize an object using a serialization format supported by **py-adapter**
:param obj: Python object to serialize
:param format: Serialization format as supported by a **py-adpater** plugin, e.g. ``JSON``.
:param format: Serialization format as supported by a **py-adapter** plugin, e.g. ``JSON``.
:param writer_schema: Data schema to serialize the data with, as JSON bytes.
"""
serialize_fn = py_adapter.plugin.plugin_hook(format, "serialize")
Expand All @@ -102,13 +113,27 @@ def serialize(obj: Any, *, format: str, writer_schema: bytes = b"") -> bytes:
return data


def serialize_many(objs: Sequence[Any], *, format: str, writer_schema: bytes = b"") -> bytes:
"""
Serialize multiple objects using a serialization format supported by **py-adapter**
:param objs: Python objects to serialize
:param format: Serialization format as supported by a **py-adapter** plugin, e.g. ``JSON``.
:param writer_schema: Data schema to serialize the data with, as JSON bytes.
"""
serialize_fn = py_adapter.plugin.plugin_hook(format, "serialize_many")
basic_objs = [to_basic_type(obj) for obj in objs]
data = serialize_fn(objs=basic_objs, writer_schema=writer_schema)
return data


def deserialize(data: bytes, py_type: Type[Obj], *, format: str, writer_schema: bytes = b"") -> Obj:
"""
Deserialize bytes as a Python object of a given type from a serialization format supported by **py-adapter**
:param data: Serialized data
:param py_type: The Python class to create an instance from
:param format: Serialization format as supported by a **py-adpater** plugin, e.g. ``JSON``.
:param format: Serialization format as supported by a **py-adapter** plugin, e.g. ``JSON``.
:param writer_schema: Data schema used to serialize the data with, as JSON bytes.
"""
deserialize_fn = py_adapter.plugin.plugin_hook(format, "deserialize")
Expand Down
1 change: 0 additions & 1 deletion tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def test_serialize_stream_avro(ship_obj, ship_class):
assert obj_out == ship_obj


@pytest.mark.skip("TODO")
def test_serialize_many_json(ship_obj, ship_class):
ship_objs = [ship_obj, ship_obj]
data = py_adapter.serialize_many(ship_objs, format="JSON")
Expand Down

0 comments on commit ca9f688

Please sign in to comment.