aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhut <hut@hut.pm>2017-07-16 05:36:13 +0200
committerhut <hut@hut.pm>2017-07-16 05:36:13 +0200
commitb40746d90c52684d9530b59bdfea44bb6f203214 (patch)
tree8e87b6a058751c29bfd0ee5ce8680d584c1152f9
parent62f0448e2821d9bc7dc1de5c2f32d2d87cc95ee3 (diff)
downloadranger-b40746d90c52684d9530b59bdfea44bb6f203214.tar.gz
ext.lazy_property: allow resetting lazy properties
-rw-r--r--ranger/ext/lazy_property.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/ranger/ext/lazy_property.py b/ranger/ext/lazy_property.py
index 92dc309d..c468a8c9 100644
--- a/ranger/ext/lazy_property.py
+++ b/ranger/ext/lazy_property.py
@@ -1,4 +1,6 @@
-# From http://blog.pythonisito.com/2008/08/lazy-descriptors.html
+# This file is part of ranger, the console file manager.
+# License: GNU GPL version 3, see the file "AUTHORS" for details.
+# Based on http://blog.pythonisito.com/2008/08/lazy-descriptors.html
from __future__ import (absolute_import, division, print_function)
@@ -17,6 +19,22 @@ class lazy_property(object): # pylint: disable=invalid-name,too-few-public-meth
42
>>> foo.answer
42
+ >>> foo.answer__reset()
+ >>> foo.answer
+ calculating answer...
+ 42
+ >>> foo.answer
+ 42
+
+ Avoid interaction between multiple objects:
+
+ >>> bar = Foo()
+ >>> bar.answer
+ calculating answer...
+ 42
+ >>> foo.answer__reset()
+ >>> bar.answer
+ 42
"""
def __init__(self, method):
@@ -27,8 +45,14 @@ class lazy_property(object): # pylint: disable=invalid-name,too-few-public-meth
def __get__(self, obj, cls=None):
if obj is None: # to fix issues with pydoc
return None
+
+ def reset_function():
+ setattr(obj, self.__name__, self)
+ del obj.__dict__[self.__name__] # force "__get__" being called
+
result = self._method(obj)
obj.__dict__[self.__name__] = result
+ obj.__dict__[self.__name__ + "__reset"] = reset_function
return result