<commit_before># processors.py
# Copyright (C) 2010 <NAME> <EMAIL>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

"""defines generic type conversion functions, as used in result processors.

They all share one common characteristic: None is passed through unchanged.

"""

import codecs
import re
import datetime

def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    def process(value):
        if value is None:
            return None
        else:
            return type_(*map(int, rmatch(value).groups(0)))
    return process

try:
    from sqlalchemy.cprocessors import UnicodeResultProcessor, \
                                       DecimalResultProcessor, \
                                       to_float, to_str, int_to_boolean, \
                                       str_to_datetime, str_to_time, \
                                       str_to_date

    def to_unicode_processor_factory(encoding, error=None):
        # TODO: this is cumbersome
        if error is not None:
            return UnicodeResultProcessor(encoding, error).process
        else:
            return UnicodeResultProcessor(encoding).process

    def to_decimal_processor_factory(target_class):
        return DecimalResultProcessor(target_class).process

except ImportError:
    def to_unicode_processor_factory(encoding, error=None):
        decoder = codecs.getdecoder(encoding)

        def process(value):
            if value is None:
                return None
            else:
                # decoder returns a tuple: (value, len). Simply dropping the
                # len part is safe: it is done that way in the normal
                # 'xx'.decode(encoding) code path.
                # cfr python-source/Python/codecs.c:PyCodec_Decode
                return decoder(value, error)[0]<commit_msg>rename "error" kwarg to "errors" to be consistent with builtin unicode conversion functions.<commit_after># processors.py
# Copyright (C) 2010 <NAME> <EMAIL>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

"""defines generic type conversion functions, as used in result processors.

They all share one common characteristic: None is passed through unchanged.

"""

import codecs
import re
import datetime

def str_to_datetime_processor_factory(regexp, type_):
    rmatch = regexp.match
    # Even on python2.6 datetime.strptime is both slower than this code
    # and it does not support microseconds.
    def process(value):
        if value is None:
            return None
        else:
            return type_(*map(int, rmatch(value).groups(0)))
    return process

try:
    from sqlalchemy.cprocessors import UnicodeResultProcessor, \
                                       DecimalResultProcessor, \
                                       to_float, to_str, int_to_boolean, \
                                       str_to_datetime, str_to_time, \
                                       str_to_date

    def to_unicode_processor_factory(encoding, errors=None):
        # this is cumbersome but it would be even more so on the C side
        if errors is not None:
            return UnicodeResultProcessor(encoding, errors).process
        else:
            return UnicodeResultProcessor(encoding).process

    def to_decimal_processor_factory(target_class):
        return DecimalResultProcessor(target_class).process

except ImportError:
    def to_unicode_processor_factory(encoding, errors=None):
        decoder = codecs.getdecoder(encoding)

        def process(value):
            if value is None:
                return None
            else:
                # decoder returns a tuple: (value, len). Simply dropping the
                # len part is safe: it is done that way in the normal
                # 'xx'.decode(encoding) code path.
                # cfr python-source/Python/codecs.c:PyCodec_Decode
                return decoder(value, errors)[0]
{'fraction_non_alphanumeric': 0.06693760459000717, 'fraction_numerical': 0.0038250059765718384, 'mean_word_length': 2.1841704718417048, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7530091', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1037, 'n_words': 345}