summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJason Kirtland <jek@discorporate.us>2007-09-07 00:30:53 +0000
committerJason Kirtland <jek@discorporate.us>2007-09-07 00:30:53 +0000
commit64b0267d280f8990f0551f7308e954427f1de625 (patch)
tree8dcbf02c308e033da7ac37787c41626978dc5735 /lib
parent9206d7ed5a9e93f38de8b46cdb19c0c237fdb943 (diff)
downloadsqlalchemy-64b0267d280f8990f0551f7308e954427f1de625.tar.gz
Added 'collection_iter', like 'iter', for anything that implements the @collection.iterator or __iter__ interface.
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/orm/collections.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/sqlalchemy/orm/collections.py b/lib/sqlalchemy/orm/collections.py
index 2a02eb8ff..e08a4b8b7 100644
--- a/lib/sqlalchemy/orm/collections.py
+++ b/lib/sqlalchemy/orm/collections.py
@@ -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.