From: Jason Kirtland Date: Fri, 7 Sep 2007 00:30:53 +0000 (+0000) Subject: Added 'collection_iter', like 'iter', for anything that implements the @collection... X-Git-Tag: rel_0_4beta6~38 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=64b0267d280f8990f0551f7308e954427f1de625;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Added 'collection_iter', like 'iter', for anything that implements the @collection.iterator or __iter__ interface. --- 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.