<commit_before>from __future__ import print_function

import os
import time
import numpy as np


class CacheResults(object):
    """Helper for cacheing results of expensive computations"""
    def __init__(self, cache_dir, verbose=0):
        self.cache_dir = os.path.abspath(cache_dir)
        self.verbose = verbose

    def key_to_file(self, f, key):
        return os.path.join(self.cache_dir,
                            "_{0}_{1}.npy".format(f.__name__, key))

    def call(self, f, key, overwrite=False, args=None, kwargs=None):
        if self.verbose:
            print(key, "...", sep="", end=" ", flush=True)
            t0 = time.time()

        if args is None:
            args = ()
        if kwargs is None:
            kwargs = {}

        if not os.path.exists(self.cache_dir):
            os.makedirs(self.cache_dir)

        cache_file = self.key_to_file(f, key)
        if os.path.exists(cache_file) and not overwrite:
            result = np.load(cache_file)
            if self.verbose:
                print("(cached)", flush=True)
        else:
            result = f(key, *args, **kwargs)
            np.save(cache_file, result)
            if self.verbose:
                print("{0:.1f} sec".format(time.time() - t0), flush=True)

        return result<commit_msg>change verbose behavior of chaching<commit_after>from __future__ import print_function

import os
import time
import numpy as np


class CacheResults(object):
    """Helper for cacheing results of expensive computations"""
    def __init__(self, cache_dir, verbose=0):
        self.cache_dir = os.path.abspath(cache_dir)
        self.verbose = verbose

    def key_to_file(self, f, key):
        return os.path.join(self.cache_dir,
                            "_{0}_{1}.npy".format(f.__name__, key))

    def call(self, f, key, overwrite=False, args=None, kwargs=None):
        if args is None:
            args = ()
        if kwargs is None:
            kwargs = {}

        if not os.path.exists(self.cache_dir):
            os.makedirs(self.cache_dir)

        cache_file = self.key_to_file(f, key)
        if os.path.exists(cache_file) and not overwrite:
            result = np.load(cache_file)
        else:
            if self.verbose:
                print(key, "...", sep="", end=" ", flush=True)
                t0 = time.time()
            result = f(key, *args, **kwargs)
            np.save(cache_file, result)
            if self.verbose:
                print("{0:.1f} sec".format(time.time() - t0), flush=True)

        return result
{'fraction_non_alphanumeric': 0.12037765538945712, 'fraction_numerical': 0.0055074744295830055, 'mean_word_length': 2.009467455621302, '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': '3064915', 'n_tokens_mistral': 789, 'n_tokens_neox': 761, 'n_words': 189}