<commit_before># -*- coding: utf-8 -*-
#
# Copyright (c) 2012 TSH Labs <<EMAIL>>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are
# met:
# 
# * Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


"""
Immutable wrappers for model objects suitable for being rendered
as JSON and an encoder for doing so.
"""


try:
    import simplejson as json
except ImportError:
    import json


__all__ = [
    'IdNameElm',
    'JSONEncoder',
    'TrackElm'
    ]


class JSONEncoder(json.JSONEncoder):
    
    """ Special JSON encoder to convert an iterable of
        IdNameElm objects into a list of dictionaries.
    """

    def default(self, res):
        """ Return a list of dictionaries representing IdNameElm
            of TrackElm objects.
        """
        return [self._render_elm(o) for o in res]

    def _render_elm(self, o):
        """ Return a dictionary representation of an element.
        """
        obj = {
            'id': o.id,
            'name': o.name
            }
        
        if isinstance(o, TrackElm):
            obj['track'] = o.track
            obj['year'] = o.year
            obj['album'] = o.album
            obj['album_id'] = o.album_id
            obj['artist'] = o.artist
            obj['artist_id'] = o.artist_id
            obj['genre'] = o.genre
            obj['genre_id'] = o.genre_id
        return obj


class IdNameElm(object):

    """ Immutable, hashable representation of a model with ID
        and name attributes (everything besides Tracks).
    """

    def __init__(self, elm_id, elm_name):
        """ Set the element attributes.
        """
        self._id = elm_id
        self._name = elm_name

    @property
    def id(self):
        """ Element ID.
        """
        return self._id

    @property
    def name(self):
        """ Element name.
        """
        return self._name

    def __eq__(self, o):
        """ Two instances are equal based on ID and name.
        """
        if not isinstance(o, self.__class__):
            return False
        return o.id == self.id and o.name == self.name

    def __hash__(self):
       """ Hash is computed from ID, name, and class.
       """
       return hash(self.id) ^ (hash(self.name) * 7) ^ (hash(self.__class__) * 31)


class TrackElm(IdNameElm):

    """ Immutable, hashable representation of a Track model.
    """

    def __init__(self, t_id, t_name, t_track, t_year,
                 t_album, t_album_id, t_artist, t_artist_id,
                 t_genre, t_genre_id):
        """ Set the track attributes.
        """
        super(TrackElm, self).__init__(t_id, t_name)

        self._track = t_track
        self._year = t_year
        self._album = t_album
        self._album_id = t_album_id
        self._artist = t_artist
        self._artist_id = t_artist_id
        self._genre = t_genre
        self._genre_id = t_genre_id

    @property
    def track(self):
        """ Track track number.
        """
        return self._track

    @property
    def year(self):
        """ Track year.
        """
        return self._year

    @property
    def album(self):
        """ Track album name.
        """
        return self._album

    @property
    def album_id(self):
        """ Track album ID.
        """
        return self._album_id

    @property
    def artist(self):
        """ Track artist name.
        """
        return self._artist

    @property
    def artist_id(self):
        """ Track artist ID.
        """
        return self._artist_id
    
    @property
    def genre(self):
        """ Track genre name.
        """
        return self._genre
    
    @property
    def genre_id(self):
        """ Track genre ID.
        """
        return self._genre_id

<commit_msg>Comments<commit_after># -*- coding: utf-8 -*-
#
# Copyright (c) 2012 TSH Labs <<EMAIL>>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are
# met:
# 
# * Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


"""
Immutable wrappers for model objects suitable for being rendered
as JSON and an encoder for doing so.
"""


try:
    import simplejson as json
except ImportError:
    import json


__all__ = [
    'IdNameElm',
    'JSONEncoder',
    'TrackElm'
    ]


class JSONEncoder(json.JSONEncoder):
    
    """ Special JSON encoder to convert the output format from
        services used by the web layer to datatypes that can be
        easily rendered.
    """

    def default(self, res):
        """ Return a list of dictionaries representing IdNameElm
            of TrackElm objects.
        """
        return [self._render_elm(o) for o in res]

    def _render_elm(self, o):
        """ Return a dictionary representation of an element.
        """
        obj = {
            'id': o.id,
            'name': o.name
            }
        
        if isinstance(o, TrackElm):
            obj['track'] = o.track
            obj['year'] = o.year
            obj['album'] = o.album
            obj['album_id'] = o.album_id
            obj['artist'] = o.artist
            obj['artist_id'] = o.artist_id
            obj['genre'] = o.genre
            obj['genre_id'] = o.genre_id
        return obj


class IdNameElm(object):

    """ Immutable, hashable representation of a model with ID
        and name attributes (everything besides Tracks).
    """

    def __init__(self, elm_id, elm_name):
        """ Set the element attributes.
        """
        self._id = elm_id
        self._name = elm_name

    @property
    def id(self):
        """ Element ID.
        """
        return self._id

    @property
    def name(self):
        """ Element name.
        """
        return self._name

    def __eq__(self, o):
        """ Two instances are equal based on ID and name.
        """
        if not isinstance(o, self.__class__):
            return False
        return o.id == self.id and o.name == self.name

    def __hash__(self):
       """ Hash is computed from ID, name, and class.
       """
       return hash(self.id) ^ (hash(self.name) * 7) ^ (hash(self.__class__) * 31)


class TrackElm(IdNameElm):

    """ Immutable, hashable representation of a Track model.
    """

    def __init__(self, t_id, t_name, t_track, t_year,
                 t_album, t_album_id, t_artist, t_artist_id,
                 t_genre, t_genre_id):
        """ Set the track attributes.
        """
        super(TrackElm, self).__init__(t_id, t_name)

        self._track = t_track
        self._year = t_year
        self._album = t_album
        self._album_id = t_album_id
        self._artist = t_artist
        self._artist_id = t_artist_id
        self._genre = t_genre
        self._genre_id = t_genre_id

    @property
    def track(self):
        """ Track track number.
        """
        return self._track

    @property
    def year(self):
        """ Track year.
        """
        return self._year

    @property
    def album(self):
        """ Track album name.
        """
        return self._album

    @property
    def album_id(self):
        """ Track album ID.
        """
        return self._album_id

    @property
    def artist(self):
        """ Track artist name.
        """
        return self._artist

    @property
    def artist_id(self):
        """ Track artist ID.
        """
        return self._artist_id
    
    @property
    def genre(self):
        """ Track genre name.
        """
        return self._genre
    
    @property
    def genre_id(self):
        """ Track genre ID.
        """
        return self._genre_id


{'fraction_non_alphanumeric': 0.09271790728209271, 'fraction_numerical': 0.001615998384001616, 'mean_word_length': 2.161558109833972, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '455586', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2755, 'n_words': 979}