<commit_before>"""
Tinman Cache Module
"""
__author__ = "<NAME>"
__email__ = "<EMAIL>"
__date__ = "2010-03-10"
__version__ = 0.2

from logging import debug

# Module wide dictionary to hold the cached values in
local_cache = dict()


# Cache Decorator
def memoize(fn):

    def wrapper(*args):
        # Our module wide local_cache
        global local_cache

        # Get the class name for the key
        key = repr(args[0])

        # Add the arguments to the key
        for value in args[1:]:
            key += ':%s' % str(value)

        debug('memoize: %s' % key)
        if key in local_cache:
            debug('memoize hit: %s' % key)
            return local_cache[key]

        # Call and return the original function
        value = fn(*args)

        # Set the Value
        debug('memoize set: %s' % key)
        local_cache[key] = value

        # Return the value
        return value

    return wrapper


def flush():
    """<commit_msg>Make memoize cache all the output of RequestHandler.write by monkey patching the module into the write and finish code flow<commit_after>"""
Tinman Cache Module
"""
__author__ = "<NAME>"
__email__ = "<EMAIL>"
__date__ = "2010-03-10"
__version__ = 0.2

from functools import wraps
from logging import debug

# Module wide dictionary to hold the cached values in
local_cache = dict()


def memoize_write(*args):

    # Append the value if the key exists, otherwise just set it
    if args[0].tinman_memoize_key in local_cache:
        debug('memoize append: %s' % key)
        local_cache[args[0].tinman_memoize_key] += args[1]
    else:
        debug('memoize set: %s' % key)
        local_cache[args[0].tinman_memoize_key] = args[1]

    # Call the monkey patched RequestHandler.write
    args[0]._write(args[1])


def memoize_finish(*args):

    # If they passed in a last chunk, run the write
    if len(args) > 1:
        memoize_write(args)

    # Un-Monkey-patch
    args[0].write = args[0]._write
    args[0].finish = args[0]._finish

    # Remove the monkey patched attributes
    del args[0]._write
    del args[0]._finish

    # Call the RequestHandler.finish
    args[0].finish()


# Cache Decorator
def memoize(method):

    @wraps(method)
    def wrapper(*args, **kwargs):

        # Our module wide local_cache
        global local_cache

        if not hasattr(args[0], 'write'):
            raise AttributeError("Could not find the ")

        # Get the class name for the key
        key = repr(args[0])

        # Add the arguments to the key
        for value in args[1:]:
            key += ':%s' % str(value)

        debug('memoize: %s' % key)

        # See if the key is in cache and if so, send it
        if key in local_cache:
            debug('memoize hit: %s' % key)
            return self.finish(local_cache[key])

        # Assign our key
        args[0].tinman_memoize_key = key

        # Monkey-patch the write and finish functions
        args[0]._write = args[0].write
        args[0]._finish = args[0].finish
        args[0].write = memoize_write
        args[0].finish = memoize_finish

        # Return the value
        return method(*args, **kwargs)

    return wrapper


def flush():
    """
{'fraction_non_alphanumeric': 0.10491700595051676, 'fraction_numerical': 0.014719699342311305, 'mean_word_length': 2.408751334044824, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '6563487', 'n_tokens_mistral': 1031, 'n_tokens_neox': 979, 'n_words': 319}