adding read-from-config boilerplate

This commit is contained in:
2019-12-02 00:23:00 +00:00
parent 24e42f47b9
commit e2f07a0d9f
4 changed files with 276 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
"""Caching utilities."""
class cachedproperty:
"""A decorator for caching a property's result.
Similar to `property`, but the wrapped method's result is cached
on the instance. This is achieved by setting an entry in the object's
instance dictionary with the same name as the property. When the name
is later accessed, the value in the instance dictionary takes precedence
over the (non-data descriptor) property.
This is useful for implementing lazy-loaded properties.
The cache can be invalidated via `delattr()`, or by modifying `__dict__`
directly. It will be repopulated on next access.
.. versionadded:: 6.3.0
"""
def __init__(self, func, doc=None):
"""Initialize the descriptor."""
self.func = self.__wrapped__ = func
if doc is None:
doc = func.__doc__
self.__doc__ = doc
def __get__(self, obj, objtype=None):
"""Implement descriptor getter.
Calculate the property's value and then store it in the
associated object's instance dictionary.
"""
if obj is None:
return self
value = obj.__dict__[self.func.__name__] = self.func(obj)
return value
def __repr__(self):
"""Return repr(self)."""
return "<%s %s>" % (self.__class__.__name__, self.func)