<commit_before>"""
Renderers used to render a Python object into byte array.
"""

import simplejson
import pickle

from abc import ABCMeta, abstractmethod
from .mimetypes import MimeType


class RendererBase(metaclass=ABCMeta):
    """
    Base class for all renderers.
    """

    mimetype = None

    @abstractmethod
    def render(self, data, mimetype):
        """
        Render the given data into JSON and returns a byte array.
        :param data: The data to be rendered.
        :param mimetype: The mimetype to render the data.
        :return: A byte array.
        """


class JSONRenderer(RendererBase):
    """
    Renderer which render into JSON.
    """

    mimetype = MimeType('application/json')

    def render(self, data, mimetype):
        """
        Serializes a Python object into a byte array containing a JSON document.

        :param data: A Python object.
        :param MimeType mimetype: The mimetype to render the data.
        :return: A byte array containing a JSON document.
        """

        indent = self.get_indent(mimetype)
        encoding = mimetype.params.get('charset') or 'utf-8'
        return simplejson.dumps(data, indent=indent).encode(encoding)

    def get_indent(self, mimetype):
        """
        Gets the indent parameter from the mimetype.

        :param MimeType mimetype: The mimetype with parameters.
        :return int: The indent if found, otherwise none.
        """
        try:
            indent = max(int(mimetype.params.get('indent', '0')), 0)

            if indent == 0:
                return None

            return indent
        except:
            return None


class PickleRenderer(RendererBase):
    """
    Renderer which render into Pickle binary.
    """

    mimetype = MimeType('application/pickle')

    def render(self, data, mimetype):
        """
        Serializes a Python object into a Pickle byte array.

        :param data: A Python object.
        :param mimetype: The mimetype to render the data.
        :return: A byte array containing a Pickle document.
        """

        return pickle.dumps(data)<commit_msg>Only cast exception should be ignored<commit_after>"""
Renderers used to render a Python object into byte array.
"""

import simplejson
import pickle

from abc import ABCMeta, abstractmethod
from .mimetypes import MimeType


class RendererBase(metaclass=ABCMeta):
    """
    Base class for all renderers.
    """

    mimetype = None

    @abstractmethod
    def render(self, data, mimetype):
        """
        Render the given data into JSON and returns a byte array.
        :param data: The data to be rendered.
        :param mimetype: The mimetype to render the data.
        :return: A byte array.
        """


class JSONRenderer(RendererBase):
    """
    Renderer which render into JSON.
    """

    mimetype = MimeType('application/json')

    def render(self, data, mimetype):
        """
        Serializes a Python object into a byte array containing a JSON document.

        :param data: A Python object.
        :param MimeType mimetype: The mimetype to render the data.
        :return: A byte array containing a JSON document.
        """

        indent = self.get_indent(mimetype)
        encoding = mimetype.params.get('charset') or 'utf-8'
        return simplejson.dumps(data, indent=indent).encode(encoding)

    def get_indent(self, mimetype):
        """
        Gets the indent parameter from the mimetype.

        :param MimeType mimetype: The mimetype with parameters.
        :return int: The indent if found, otherwise none.
        """
        try:
            indent = max(int(mimetype.params.get('indent', '0')), 0)

            if indent == 0:
                return None

            return indent
        except ValueError:
            return None


class PickleRenderer(RendererBase):
    """
    Renderer which render into Pickle binary.
    """

    mimetype = MimeType('application/pickle')

    def render(self, data, mimetype):
        """
        Serializes a Python object into a Pickle byte array.

        :param data: A Python object.
        :param mimetype: The mimetype to render the data.
        :return: A byte array containing a Pickle document.
        """

        return pickle.dumps(data)
{'fraction_non_alphanumeric': 0.08573436401967674, 'fraction_numerical': 0.0018739751698289998, 'mean_word_length': 2.2770529547198772, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '7525232', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1136, 'n_words': 425}