From 7198ffb4bcb6c3b73b574661f3dc22063dc35f26 Mon Sep 17 00:00:00 2001 From: Thor Whalen <1906276+thorwhalen@users.noreply.github.com> Date: Sat, 14 Dec 2024 13:35:18 +0100 Subject: [PATCH] fix: Attempt at making cache_this chainable --- dol/tools.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/dol/tools.py b/dol/tools.py index 11a641b5..7807a2a0 100644 --- a/dol/tools.py +++ b/dol/tools.py @@ -149,6 +149,11 @@ def __get__(self, instance, owner=None): # If cache is False, always compute the value return self.func(instance) + cache = self._get_cache(instance) + + return self._get_or_compute(instance, cache) + + def _get_cache(self, instance): try: cache = self.__get_cache(instance) except ( @@ -159,7 +164,9 @@ def __get__(self, instance, owner=None): f"instance to cache {self.attrname!r} property." ) raise TypeError(msg) from None + return cache + def _get_or_compute(self, instance, cache): val = cache.get(self.cache_key, _NOT_FOUND) if val is _NOT_FOUND: with self.lock: @@ -180,6 +187,21 @@ def __get__(self, instance, owner=None): __class_getitem__ = classmethod(GenericAlias) + # TODO: Time-boxed attempt to get a __call__ method to work with the class + # (so that you can chain two cache_this decorators, (problem is that the outer + # expects the inner to be a function, not an instance of CachedProperty, so + # tried to make CachedProperty callable). + # def __call__(self, instance): + # """ + # Call the cached property. + + # :param func: The function to be called. + # :return: The cached property. + # """ + # cache = self._get_cache(instance) + + # return self._get_or_compute(instance, cache) + def cache_this( func: PropertyFunc = None,