<commit_before>from collections import defaultdict

def getCached(data, funct, params = (), kwparams = {}):
    if not data: return None
    if getattr(data, "info", None) == None or data.info["__version__"] != data.version: 
        setattr(data, "info", {"__version__": data.version})

    if data.info.has_key(funct):
        return data.info[funct]
    else:
        if type(funct) != str:
            data.info[funct] = funct(*params, **kwparams)
            return data.info[funct]
        else:
            return None 
         

def setCached(data, name, info):
    if not data: return
    if getattr(data, "info", None) == None or data.info["__version__"] != data.version:
        setattr(data, "info", {"__version__": data.version})
    data.info[name] = info

def delCached(data, name):
    if not data: return
    if getattr(data, "info", None) != None and data.info.has_key(name):
        del data.info[name]
        
class DataHintsCache(object):
    def __init__(self, ):
        self._hints = defaultdict(lambda: defaultdict(list))
        pass
    
    def set_hint(self, data, key, value, weight=1.0):
        attrs = data.domain.variables + data.domain.getmetas().values()
        for attr in attrs:    
            self._hints[key][attr].append((value, weight/len(attrs)))
        
    def get_weighted_hints(self, data, key):
        attrs = data.domain.variables + data.domain.getmetas().values()
        weighted_hints = defaultdict(float)
        for attr in attrs:
            for val, w in self._hints[key][attr]:
                weighted_hints[val] += w
        return sorted(weighted_hints.items(), key=lambda key:key[1], reverse=True)
    
    def get_hint(self, data, key, default=None):
        hints = self.get_weighted_hints(data, key)
        if hints:
            return hints[0][0]
        else:
            return default
        
data_hints = DataHintsCache()
    <commit_msg>Refactored and simplified widgets.utils.datacaching; it now assumes the data immutable<commit_after>from collections import defaultdict
from operator import itemgetter

def getCached(data, funct, params=(), kwparams=None):
    if data is None:
        return None
    info = getattr(data, "__data_cache", None)
    if info is not None:
        if funct in data.info:
            return data.info(funct)
    else:
        info = data.info = {}
    if isinstance(funct, str):
        return None
    if kwparams is None:
        kwparams = {}
    info[funct] = res = funct(*params, **kwparams)
    return res


def setCached(data, name, value):
    if data is None:
        return
    info = getattr(data, "__data_cache")
    if info is None:
        info = data.info = {}
    info[name] = value

def delCached(data, name):
    info = data is not None and getattr(data, "__data_cache")
    if info and name in info:
        del info[name]


class DataHintsCache(object):
    def __init__(self, ):
        self._hints = defaultdict(lambda: defaultdict(list))
        pass

    def set_hint(self, data, key, value, weight=1.0):
        attrs = data.domain.variables + data.domain.getmetas().values()
        for attr in attrs:
            self._hints[key][attr].append((value, weight/len(attrs)))

    def get_weighted_hints(self, data, key):
        attrs = data.domain.variables + data.domain.getmetas().values()
        weighted_hints = defaultdict(float)
        for attr in attrs:
            for val, w in self._hints[key][attr]:
                weighted_hints[val] += w
        return sorted(weighted_hints.items(), key=itemgetter(1), reverse=True)

    def get_hint(self, data, key, default=None):
        hints = self.get_weighted_hints(data, key)
        if hints:
            return hints[0][0]
        else:
            return default

data_hints = DataHintsCache()
{'fraction_non_alphanumeric': 0.117740232312566, 'fraction_numerical': 0.0026399155227032735, 'mean_word_length': 2.5345149253731343, 'pattern_counts': {'":': 2, '<': 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': '2537137', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1169, 'n_words': 310}