1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
# cachedproperty.py -- a property that caches its asynchronously computed value
#
# Copyright (c) 2012 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# See http://www.gnu.org/licenses/ for more information.
"""
An advanced property that computes and caches expensive operations
(such as running an external command and reading its output).
A callback when a value is computed/read is also supported.
You may inherit from CachedProperty to implement the logic to compute
or retrieve the value, or you may use the CachedProperty as a decorator,
where the function is called once to return the result or to assign it
to the property.
If you are using the property as a descriptor e.g.:
import cachedproperty
class MyClass(object):
version = cachedproperty.cachedproperty()
obj = MyClass()
you can retrieve the value with
obj.version()
Note that, unlike the Python 'property' built-in, parentheses are needed to get
the value. Without parentheses the property itself is returned, which has some
additional features. obj.version() is equivalent to obj.version.get()
If the returned value is None, the property is considered to be unset (i.e.
not set or computed yet). You can force the value to be computed with:
obj.version.start()
(and be notified when set with:
obj.version.computed.connect(myfunction)
), and delete the value with:
del obj.version
You can also assign a value:
obj.version = 123
In that case, the value will not be computed anymore.
If you want the value now or later, you can also use
obj.version.callback(myfunction)
which will call myfunction either now or later with the value.
Properties can also depend on each other:
class MyClass(object):
@cachedproperty.cachedproperty
def command(self):
# ....
@cachedproperty.cachedproperty(depends=command)
def version(self):
cmd = self.command()
# command has already been computed when this function runs.
When used this way, the function can either return the value for the property
or set if directly. If the function returns None, it is assumed to set the
property by itself now or later. If the function returns a different value, the
default implementation sets the property to the returned value.
This module uses the signals module for the callback logic.
"""
import weakref
import signals
class CachedProperty(object):
"""An advanced property that can compute and cache expensive operations.
This can be used to e.g. run an external command and read its output.
A callback when a value is computed/read is also supported, either via
the callback() method or the computed() signal.
"""
# descriptor part
@classmethod
def cachedproperty(cls, func=None, depends=None):
"""Decorator to make cached properties."""
if func is not None:
return cls(func, depends)
elif depends is None:
return cls
def decorator(func):
return cls(func, depends)
return decorator
def __init__(self, func=None, depends=None):
"""Initialize the property/descriptor."""
self._func = func
if depends is None:
self._depends = ()
elif not isinstance(depends, (tuple, list)):
self._depends = (depends,)
else:
self._depends = depends
self._state = weakref.WeakKeyDictionary()
def __get__(self, instance, cls=None):
if instance is None:
return self._func or self
return self.bound(instance)
def __set__(self, instance, value):
self.__get__(instance).set(value)
def __delete__(self, instance):
self.__get__(instance).unset()
def bound(self, instance):
"""Returns a bound instance."""
cls = type(self)
prop = cls.__new__(cls)
prop._instance = instance
prop._property = self
return prop
# instance part
class State(object):
signal = signals.Signal()
def __init__(self):
self.value = None
self.running = False
def state(self):
"""Returns the state for the instance."""
instance = self.instance()
d = self._property._state
try:
state = d[instance]
except KeyError:
state = d[instance] = self.State()
return state
def instance(self):
"""The instance we are a property for."""
return self._instance
@property
def computed(self):
"""The signal that is emitted when the value is set."""
return self.state().signal
def set(self, value):
"""Sets a value.
If the value is not None, the computed(value) signal is emitted.
"""
state = self.state()
state.value = value
state.running = False
if value is not None:
self.computed.emit(value)
self.computed.clear()
def unset(self):
"""Sets the value to None, the property is considered unset."""
self.state().value = None
def get(self):
"""Retrieves the value, which may be None (unset)."""
return self.state().value
def __call__(self):
"""Retrieves the value, starting the computation if needed.
If the function immediately returns a value it is returned;
otherwise None is returned.
"""
state = self.state()
if state.value is None:
self.start()
return state.value
def name(self):
"""Returns the name of the property, if given via the function."""
if self._property._func:
return self._property._func.__name__
def isset(self):
"""Returns True if the property is set."""
return self.state().value is not None
def iscomputing(self):
"""Returns True if the property is being computed."""
return self.state().running
def callback(self, func):
"""Calls the specified function back with the value.
If the value already is known, the callback is performed immediately
(synchronous) and this method returns True.
If the value yet has to be computed, the function is connected to the
computed() signal and start() is called, so the function is called later
with the value. In that case this method returns None.
"""
value = self.state().value
if value is not None:
func(value)
return True
self.computed.connect(func)
self.start()
def start(self):
"""Starts the machinery that computes the value.
This simply happens by calling run(), which should be reimplemented
to perform the actual action.
"""
state = self.state()
if not state.running and state.value is None:
state.running = True
self.checkstart()
def checkstart(self):
"""Starts if all dependencies are met."""
for d in self._property._depends:
prop = d.__get__(self.instance())
if prop.get() is None:
prop.computed.connect(self.checkstart)
prop.start()
break
else:
self.run()
def run(self):
"""Starts the computation.
The result must be set using self.set(value), which will automatically
call all registered callbacks once.
The default implementation starts the function, if given on init.
"""
if self._property._func:
result = self._property._func(self.instance())
if result is not None:
self.set(result)
else:
self.set("(null)")
cachedproperty = CachedProperty.cachedproperty
|