Property that maps to a key in a local dict-like attribute.
| 188 | |
| 189 | |
| 190 | class DictProperty(object): |
| 191 | """ Property that maps to a key in a local dict-like attribute. """ |
| 192 | |
| 193 | def __init__(self, attr, key=None, read_only=False): |
| 194 | self.attr, self.key, self.read_only = attr, key, read_only |
| 195 | |
| 196 | def __call__(self, func): |
| 197 | functools.update_wrapper(self, func, updated=[]) |
| 198 | self.getter, self.key = func, self.key or func.__name__ |
| 199 | return self |
| 200 | |
| 201 | def __get__(self, obj, cls): |
| 202 | if obj is None: return self |
| 203 | key, storage = self.key, getattr(obj, self.attr) |
| 204 | if key not in storage: storage[key] = self.getter(obj) |
| 205 | return storage[key] |
| 206 | |
| 207 | def __set__(self, obj, value): |
| 208 | if self.read_only: raise AttributeError("Read-Only property.") |
| 209 | getattr(obj, self.attr)[self.key] = value |
| 210 | |
| 211 | def __delete__(self, obj): |
| 212 | if self.read_only: raise AttributeError("Read-Only property.") |
| 213 | del getattr(obj, self.attr)[self.key] |
| 214 | |
| 215 | |
| 216 | class cached_property(object): |
no outgoing calls
no test coverage detected
searching dependent graphs…