[Checkins] SVN: megrok.rdb/trunk/src/megrok/rdb/ Add locatedproperty decorator. This may not be the perfect way to

Martijn Faassen faassen at infrae.com
Tue Oct 21 06:40:39 EDT 2008


Log message for revision 92418:
  Add locatedproperty decorator. This may not be the perfect way to
  spell what's needed to expose QueryContainer objects but it does work
  well enough.
  

Changed:
  U   megrok.rdb/trunk/src/megrok/rdb/__init__.py
  A   megrok.rdb/trunk/src/megrok/rdb/prop.py

-=-
Modified: megrok.rdb/trunk/src/megrok/rdb/__init__.py
===================================================================
--- megrok.rdb/trunk/src/megrok/rdb/__init__.py	2008-10-21 10:32:47 UTC (rev 92417)
+++ megrok.rdb/trunk/src/megrok/rdb/__init__.py	2008-10-21 10:40:38 UTC (rev 92418)
@@ -3,6 +3,7 @@
 from megrok.rdb.directive import key, metadata, tablename, reflected
 from megrok.rdb.setup import setupDatabase
 from megrok.rdb.interfaces import IDatabaseSetupEvent
+from megrok.rdb.prop import locatedproperty
 
 from sqlalchemy import MetaData
 

Added: megrok.rdb/trunk/src/megrok/rdb/prop.py
===================================================================
--- megrok.rdb/trunk/src/megrok/rdb/prop.py	                        (rev 0)
+++ megrok.rdb/trunk/src/megrok/rdb/prop.py	2008-10-21 10:40:38 UTC (rev 92418)
@@ -0,0 +1,26 @@
+class locatedproperty(property):
+    """A property that locates whatever is returned.
+
+    This is useful to create properties that fulfill the ILocation protocol
+    and therefore have a __parent__ and a __name__. Use it like this:
+
+    @locatedproperty
+    def whatever(self):
+        return Something()
+
+    now obj.whatever will have a __parent__ set to obj and a __name__
+    set to 'whatever'.
+
+    It's quite possible we'll find a nicer spelling for this
+    eventually for the purposes of megrok.rdb, but this works for now.
+    """
+    
+    def __init__(self, f):
+        super(locatedproperty, self).__init__(f)
+        self.__name__ = f.__name__
+
+    def __get__(self, obj, type=None):
+        value = super(locatedproperty, self).__get__(obj, type)
+        value.__parent__ = obj
+        value.__name__ = self.__name__
+        return value



More information about the Checkins mailing list