How to use the numcodecs.gzip.GzipFile function in numcodecs

To help you get started, we’ve selected a few numcodecs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zarr-developers / numcodecs / numcodecs / gzip.py View on Github external
def decode(self, buf, out=None):

        # normalise inputs
        # BytesIO only copies if the data is not of `bytes` type.
        # This allows `bytes` objects to pass through without copying.
        buf = io.BytesIO(ensure_bytes(buf))

        # do decompression
        with _gzip.GzipFile(fileobj=buf, mode='rb') as decompressor:
            if out is not None:
                out_view = ensure_contiguous_ndarray(out)
                decompressor.readinto(out_view)
                if decompressor.read(1) != b'':
                    raise ValueError("Unable to fit data into `out`")
            else:
                out = decompressor.read()

        return out
github zarr-developers / numcodecs / numcodecs / gzip.py View on Github external
def encode(self, buf):

        # normalise inputs
        buf = ensure_contiguous_ndarray(buf)

        # do compression
        compressed = io.BytesIO()
        with _gzip.GzipFile(fileobj=compressed,
                            mode='wb',
                            compresslevel=self.level) as compressor:
            compressor.write(buf)
        compressed = compressed.getvalue()

        return compressed