Skip to content

Commit

Permalink
feat(cache): remove deprecated cached_property in favour of functools…
Browse files Browse the repository at this point in the history
….cached_property
  • Loading branch information
ljgray committed Aug 2, 2024
1 parent c7f8de8 commit b4cfc46
Showing 1 changed file with 0 additions and 49 deletions.
49 changes: 0 additions & 49 deletions caput/cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Tools for caching expensive calculations."""

import weakref

import numpy as np
from cachetools import LRUCache

Expand All @@ -26,50 +24,3 @@ def _array_size(arr: np.ndarray):
return arr.nbytes

super().__init__(maxsize=size_bytes, getsizeof=_array_size)


class cached_property:
"""An immutable cached property.
This exists in Python 3.8, and later but we're restricted to Python 3.6.
Example
-------
>>> class A:
... @cached_property
... def v(self):
... print("I'm in here")
... return 2
>>> a = A()
>>> a.v
I'm in here
2
>>> a.v
2
>>> a.v = 4
Traceback (most recent call last):
...
AttributeError: Can't set attribute
"""

def __init__(self, func, doc=None):
self.func = func
if doc is None:
doc = func.__doc__
self.__doc__ = doc
self._value_cache = weakref.WeakKeyDictionary()

def __get__(self, obj, objtype=None):
if obj is None:
return self

try:
return self._value_cache[obj]
except KeyError:
val = self.func(obj)
self._value_cache[obj] = val
return val

def __set__(self, *args):
raise AttributeError("Can't set attribute")

0 comments on commit b4cfc46

Please sign in to comment.