File size: 823 Bytes
0220cd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Helpers for properties."""


def with_doc(f):
    return property(f, None, None, f.__doc__)


# copied over unmodified from
# https://github.com/devpi/devpi/blob/master/common/devpi_common/types.py
def cached(f):
    """returns a cached property that is calculated by function f."""

    def get(self):
        try:
            return self._property_cache[f]
        except AttributeError:
            self._property_cache = {}
        except KeyError:
            pass
        res = f(self)
        self._property_cache[f] = res
        return res

    def set(self, val):
        propcache = self.__dict__.setdefault("_property_cache", {})
        propcache[f] = val

    def fdel(self):
        propcache = self.__dict__.setdefault("_property_cache", {})
        del propcache[f]

    return property(get, set, fdel)