<commit_before># -*- coding: utf-8 -*-

#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       https://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.

"""linebot.models.base module."""

from __future__ import unicode_literals

import json

from .. import utils


class Base(object):
    """Base class of model.

    Suitable for JSON base data.
    """

    def __init__(self, **kwargs):
        """__init__ method.

        :param kwargs:
        """
        pass

    def __str__(self):
        """__str__ method.

        :return:
        """
        return self.as_json_string()

    def __repr__(self):
        """__repr__ method.

        :return:
        """
        return str(self)

    def __eq__(self, other):
        """__eq__ method.

        :param other:
        :return:
        """
        return other and self.as_json_dict() == other.as_json_dict()

    def __ne__(self, other):
        """__ne__ method.

        :param other:
        :return:
        """
        return not self.__eq__(other)

    def as_json_string(self):
        """Return JSON string from this object.

        :rtype: str
        :return:
        """
        return json.dumps(self.as_json_dict(), sort_keys=True)

    def as_json_dict(self):
        """Return dictionary from this object.

        :return: dict
        """
        data = {}
        for key in self.__dict__.keys():
            camel_key = utils.to_camel_case(key)
            if isinstance(getattr(self, key, None), (list, tuple, set)):
                data[camel_key] = list()
                for sub_obj in getattr(self, key, None):
                    if getattr(sub_obj, 'as_json_dict', None):
                        data[camel_key].append(sub_obj.as_json_dict())
                    else:
                        data[camel_key].append(sub_obj)

            elif getattr(getattr(self, key, None), 'as_json_dict', None):
                data[camel_key] = getattr(self, key).as_json_dict()

            else:
                data[camel_key] = getattr(self, key, None)

        return data

    @classmethod
    def new_from_json_dict(cls, data):
        """Create a new instance from a dict.

        :param data: JSON dict
        :rtype:
        :return:
        """
        new_data = {utils.to_snake_case(key): value
                    for key, value in data.items()}

        return cls(**new_data)

    @staticmethod
    def get_or_new_from_json_dict(data, cls):
        """Get `cls` object w/ deserialization from json if needed.
<commit_msg>modify code lines for as_json_dict method in Base class (#95)<commit_after># -*- coding: utf-8 -*-

#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       https://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.

"""linebot.models.base module."""

from __future__ import unicode_literals

import json

from .. import utils


class Base(object):
    """Base class of model.

    Suitable for JSON base data.
    """

    def __init__(self, **kwargs):
        """__init__ method.

        :param kwargs:
        """
        pass

    def __str__(self):
        """__str__ method.

        :return:
        """
        return self.as_json_string()

    def __repr__(self):
        """__repr__ method.

        :return:
        """
        return str(self)

    def __eq__(self, other):
        """__eq__ method.

        :param other:
        :return:
        """
        return other and self.as_json_dict() == other.as_json_dict()

    def __ne__(self, other):
        """__ne__ method.

        :param other:
        :return:
        """
        return not self.__eq__(other)

    def as_json_string(self):
        """Return JSON string from this object.

        :rtype: str
        :return:
        """
        return json.dumps(self.as_json_dict(), sort_keys=True)

    def as_json_dict(self):
        """Return dictionary from this object.

        :return: dict
        """
        data = {}
        for key, value in self.__dict__.items():
            camel_key = utils.to_camel_case(key)
            if isinstance(value, (list, tuple, set)):
                data[camel_key] = list()
                for item in value:
                    if hasattr(item, 'as_json_dict'):
                        data[camel_key].append(item.as_json_dict())
                    else:
                        data[camel_key].append(item)

            elif hasattr(value, 'as_json_dict'):
                data[camel_key] = value.as_json_dict()

            else:
                data[camel_key] = value

        return data

    @classmethod
    def new_from_json_dict(cls, data):
        """Create a new instance from a dict.

        :param data: JSON dict
        :rtype:
        :return:
        """
        new_data = {utils.to_snake_case(key): value
                    for key, value in data.items()}

        return cls(**new_data)

    @staticmethod
    def get_or_new_from_json_dict(data, cls):
        """Get `cls` object w/ deserialization from json if needed.

{'fraction_non_alphanumeric': 0.1048951048951049, 'fraction_numerical': 0.0020467337540508272, 'mean_word_length': 1.9796747967479675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '6850067', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1681, 'n_words': 513}