[PEAK] QueryDM's with no query params

Phillip J. Eby pje at telecommunity.com
Fri May 20 18:10:03 EDT 2005

At 04:17 PM 5/20/2005 -0400, Erik Rose wrote:
>A common case in my app is ask for all the rows of a DB table—say, to get 
>a full list of values for some popup menu. Correct me if I'm wrong, but it 
>seems to make sense to use a QueryDM for this. The trouble is that 
>QueryDM's seem to assume you'll need to parametrize their queries: for 
>instance, QueryDM[someForeignKey]. In my case, I need no params: I simply 
>want all the rows returned. I would expect something like this to work:
>for each in myQueryDM:
>     print each
>However, it iterates forever,


>and I can't even figure out what it's finding to iterate over. (I can't 
>find any __iter__() calls or anything.)

Python will iterate over anything with a __getitem__.

>  Things break mysteriously when I try to use the debugger on them.


>Passing it a dummy param works...
>for each in myQueryDM[0]:
>     print each
>...but that's nasty. I'll kludge it for now, but what's the right way to 
>do this?

This would be simplest:

     def __iter__(self):
         for item in self[0]:
             yield item

And perhaps QueryDM should get a default __iter__ that raises 
NotImplementedError, and __iter__ can then also be overridden (by DM's that 
wish to do so) to iterate over all their contents.  This, in conjunction 
with the 'add' and 'remove' stubs, would move a few steps towards making 
today's DM's look like tomorrow's "workspaces".

More information about the PEAK mailing list