initial implementation of cachedproperty and tests, addressing #66. docs soon.

This commit is contained in:
Mahmoud Hashemi 2016-05-17 01:32:55 -07:00
parent d14b5a793d
commit 3c1094359d
2 changed files with 45 additions and 1 deletions

View File

@ -572,6 +572,22 @@ def cachedmethod(cache, typed=False, selfish=True):
return cached_method_decorator
class cachedproperty(object):
def __init__(self, func):
self.func = func
def __get__(self, obj, objtype=None):
if obj is None:
return self
value = self.func(obj)
setattr(obj, self.func.__name__, value)
return value
def __repr__(self):
cn = self.__class__.__name__
return '<%s func=%s>' % (cn, self.func)
class ThresholdCounter(object):
"""A **bounded** dict-like Mapping from keys to counts. The
ThresholdCounter automatically compacts after every (1 /

View File

@ -2,7 +2,7 @@
import string
from boltons.cacheutils import LRU, LRI, cached, cachedmethod
from boltons.cacheutils import LRU, LRI, cached, cachedmethod, cachedproperty
class CountingCallable(object):
@ -242,3 +242,31 @@ def test_cachedmethod():
print(repr(car_two.door))
print(repr(Car.door))
return
def test_cachedproperty():
class Proper(object):
def __init__(self):
self.expensive_func = CountingCallable()
@cachedproperty
def useful_attr(self):
return self.expensive_func()
prop = Proper()
assert prop.expensive_func.call_count == 0
assert prop.useful_attr == 1
assert prop.expensive_func.call_count == 1
assert prop.useful_attr == 1
assert prop.expensive_func.call_count == 1
prop.useful_attr += 1 # would not be possible with normal properties
assert prop.useful_attr == 2
delattr(prop, 'useful_attr')
assert prop.expensive_func.call_count == 1
assert prop.useful_attr
assert prop.expensive_func.call_count == 2
repr(Proper.useful_attr)