<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division
import gzip as _gzip
import io


from .abc import Codec
from .compat import ensure_ndarray, ensure_contiguous_ndarray, PY2


class GZip(Codec):
    """Codec providing gzip compression using zlib via the Python standard library.

    Parameters
    ----------
    level : int
        Compression level.

    """

    codec_id = 'gzip'

    def __init__(self, level=1):
        self.level = level

    def encode(self, buf):

        # normalise inputs
        buf = ensure_contiguous_ndarray(buf)
        if PY2:  # pragma: py3 no cover
            # view as u1 needed on PY2
            # ref: https://github.com/zarr-developers/numcodecs/pull/128#discussion_r236786466
            buf = buf.view('u1')

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

        try:
            compressed = compressed.getbuffer()
        except AttributeError:  # pragma: py3 no cover
            compressed = compressed.getvalue()

        return ensure_ndarray(compressed)

    # noinspection PyMethodMayBeStatic
    def decode(self, buf, out=None):

        # normalise inputs
        buf = ensure_contiguous_ndarray(buf)

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

        # handle destination - Python standard library zlib module does not
        # support direct decompression into buffer, so we have to copy into
        # out if given<commit_msg>Tweak error message for `out` that is too small<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division
import gzip as _gzip
import io


from .abc import Codec
from .compat import ensure_ndarray, ensure_contiguous_ndarray, PY2


class GZip(Codec):
    """Codec providing gzip compression using zlib via the Python standard library.

    Parameters
    ----------
    level : int
        Compression level.

    """

    codec_id = 'gzip'

    def __init__(self, level=1):
        self.level = level

    def encode(self, buf):

        # normalise inputs
        buf = ensure_contiguous_ndarray(buf)
        if PY2:  # pragma: py3 no cover
            # view as u1 needed on PY2
            # ref: https://github.com/zarr-developers/numcodecs/pull/128#discussion_r236786466
            buf = buf.view('u1')

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

        try:
            compressed = compressed.getbuffer()
        except AttributeError:  # pragma: py3 no cover
            compressed = compressed.getvalue()

        return ensure_ndarray(compressed)

    # noinspection PyMethodMayBeStatic
    def decode(self, buf, out=None):

        # normalise inputs
        buf = ensure_contiguous_ndarray(buf)

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

        # handle destination - Python standard library zlib module does not
        # support direct decompression into buffer, so we have to copy into
        # out if given
{'fraction_non_alphanumeric': 0.08365570599613152, 'fraction_numerical': 0.010638297872340425, 'mean_word_length': 2.0352164343360233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1409714', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1139, 'n_words': 327}