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

""" PyString v1.0
Python 2 & 3 compatible string class.

Usage:
    >>> string = String("hoge") #py2 or 3
    >>> string = String(u"hoge") #py2
    >>> string = String(b"hoge") #py3
"""

import six

class String(str):
    """内部でstr->unicode, bytes->str変換する。
    """
    def __new__(cls, value="", encoding="utf-8", *args, **kwargs):
        # str
        if six.PY2 and isinstance(value, str):
            return str.__new__(str, value, *args, **kwargs).decode(encoding)

        if six.PY3 and isinstance(value, str):
            return str.__new__(str, value, *args, **kwargs)

        # PY3 bytes
        if six.PY3 and not isinstance(value, str):
            # encode once, and decode again, 'cause str() doesn't accept bytes
            value = value.decode(encoding)
            return str.__new__(str, value, *args, **kwargs)

        # PY2 unicode 
        # encode once, and decode again, 'cause str() doesn't accept unicode
        value = value.encode(encoding)
        return str.__new__(str, value, *args, **kwargs).decode(encoding)

    def encode(self, *args, **kwargs):
        return super(String, self).encode(*args, **kwargs)


if __name__ == "__main__":
    # py3
    #assert String("рщпу") == "рщпу"
    #assert String("рщпу".encode("utf-8")) == "рщпу"

    # py2
    assert String("рщпу") == u"рщпу"
    assert String(u"рщпу") == u"рщпу"
    s = String(u"ыпрущ")
    with open("./ru.txt", "wb") as fd:
        fd.write(s.encode("utf-8"))
<commit_msg>remove trash<commit_after>#-*- coding:utf-8 -*-

""" PyString v1.0
Python 2 & 3 compatible string class.

Usage:
    >>> string = String("hoge") #py2 or 3
    >>> string = String(u"hoge") #py2
    >>> string = String(b"hoge") #py3
"""

import six

class String(str):
    """内部でstr->unicode, bytes->str変換する。
    """
    def __new__(cls, value="", encoding="utf-8", *args, **kwargs):
        # str
        if six.PY2 and isinstance(value, str):
            return str.__new__(str, value, *args, **kwargs).decode(encoding)

        if six.PY3 and isinstance(value, str):
            return str.__new__(str, value, *args, **kwargs)

        # PY3 bytes
        if six.PY3 and not isinstance(value, str):
            # encode once, and decode again, 'cause str() doesn't accept bytes
            value = value.decode(encoding)
            return str.__new__(str, value, *args, **kwargs)

        # PY2 unicode 
        # encode once, and decode again, 'cause str() doesn't accept unicode
        value = value.encode(encoding)
        return str.__new__(str, value, *args, **kwargs).decode(encoding)

    def encode(self, *args, **kwargs):
        return super(String, self).encode(*args, **kwargs)

{'fraction_non_alphanumeric': 0.16050295857988164, 'fraction_numerical': 0.01257396449704142, 'mean_word_length': 2.6852861035422344, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 25, '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': '4181332', 'n_tokens_mistral': 900, 'n_tokens_neox': 874, 'n_words': 240}