summaryrefslogtreecommitdiffstats
path: root/crmsh/pyshim.py
blob: 640ad18c3b1f57df8035c8662a0b89cd4659b08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import functools


try:
    from functools import cache
except ImportError:
    def cache(f):
        cached_return_value = dict()

        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            nonlocal cached_return_value
            key = (tuple(args), tuple(sorted(kwargs.items())))
            try:
                return cached_return_value[key]
            except KeyError:
                ret = f(*args, **kwargs)
                cached_return_value[key] = ret
                return ret

        return wrapper