<commit_before>""" Caching facility for SymPy """

def mycopy(obj, level=0):
    if isinstance(obj, (list, tuple)):
        return obj.__class__(map(mycopy, obj))
    elif isinstance(obj, dict):
        d = obj.__class__()
        for k,v in obj.items():
            d[mycopy(k)] = mycopy(v)
        return d
    return obj


# TODO: refactor CACHE & friends into class?

# global cache registry:
CACHE = []  # [] of
            #    (item, {} or tuple of {})


def cache_it_fast(func):
    func._cache_it_cache = func_cache_it_cache = {}
    CACHE.append((func, func_cache_it_cache))

    def wrapper(*args, **kw_args):
        if kw_args:
            keys = kw_args.keys()
            keys.sort()
            items = [(k+'=',kw_args[k]) for k in keys]
            k = args + tuple(items)
        else:
            k = args
        cache_flag = False
        try:
            r = func_cache_it_cache[k]
        except KeyError:
            r = func(*args, **kw_args)
            cache_flag = True
        if cache_flag:
            func_cache_it_cache[k] = r
        return mycopy(r)
    return wrapper

def cache_it_immutable(func):<commit_msg>cache: utilities to print and clear it  (#656)<commit_after>""" Caching facility for SymPy """

def mycopy(obj, level=0):
    if isinstance(obj, (list, tuple)):
        return obj.__class__(map(mycopy, obj))
    elif isinstance(obj, dict):
        d = obj.__class__()
        for k,v in obj.items():
            d[mycopy(k)] = mycopy(v)
        return d
    return obj


# TODO: refactor CACHE & friends into class?

# global cache registry:
CACHE = []  # [] of
            #    (item, {} or tuple of {})

def print_cache():
    """print cache content"""

    for item, cache in CACHE:
        item = str(item)
        head = '='*len(item)

        print head
        print item
        print head


        if not isinstance(cache, tuple):
            cache = (cache,)
            shown = False
        else:
            shown = True

        for i, kv in enumerate(cache):
            if shown:
                print '\n*** %i ***\n' % i

            for k, v in kv.iteritems():
                print '  %s :\t%s' % (k, v)


def clear_cache():
    """clear cache content"""
    for item, cache in CACHE:
        if not isinstance(cache, tuple):
            cache = (cache,)

        for kv in cache:
            kv.clear()



def cache_it_fast(func):
    func._cache_it_cache = func_cache_it_cache = {}
    CACHE.append((func, func_cache_it_cache))

    def wrapper(*args, **kw_args):
        if kw_args:
            keys = kw_args.keys()
            keys.sort()
            items = [(k+'=',kw_args[k]) for k in keys]
            k = args + tuple(items)
        else:
            k = args
        cache_flag = False
        try:
            r = func_cache_it_cache[k]
        except KeyError:
            r = func(*args, **kw_args)
            cache_flag = True
        if cache_flag:
            func_cache_it_cache[k] = r
        return mycopy(r)
    return wrapper

def cache_it_immutable(func):
{'fraction_non_alphanumeric': 0.11982928430728824, 'fraction_numerical': 0.0016414970453053185, 'mean_word_length': 1.7928505957836847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5353823', 'n_tokens_mistral': 967, 'n_tokens_neox': 941, 'n_words': 266}