[Checkins] SVN: megrok.rdb/trunk/ Allow conversion of query results.

Martijn Faassen faassen at infrae.com
Thu Sep 4 07:45:56 EDT 2008


Log message for revision 90801:
  Allow conversion of query results.
  

Changed:
  U   megrok.rdb/trunk/buildout.cfg
  U   megrok.rdb/trunk/src/megrok/rdb/README.txt
  U   megrok.rdb/trunk/src/megrok/rdb/components.py

-=-
Modified: megrok.rdb/trunk/buildout.cfg
===================================================================
--- megrok.rdb/trunk/buildout.cfg	2008-09-04 11:21:25 UTC (rev 90800)
+++ megrok.rdb/trunk/buildout.cfg	2008-09-04 11:45:55 UTC (rev 90801)
@@ -5,6 +5,9 @@
 extends = http://grok.zope.org/releaseinfo/grok-0.13.cfg
 versions = versions
 
+[versions]
+zc.recipe.testrunner = 1.0.0
+
 [test]
 recipe = zc.recipe.testrunner
 eggs = megrok.rdb

Modified: megrok.rdb/trunk/src/megrok/rdb/README.txt
===================================================================
--- megrok.rdb/trunk/src/megrok/rdb/README.txt	2008-09-04 11:21:25 UTC (rev 90800)
+++ megrok.rdb/trunk/src/megrok/rdb/README.txt	2008-09-04 11:45:55 UTC (rev 90801)
@@ -10,6 +10,12 @@
 
 .. _SQLAlchemy: http://www.sqlalchemy.org
 
+XXX a hack to make things work in doctests. In some particular setup
+this hack wasn't needed anymore, but I am unable at this time to
+reestablish this combination of packages::
+
+  >>> __file__ = 'foo'
+
 In this document we will show you how to use ``megrok.rdb``.
 
 ``megrok.rdb`` uses SQLAlchemy's ORM system, in particular its
@@ -371,3 +377,69 @@
   ...   result.append(key)
   >>> sorted(result)
   [u'1', u'2']
+
+Converting results of QueryContainer
+------------------------------------
+
+Sometimes it's useful to convert (or modify) the output of the query
+to something else before they appear in the container. You can implement
+the ``convert`` method to do so. It takes the individual value resulting
+from the value and should return the converted value::
+
+  >>> class ConvertingQueryContainer(rdb.QueryContainer):
+  ...   def query(self):
+  ...      return session.query(Department)
+  ...   def convert(self, value):
+  ...      return SpecialDepartment(value.id)
+
+  >>> class SpecialDepartment(object):
+  ...    def __init__(self, id):
+  ...      self.id = id
+
+  >>> qc = ConvertingQueryContainer()
+
+Let's now check that all values are ``SpecialDepartment``::
+
+  >>> isinstance(qc['1'], SpecialDepartment)
+  True
+  >>> isinstance(qc['2'], SpecialDepartment)
+  True
+
+KeyError still works::
+
+  >>> qc['3']
+  Traceback (most recent call last):
+    ...
+  KeyError: '3'
+
+``get``::
+
+  >>> isinstance(qc.get('1'), SpecialDepartment)
+  True
+  >>> qc.get('3') is None
+  True
+  >>> qc.get('3', 'foo')
+  'foo'
+
+``values``::
+
+  >>> [isinstance(v, SpecialDepartment) for v in qc.values()]
+  [True, True]
+
+The parents of all the values are the query container::
+
+  >>> [v.__parent__ is qc for v in qc.values()]
+  [True, True]
+  >>> sorted([v.__name__ for v in qc.values()])
+  [u'1', u'2']
+
+``items``::
+
+  >>> sorted([(key, isinstance(value, SpecialDepartment)) for (key, value) in qc.items()])
+  [(u'1', True), (u'2', True)]
+
+  >>> [value.__parent__ is qc for (key, value) in qc.items()]
+  [True, True]
+  >>> sorted([value.__name__ for (key, value) in qc.items()])
+  [u'1', u'2']
+  
\ No newline at end of file

Modified: megrok.rdb/trunk/src/megrok/rdb/components.py
===================================================================
--- megrok.rdb/trunk/src/megrok/rdb/components.py	2008-09-04 11:21:25 UTC (rev 90800)
+++ megrok.rdb/trunk/src/megrok/rdb/components.py	2008-09-04 11:45:55 UTC (rev 90801)
@@ -107,12 +107,20 @@
     implements(IContainer, ILocation)
 
     def query(self):
+        """Implement this returning the result of a query.
+        """
         raise NotImplementedError
 
+    def convert(self, value):
+        """Override this to convert output of query container.
+        """
+        return value
+
     def __getitem__(self, key):
         result = self.query().get(key)
         if result is None:
             raise KeyError(key)
+        result = self.convert(result)
         result.__parent__ = self
         result.__name__ = key
         return result
@@ -147,17 +155,19 @@
     def values(self):
         result = []
         for v in self.query().all():
-            v.__parent__ = self
-            v.__name__ = self.keyfunc(v)
-            result.append(v)
+            converted = self.convert(v)
+            converted.__parent__ = self
+            converted.__name__ = self.keyfunc(v)
+            result.append(converted)
         return result
         
     def items(self):
         result = []
         for v in self.query().all():
-            v.__parent__ = self
-            key = v.__name__ = self.keyfunc(v)
-            result.append((key, v))
+            converted = self.convert(v)
+            converted.__parent__ = self
+            key = converted.__name__ = self.keyfunc(v)
+            result.append((key, converted))
         return result
 
     def __len__(self):



More information about the Checkins mailing list