]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Added 'collection_iter', like 'iter', for anything that implements the @collection...
authorJason Kirtland <jek@discorporate.us>
Fri, 7 Sep 2007 00:30:53 +0000 (00:30 +0000)
committerJason Kirtland <jek@discorporate.us>
Fri, 7 Sep 2007 00:30:53 +0000 (00:30 +0000)
lib/sqlalchemy/orm/collections.py

index 2a02eb8ff20f7e0c02ad4ac6b156ad8725135866..e08a4b8b74a739ae7a4fdcecbaab5b1618e9a1c7 100644 (file)
@@ -402,6 +402,21 @@ def collection_adapter(collection):
 
     return getattr(collection, '_sa_adapter', None)
 
+def collection_iter(collection):
+    """Iterate over an object supporting the @iterator or __iter__ protocols.
+
+    If the collection is an ORM collection, it need not be attached to an
+    object to be iterable.
+    """
+
+    try:
+        return getattr(collection, '_sa_iterator',
+                       getattr(collection, '__iter__'))()
+    except AttributeError:
+        raise TypeError("'%s' object is not iterable" %
+                        type(collection).__name__)
+
+    
 class CollectionAdapter(object):
     """Bridges between the ORM and arbitrary Python collections.