Skip to content

Commit 3c8ac07

Browse files
author
Balaji Veeramani
committed
Update serializers.py
1 parent 78d7612 commit 3c8ac07

File tree

1 file changed

+32
-74
lines changed

1 file changed

+32
-74
lines changed

src/sagemaker/serializers.py

Lines changed: 32 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -46,100 +46,58 @@ def CONTENT_TYPE(self):
4646

4747

4848
class CSVSerializer(BaseSerializer):
49-
"""Placeholder docstring"""
49+
"""Searilize data of various formats to a CSV-formatted string."""
5050

5151
CONTENT_TYPE = "text/csv"
5252

5353
def serialize(self, data):
54-
"""Take data of various data formats and serialize them into CSV.
54+
"""Serialize data of various formats to a CSV-formatted string.
5555
5656
Args:
57-
data (object): Data to be serialized.
57+
data (object): Data to be serialized. Can be a NumPy array, list,
58+
file, or buffer.
5859
5960
Returns:
60-
object: Sequence of bytes to be used for the request body.
61-
"""
62-
# For inputs which represent multiple "rows", the result should be newline-separated CSV
63-
# rows
64-
if _is_mutable_sequence_like(data) and len(data) > 0 and _is_sequence_like(data[0]):
65-
return "\n".join([CSVSerializer._serialize_row(row) for row in data])
66-
return CSVSerializer._serialize_row(data)
67-
68-
@staticmethod
69-
def _serialize_row(data):
70-
# Don't attempt to re-serialize a string
71-
"""
72-
Args:
73-
data:
61+
str: The data serialized as a CSV-formatted string.
7462
"""
75-
if isinstance(data, str):
76-
return data
77-
if isinstance(data, np.ndarray):
78-
data = np.ndarray.flatten(data)
79-
if hasattr(data, "__len__"):
80-
if len(data) == 0:
81-
raise ValueError("Cannot serialize empty array")
82-
return _csv_serialize_python_array(data)
83-
84-
# files and buffers
8563
if hasattr(data, "read"):
86-
return _csv_serialize_from_buffer(data)
64+
return data.read()
8765

88-
raise ValueError("Unable to handle input format: ", type(data))
66+
def is_sequence_like(obj):
67+
return hasattr(obj, "__iter__") and hasattr(obj, "__getitem__")
8968

69+
def is_mutable_sequence_like(obj):
70+
return is_sequence_like and hasattr(obj, "__setitem__")
9071

91-
def _csv_serialize_python_array(data):
92-
"""
93-
Args:
94-
data:
95-
"""
96-
return _csv_serialize_object(data)
72+
if is_mutable_sequence_like(data) and len(data) > 0 and is_sequence_like(data[0]):
73+
return "\n".join([self._serialize_row(row) for row in data])
9774

75+
return self._serialize_row(data)
9876

99-
def _csv_serialize_from_buffer(buff):
100-
"""
101-
Args:
102-
buff:
103-
"""
104-
return buff.read()
77+
def _serialize_row(self, data):
78+
"""Serialize data as a CSV-formatted row.
10579
80+
Args:
81+
data (object): Data to be serialized in a row.
10682
107-
def _csv_serialize_object(data):
108-
"""
109-
Args:
110-
data:
111-
"""
112-
csv_buffer = io.StringIO()
113-
114-
csv_writer = csv.writer(csv_buffer, delimiter=",")
115-
csv_writer.writerow(data)
116-
return csv_buffer.getvalue().rstrip("\r\n")
117-
118-
119-
def _is_mutable_sequence_like(obj):
120-
"""
121-
Args:
122-
obj:
123-
"""
124-
return _is_sequence_like(obj) and hasattr(obj, "__setitem__")
125-
83+
Returns:
84+
str: The data serialized as a CSV-formatted row.
85+
"""
86+
if isinstance(data, str):
87+
return data
12688

127-
def _is_sequence_like(obj):
128-
"""
129-
Args:
130-
obj:
131-
"""
132-
return hasattr(obj, "__iter__") and hasattr(obj, "__getitem__")
89+
if isinstance(data, np.ndarray):
90+
data = np.ndarray.flatten(data)
13391

92+
if hasattr(data, "__len__"):
93+
if len(data) == 0:
94+
raise ValueError("Cannot serialize empty array")
95+
csv_buffer = io.StringIO()
96+
csv_writer = csv.writer(csv_buffer, delimiter=",")
97+
csv_writer.writerow(data)
98+
return csv_buffer.getvalue().rstrip("\r\n")
13499

135-
def _row_to_csv(obj):
136-
"""
137-
Args:
138-
obj:
139-
"""
140-
if isinstance(obj, str):
141-
return obj
142-
return ",".join(obj)
100+
raise ValueError("Unable to handle input format: ", type(data))
143101

144102

145103
class NumpySerializer(BaseSerializer):

0 commit comments

Comments
 (0)