degal/utils.py
changeset 96 d9cf1e272e90
parent 84 891545a38a2b
child 109 66a01c0806f1
--- a/degal/utils.py	Thu Jun 11 21:50:48 2009 +0300
+++ b/degal/utils.py	Thu Jun 11 22:50:21 2009 +0300
@@ -4,20 +4,29 @@
 
 import functools
 
-class LazyProperty (property) :
+class LazyProperty (object) :
     """
         Lazy-loaded cached properties.
 
         This functions by overriding the class's property attribute with the actual value attribute in the instance's
         dict, causing all future attribute lookups to return that directly.
+
+        >>> class Foo (object) :
+        ...     counter = 0
+        ...     @lazy_load
+        ...     def foo (self) :
+        ...             self.counter += 1
+        ...             return self.counter
+        ... 
+        >>> foo = Foo(); foo.foo, foo.foo, foo.counter
+        (1, 1, 1)
+
     """
 
     def __init__ (self, func) :
         """
             Initialize with no value
         """
-
-        super(LazyProperty, self).__init__(func)
         
         # the getter function
         self.func = func
@@ -38,14 +47,16 @@
         """
             Store the cached value, overring ourself
         """
-        
+
+#        print "[%x] %r.__dict__[%r] = %r" % (id(obj), obj, self.name, value)
+
         obj.__dict__[self.name] = value
 
     def __get__ (self, obj, owner) :
         """
             Generate the value, store it as the attribute and return it.
         """
-        
+
         # compute value
         value = self.run(obj)
 
@@ -54,7 +65,7 @@
         
         # return it
         return value
-
+    
 class LazyIteratorProperty (LazyProperty) :
     """
         A lazy-loaded property that automatically converts an iterator/genexp into a list.
@@ -65,8 +76,14 @@
             Wrap LazyProperty.run to return a list
         """
 
-        return list(super(LazyIteratorProperty, self).run(obj))
+        return list(self.func(obj))
 
 lazy_load = LazyProperty
 lazy_load_iter = LazyIteratorProperty
 
+# testing
+if __name__ == '__main__' :
+    import doctest
+
+    doctest.testmod()
+