<commit_before>#!/usr/bin/env python
# serialization.py
#
# Copyright (C) 2008-2018 <NAME>, https://bitdust.io
#
# This file (serialization.py) is part of BitDust Software.
#
# BitDust is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BitDust Software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with BitDust Software.  If not, see <http://www.gnu.org/licenses/>.
#
# Please contact us if you have any questions at <EMAIL>


"""
A possible methods:

    * pickle
    * cPickle
    * msgpack
    * jsonpickle

Some methods are faster, but libraries needs to be pre-compiled and redistributed.
So I decide to use standard pickle module and upgrade that in future.
"""

#------------------------------------------------------------------------------

from __future__ import absolute_import
import six

#------------------------------------------------------------------------------

SERIALIZATION_METHOD = 'fallback'

#------------------------------------------------------------------------------


def ObjectToString(obj):
    """
    """
    if SERIALIZATION_METHOD == 'pickle':
        import six.moves.cPickle as pickle
        return pickle.dumps(obj, protocol=2)
    
    elif SERIALIZATION_METHOD == 'cPickle':
        import six.moves.cPickle
        return six.moves.cPickle.dumps(obj, protocol=2)

    elif SERIALIZATION_METHOD == 'msgpack':
        import msgpack
        return msgpack.dumps(obj)

    elif SERIALIZATION_METHOD == 'jsonpickle':
        import json
        import jsonpickle
        return json.dumps(jsonpickle.encode(obj), ensure_ascii=False)

    else:
        raise Exception('unknown SERIALIZATION_METHOD')


def StringToObject(inp):
    """
    """
    if SERIALIZATION_METHOD == 'pickle':
        import six.moves.cPickle as pickle
        if six.PY2:
            return pickle.loads(inp)
        else:
            return pickle.loads(inp, encoding='bytes')

    elif SERIALIZATION_METHOD == 'cPickle':
        import six.moves.cPickle
        return six.moves.cPickle.loads(inp)

    elif SERIALIZATION_METHOD == 'msgpack':    
        import msgpack
        return msgpack.loads(inp, use_list=False)

    elif SERIALIZATION_METHOD == 'jsonpickle':
        import json
        import jsonpickle
        return jsonpickle.decode(json.loads(inp))

    else:
        raise Exception('unknown SERIALIZATION_METHOD')
<commit_msg>default serialization method back to "pickle"<commit_after>#!/usr/bin/env python
# serialization.py
#
# Copyright (C) 2008-2018 <NAME>, https://bitdust.io
#
# This file (serialization.py) is part of BitDust Software.
#
# BitDust is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BitDust Software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with BitDust Software.  If not, see <http://www.gnu.org/licenses/>.
#
# Please contact us if you have any questions at <EMAIL>


"""
A possible methods:

    * pickle
    * cPickle
    * msgpack
    * jsonpickle

Some methods are faster, but libraries needs to be pre-compiled and redistributed.
So I decide to use standard pickle module and upgrade that in future.
"""

#------------------------------------------------------------------------------

from __future__ import absolute_import
import six

#------------------------------------------------------------------------------

SERIALIZATION_METHOD = 'pickle'

#------------------------------------------------------------------------------


def ObjectToString(obj):
    """
    """
    if SERIALIZATION_METHOD == 'pickle':
        import six.moves.cPickle as pickle
        return pickle.dumps(obj, protocol=2)
    
    elif SERIALIZATION_METHOD == 'cPickle':
        import six.moves.cPickle
        return six.moves.cPickle.dumps(obj, protocol=2)

    elif SERIALIZATION_METHOD == 'msgpack':
        import msgpack
        return msgpack.dumps(obj)

    elif SERIALIZATION_METHOD == 'jsonpickle':
        import json
        import jsonpickle
        return json.dumps(jsonpickle.encode(obj), ensure_ascii=False)

    else:
        raise Exception('unknown SERIALIZATION_METHOD')


def StringToObject(inp):
    """
    """
    if SERIALIZATION_METHOD == 'pickle':
        import six.moves.cPickle as pickle
        if six.PY2:
            return pickle.loads(inp)
        else:
            return pickle.loads(inp, encoding='bytes')

    elif SERIALIZATION_METHOD == 'cPickle':
        import six.moves.cPickle
        return six.moves.cPickle.loads(inp)

    elif SERIALIZATION_METHOD == 'msgpack':    
        import msgpack
        return msgpack.loads(inp, use_list=False)

    elif SERIALIZATION_METHOD == 'jsonpickle':
        import json
        import jsonpickle
        return jsonpickle.decode(json.loads(inp))

    else:
        raise Exception('unknown SERIALIZATION_METHOD')

{'fraction_non_alphanumeric': 0.15904292751583393, 'fraction_numerical': 0.00422237860661506, 'mean_word_length': 2.2448630136986303, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4113838', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1553, 'n_words': 511}