<commit_before>""" ..
"""
import json
import datetime
import six
import pickle
import base64
from .http import HttpResponse
from . import exceptions, transcoders, utils
from lxml.builder import E
from lxml import etree
from collections import Iterable, OrderedDict

class Encoder(transcoders.Transcoder):

    @classmethod
    def encode(cls, obj=None):
        """
        Transforms objects into an acceptable format for tansmission.

        @returns
            An HttpResponse object containing all neccessary information
            that can be provided.
        """
        response = HttpResponse()
        if obj is not None:
            # We have an object; we need to encode it and set content
            # type, etc
            response.content = obj
            response['Content-Type'] = cls.mimetype

        # Pass on the constructed response
        return response


@Encoder.register()
class Json(transcoders.Json, Encoder):

    @staticmethod
    def default(obj):
        if isinstance(obj, datetime.datetime) \
                or isinstance(obj, datetime.date) \
                or isinstance(obj, datetime.time):
            return obj.isoformat()

        raise TypeError('{} is not JSON encodable.'.format(obj))

    @classmethod
    def encode(cls, obj=None):
        # Is this not a dictionary or an array?
        if not isinstance(obj, dict) and not isinstance(obj, list):
            # We need this to be at least a list for valid JSON
            obj = obj,

        # Encode nicely
        text = json.dumps(obj,
                ensure_ascii=True,
                separators=(',', ':'),
                default=cls.default
            )

        # Encode it normally; move along
        return super(Json, cls).encode(text)


#This is simply a procedural call. Not a member function
def _encode_file_into_xml(e,obj):
        data = obj.read()
        base64_string = base64.b64encode(data)
        e.text = base64_string


@Encoder.register()
class Xml(transcoders.Xml, Encoder):

       #for each item in obj<commit_msg>the JSON encoder no longer chokes on binary files.  base64 is used.<commit_after>""" ..
"""
import json
import datetime
import six
import pickle
import base64
from .http import HttpResponse
from . import exceptions, transcoders, utils
from lxml.builder import E
from lxml import etree
from collections import Iterable, OrderedDict

class Encoder(transcoders.Transcoder):

    @classmethod
    def encode(cls, obj=None):
        """
        Transforms objects into an acceptable format for tansmission.

        @returns
            An HttpResponse object containing all neccessary information
            that can be provided.
        """
        response = HttpResponse()
        if obj is not None:
            # We have an object; we need to encode it and set content
            # type, etc
            response.content = obj
            response['Content-Type'] = cls.mimetype

        # Pass on the constructed response
        return response


@Encoder.register()
class Json(transcoders.Json, Encoder):

    @staticmethod
    def default(obj):
        if isinstance(obj, datetime.datetime) \
                or isinstance(obj, datetime.date) \
                or isinstance(obj, datetime.time):
            return obj.isoformat()

        raise TypeError('{} is not JSON encodable.'.format(obj))

    @classmethod
    def encode(cls, obj=None):
        
        # test if file
        try:
#            data = obj.read()
            obj = base64.b64encode(obj.read())
        except:
            pass

        # Is this not a dictionary or an array?
        if not isinstance(obj, dict) and not isinstance(obj, list):
            # We need this to be at least a list for valid JSON
            obj = obj,

        # Encode nicely
        text = json.dumps(obj,
                ensure_ascii=True,
                separators=(',', ':'),
                default=cls.default
            )

        # Encode it normally; move along
        return super(Json, cls).encode(text)


#This is simply a procedural call. Not a member function
def _encode_file_into_xml(e,obj):
        data = obj.read()
        base64_string = base64.b64encode(data)
        e.text = base64_string


@Encoder.register()
class Xml(transcoders.Xml, Encoder):

       #for each item in obj
{'fraction_non_alphanumeric': 0.0817318823801806, 'fraction_numerical': 0.006019912016670526, 'mean_word_length': 2.1146359048305694, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '206069', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1132, 'n_words': 402}