def _write_msgpack_stream(self, items, stream): """Write MessagePack stream Parameters ---------- items : list of dict Same format with dataframe.to_dict(orient="records") Examples: ``[{"time": 12345, "col1": "foo"}, {"time": 12345, "col1": "bar"}]`` stream : File like object Target file like object which has `write()` function. This object will be updated in this function. """ with gzip.GzipFile(mode="wb", fileobj=stream) as gz: packer = msgpack.Packer() for item in items: try: mp = packer.pack(item) except (OverflowError, ValueError): packer.reset() mp = packer.pack(normalized_msgpack(item)) gz.write(mp) stream.seek(0) return stream
def _prepare_file(self, file_like, fmt, **kwargs): fp = tempfile.TemporaryFile() with contextlib.closing(gzip.GzipFile(mode="wb", fileobj=fp)) as gz: packer = msgpack.Packer() with contextlib.closing(self._read_file(file_like, fmt, **kwargs)) as items: for item in items: try: mp = packer.pack(item) except (OverflowError, ValueError): packer.reset() mp = packer.pack(normalized_msgpack(item)) gz.write(mp) fp.seek(0) return fp