-
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
4 changed files
with
86 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 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 | ||
|
||
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 | ||
""" | ||
import csv | ||
|
||
text_stream = io.StringIO(newline="") # csv modules writes as text | ||
csv_writer = csv.DictWriter(text_stream, fieldnames=obj.keys()) | ||
csv_writer.writeheader() | ||
csv_writer.writerow(obj) | ||
text_stream.flush() | ||
text_stream.seek(0) | ||
|
||
stream.write(text_stream.read().encode("utf-8")) | ||
stream.flush() | ||
return stream |
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,42 @@ | ||
# 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 |
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