Skip to content

change: add csv deserializer #737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/sagemaker/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@ def _row_to_csv(obj):
return ','.join(obj)


class _CsvDeserializer(object):
def __init__(self, encoding='utf-8'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to pass the accept in as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was staying consistent with:

class _CsvSerializer(object):
    def __init__(self):
        self.content_type = CONTENT_TYPE_CSV

open to changing it though if there's a compelling use case

self.accept = CONTENT_TYPE_CSV
self.encoding = encoding

def __call__(self, stream, content_type):
try:
return list(csv.reader(stream.read().decode(self.encoding).splitlines()))
finally:
stream.close()


csv_deserializer = _CsvDeserializer()


class BytesDeserializer(object):
"""Return the response as an undecoded array of bytes.

Expand Down
20 changes: 18 additions & 2 deletions tests/unit/test_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import numpy as np

from sagemaker.predictor import RealTimePredictor
from sagemaker.predictor import json_serializer, json_deserializer, csv_serializer, BytesDeserializer, \
StringDeserializer, StreamDeserializer, numpy_deserializer, npy_serializer, _NumpyDeserializer
from sagemaker.predictor import json_serializer, json_deserializer, csv_serializer, \
csv_deserializer, BytesDeserializer, StringDeserializer, StreamDeserializer, \
numpy_deserializer, npy_serializer, _NumpyDeserializer
from tests.unit import DATA_DIR

# testing serialization functions
Expand Down Expand Up @@ -141,6 +142,21 @@ def test_csv_serializer_csv_reader():
assert result == validation_data


def test_csv_deserializer_single_element():
result = csv_deserializer(io.BytesIO(b'1'), 'text/csv')
assert result == [['1']]


def test_csv_deserializer_array():
result = csv_deserializer(io.BytesIO(b'1,2,3'), 'text/csv')
assert result == [['1', '2', '3']]


def test_csv_deserializer_2dimensional():
result = csv_deserializer(io.BytesIO(b'1,2,3\n3,4,5'), 'text/csv')
assert result == [['1', '2', '3'], ['3', '4', '5']]


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a test with just one line and one value. YMMV

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

def test_json_deserializer_array():
result = json_deserializer(io.BytesIO(b'[1, 2, 3]'), 'application/json')

Expand Down