Revert cached_property_with_ttl/threaded_cached_property_with_ttl to settable just...
[cached-property.git] / cached_property.py
1 # -*- coding: utf-8 -*-
2
3 __author__ = 'Daniel Greenfeld'
4 __email__ = 'pydanny@gmail.com'
5 __version__ = '1.0.0'
6 __license__ = 'BSD'
7
8 from time import time
9 import threading
10
11
12 class cached_property(object):
13 """
14 A property that is only computed once per instance and then replaces itself
15 with an ordinary attribute. Deleting the attribute resets the property.
16 Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
17 """ # noqa
18
19 def __init__(self, func):
20 self.__doc__ = getattr(func, '__doc__')
21 self.func = func
22
23 def __get__(self, obj, cls):
24 if obj is None:
25 return self
26 value = obj.__dict__[self.func.__name__] = self.func(obj)
27 return value
28
29
30 class threaded_cached_property(object):
31 """
32 A cached_property version for use in environments where multiple threads
33 might concurrently try to access the property.
34 """
35
36 def __init__(self, func):
37 self.__doc__ = getattr(func, '__doc__')
38 self.func = func
39 self.lock = threading.RLock()
40
41 def __get__(self, obj, cls):
42 if obj is None:
43 return self
44
45 obj_dict = obj.__dict__
46 name = self.func.__name__
47 with self.lock:
48 try:
49 # check if the value was computed before the lock was acquired
50 return obj_dict[name]
51 except KeyError:
52 # if not, do the calculation and release the lock
53 return obj_dict.setdefault(name, self.func(obj))
54
55
56 class cached_property_with_ttl(object):
57 """
58 A property that is only computed once per instance and then replaces itself
59 with an ordinary attribute. Setting the ttl to a number expresses how long
60 the property will last before being timed out.
61 """
62
63 def __init__(self, ttl=None):
64 if callable(ttl):
65 func = ttl
66 ttl = None
67 else:
68 func = None
69 self.ttl = ttl
70 self._prepare_func(func)
71
72 def __call__(self, func):
73 self._prepare_func(func)
74 return self
75
76 def __get__(self, obj, cls):
77 if obj is None:
78 return self
79
80 now = time()
81 obj_dict = obj.__dict__
82 name = self.__name__
83 try:
84 value, last_updated = obj_dict[name]
85 except KeyError:
86 pass
87 else:
88 ttl_expired = 0 < self.ttl < now - last_updated
89 if not ttl_expired:
90 return value
91
92 value = self.func(obj)
93 obj_dict[name] = (value, now)
94 return value
95
96 def __delete__(self, obj):
97 obj.__dict__.pop(self.__name__, None)
98
99 def __set__(self, obj, value):
100 obj.__dict__[self.__name__] = (value, time())
101
102 def _prepare_func(self, func):
103 self.func = func
104 if func:
105 self.__doc__ = func.__doc__
106 self.__name__ = func.__name__
107 self.__module__ = func.__module__
108
109 # Aliases to make cached_property_with_ttl easier to use
110 cached_property_ttl = cached_property_with_ttl
111 timed_cached_property = cached_property_with_ttl
112
113
114 class threaded_cached_property_with_ttl(cached_property_with_ttl):
115 """
116 A cached_property version for use in environments where multiple threads
117 might concurrently try to access the property.
118 """
119
120 def __init__(self, ttl=None):
121 super(threaded_cached_property_with_ttl, self).__init__(ttl)
122 self.lock = threading.RLock()
123
124 def __get__(self, obj, cls):
125 with self.lock:
126 return super(threaded_cached_property_with_ttl, self).__get__(obj,
127 cls)
128
129 # Alias to make threaded_cached_property_with_ttl easier to use
130 threaded_cached_property_ttl = threaded_cached_property_with_ttl
131 timed_threaded_cached_property = threaded_cached_property_with_ttl