From 64b0267d280f8990f0551f7308e954427f1de625 Mon Sep 17 00:00:00 2001 From: Jason Kirtland Date: Fri, 7 Sep 2007 00:30:53 +0000 Subject: [PATCH] Added 'collection_iter', like 'iter', for anything that implements the @collection.iterator or __iter__ interface. --- lib/sqlalchemy/orm/collections.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/sqlalchemy/orm/collections.py b/lib/sqlalchemy/orm/collections.py index 2a02eb8ff2..e08a4b8b74 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. -- 2.47.3