-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Copyright 2023 J.P. Morgan Chase & Co. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations under the License. | ||
|
||
""" | ||
CSV serializer/deserializer **py-adapter** plugin | ||
""" | ||
|
||
import io | ||
from typing import BinaryIO, Iterable, Iterator | ||
|
||
import more_itertools | ||
|
||
import py_adapter | ||
|
||
|
||
@py_adapter.plugin.hook | ||
def serialize(obj: py_adapter.Basic, stream: BinaryIO) -> BinaryIO: | ||
""" | ||
Serialize an object of basic Python types as CSV data | ||
:param obj: Python object to serialize | ||
:param stream: File-like object to serialize data to | ||
""" | ||
serialize_many([obj], stream) | ||
return stream | ||
|
||
|
||
@py_adapter.plugin.hook | ||
def serialize_many(objs: Iterable[py_adapter.Basic], stream: BinaryIO) -> BinaryIO: | ||
""" | ||
Serialize multiple Python objects of basic types as CSV data | ||
:param objs: Python objects to serialize | ||
:param stream: File-like object to serialize data to | ||
""" | ||
import csv | ||
|
||
text_stream = io.StringIO(newline="") # csv modules writes as text | ||
(first_obj,), objs = more_itertools.spy(objs) # this fails if the iterable is empty | ||
assert isinstance(first_obj, dict), "CSV serializer supports 'record' types only." | ||
csv_writer = csv.DictWriter(text_stream, fieldnames=first_obj.keys()) | ||
csv_writer.writeheader() | ||
csv_writer.writerows(objs) # type:ignore[arg-type] # We know it's a dict | ||
text_stream.flush() | ||
text_stream.seek(0) | ||
|
||
stream.write(text_stream.read().encode("utf-8")) | ||
stream.flush() | ||
return stream | ||
|
||
|
||
@py_adapter.plugin.hook | ||
def deserialize(stream: BinaryIO) -> py_adapter.Basic: | ||
""" | ||
Deserialize CSV data as an object of basic Python types | ||
:param stream: File-like object to deserialize | ||
""" | ||
obj = next(deserialize_many(stream)) | ||
return obj | ||
|
||
|
||
@py_adapter.plugin.hook | ||
def deserialize_many(stream: BinaryIO) -> Iterator[py_adapter.Basic]: | ||
""" | ||
Deserialize CSV data as an iterator over objects of basic Python types | ||
:param stream: File-like object to deserialize | ||
""" | ||
import csv | ||
|
||
text_stream = io.StringIO(stream.read().decode("utf-8")) | ||
csv_reader = csv.DictReader(text_stream) | ||
return csv_reader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright 2023 J.P. Morgan Chase & Co. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations under the License. | ||
|
||
""" | ||
Test script for CSV serialization/deserialization | ||
""" | ||
|
||
import dataclasses | ||
import datetime | ||
from typing import Optional | ||
|
||
import pytest | ||
|
||
import py_adapter | ||
|
||
|
||
@dataclasses.dataclass | ||
class SimpleShip: | ||
name: str | ||
build_on: Optional[datetime.date] = None | ||
|
||
|
||
@pytest.fixture | ||
def simple_ship(): | ||
return SimpleShip( | ||
name="Elvira", | ||
build_on=datetime.date(1970, 12, 31), | ||
) | ||
|
||
|
||
def test_serialize_1_record(simple_ship): | ||
data = py_adapter.serialize(simple_ship, format="CSV") | ||
expected_lines = [b"name,build_on", b"Elvira,1970-12-31"] | ||
assert data.splitlines() == expected_lines | ||
obj_out = py_adapter.deserialize(data, SimpleShip, format="CSV") | ||
assert obj_out == simple_ship | ||
|
||
|
||
def test_serialize_many_records(simple_ship): | ||
objs_in = [simple_ship, simple_ship] | ||
data = py_adapter.serialize_many(objs_in, format="CSV") | ||
expected_lines = [b"name,build_on", b"Elvira,1970-12-31", b"Elvira,1970-12-31"] | ||
assert data.splitlines() == expected_lines | ||
objs_out = list(py_adapter.deserialize_many(data, SimpleShip, format="CSV")) | ||
assert objs_out == objs_in | ||
|
||
|
||
@pytest.mark.xfail(reason="Not supported") | ||
def test_serialize_many_no_records(): | ||
objs_in = [] | ||
data = py_adapter.serialize_many(objs_in, format="CSV") | ||
expected_lines = [b"name,build_on"] | ||
assert data.splitlines() == expected_lines | ||
objs_out = list(py_adapter.deserialize_many(data, SimpleShip, format="CSV")) | ||
assert objs_out == objs_in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters