mirror of https://github.com/mahmoud/boltons.git
initial implementation of cachedproperty and tests, addressing #66. docs soon.
This commit is contained in:
parent
d14b5a793d
commit
3c1094359d
|
@ -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 /
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue