Using @functools.lru_cache with dictionary arguments Using @functools.lru_cache with dictionary arguments python python

Using @functools.lru_cache with dictionary arguments


Instead of using a custom hashable dictionary, use this and avoid reinventing the wheel! It's a frozen dictionary that's all hashable.

https://pypi.org/project/frozendict/

Code:

def freezeargs(func):    """Transform mutable dictionnary    Into immutable    Useful to be compatible with cache    """    @functools.wraps(func)    def wrapped(*args, **kwargs):        args = tuple([frozendict(arg) if isinstance(arg, dict) else arg for arg in args])        kwargs = {k: frozendict(v) if isinstance(v, dict) else v for k, v in kwargs.items()}        return func(*args, **kwargs)    return wrapped

and then

@freezeargs@lru_cachedef func(...):    pass

Code taken from @fast_cen 's answer

Note: this does not work on recursive datastructures; for example, you might have an argument that's a list, which is unhashable. You are invited to make the wrapping recursive, such that it goes deep into the data structure and makes every dict frozen and every list tuple.

(I know that OP nolonger wants a solution, but I came here looking for the same solution, so leaving this for future generations)


Here is a decorator that use @mhyfritz trick.

def hash_dict(func):    """Transform mutable dictionnary    Into immutable    Useful to be compatible with cache    """    class HDict(dict):        def __hash__(self):            return hash(frozenset(self.items()))    @functools.wraps(func)    def wrapped(*args, **kwargs):        args = tuple([HDict(arg) if isinstance(arg, dict) else arg for arg in args])        kwargs = {k: HDict(v) if isinstance(v, dict) else v for k, v in kwargs.items()}        return func(*args, **kwargs)    return wrapped

Simply add it before your lru_cache.

@hash_dict@functools.lru_cache()def your_function():    ...


What about creating a hashable dict class like so:

class HDict(dict):    def __hash__(self):        return hash(frozenset(self.items()))substs = HDict({'foo': 'bar', 'baz': 'quz'})cache = {substs: True}