def __call__(self, *args, **kwargs): cache_key = (force_hashable(args), force_hashable(kwargs.items())) if cache_key in self.cache: return self.cache[cache_key] else: value = self.func(*args, **kwargs) self.cache[cache_key] = value return value
def force_frozenset(obj): """Force frozenset() command to freeze the order and contents of multables and iterables like lists, dicts, generators Useful for memoization and constructing dicts or hashtables where keys must be immutable """ # make it a set/tuple of 1 if it is a scalar and not a set already return tuple(force_hashable(obj))