From 7f906f5f9cf77bd24ac6294c964a3fdb85988033 Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 22 May 2010 16:38:24 +0200 Subject: Added ranger.ext.lazy_property --- ranger/ext/lazy_property.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ranger/ext/lazy_property.py diff --git a/ranger/ext/lazy_property.py b/ranger/ext/lazy_property.py new file mode 100644 index 00000000..6f894179 --- /dev/null +++ b/ranger/ext/lazy_property.py @@ -0,0 +1,25 @@ +# From http://blog.pythonisito.com/2008/08/lazy-descriptors.html + +class lazy_property(object): + """ + A @property-like decorator with lazy evaluation + + Example: + class Foo: + @lazy_property + def bar(self): + result = [...] + return result + """ + + def __init__(self, method): + self._method = method + self.__name__ = method.__name__ + self.__doc__ = method.__doc__ + + def __get__(self, obj, cls=None): + if obj is None: # to fix issues with pydoc + return None + result = self._method(obj) + obj.__dict__[self.__name__] = result + return result -- cgit 1.4.1-2-gfad0