Source code for andes.core.model.modelcache

"""
Module for ModelCache.
"""


[docs]class ModelCache: """ Class for caching the return value of callback functions. Check ``ModelCache.__dict__.keys()`` for fields. """
[docs] def __init__(self): self._callbacks = {}
def __getattr__(self, item): if item == "_callbacks": return self.__getattribute__(item) if item not in self.__dict__: if item in self._callbacks: self.__dict__[item] = self._call(item) return self.__getattribute__(item) def __getstate__(self): return self.__dict__
[docs] def add_callback(self, name: str, callback): """ Add a cache attribute and a callback function for updating the attribute. Parameters ---------- name : str name of the cached function return value callback : callable callback function for updating the cached attribute """ self._callbacks[name] = callback
[docs] def refresh(self, name=None): """ Refresh the cached values Parameters ---------- name : str, list, optional name or list of cached to refresh, by default None for refreshing all """ if name is None: for name in self._callbacks.keys(): self.__dict__[name] = self._call(name) elif isinstance(name, str): self.__dict__[name] = self._call(name) elif isinstance(name, list): for n in name: self.__dict__[n] = self._call(n)
def _call(self, name): """ Helper function for calling callback functions. Parameters ---------- name : str attribute name to be updated Returns ------- callback result """ if name not in self._callbacks: return None else: if callable(self._callbacks[name]): return self._callbacks[name]() else: return self._callbacks[name]