@@ -46,100 +46,58 @@ def CONTENT_TYPE(self):
46
46
47
47
48
48
class CSVSerializer (BaseSerializer ):
49
- """Placeholder docstring """
49
+ """Searilize data of various formats to a CSV-formatted string. """
50
50
51
51
CONTENT_TYPE = "text/csv"
52
52
53
53
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 .
55
55
56
56
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.
58
59
59
60
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.
74
62
"""
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
85
63
if hasattr (data , "read" ):
86
- return _csv_serialize_from_buffer ( data )
64
+ return data . read ( )
87
65
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__" )
89
68
69
+ def is_mutable_sequence_like (obj ):
70
+ return is_sequence_like and hasattr (obj , "__setitem__" )
90
71
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 ])
97
74
75
+ return self ._serialize_row (data )
98
76
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.
105
79
80
+ Args:
81
+ data (object): Data to be serialized in a row.
106
82
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
126
88
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 )
133
91
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 " )
134
99
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 ))
143
101
144
102
145
103
class NumpySerializer (BaseSerializer ):
0 commit comments