Re: [SQLObject] Indexed access to a SelectResults
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Ian B. <ia...@co...> - 2003-07-17 00:05:16
|
On Wed, 2003-07-16 at 17:42, Matt Goodall wrote: > > It would, though, create a bunch of SELECTs, like SELECT blah > > LIMIT 1 OFFSET 0, SELECT blah LIMIT 1 OFFSET 1, etc. > > > > The problem is the way you're using it. > > Unfortunately, it's not my code - it's another package that is walking > the list using an index. I can probably change that package but > couldn't/shouldn't SQLObject be a bit more graceful under these > circumstances? I don't know. If the code is going to treat select results as a list, a list seems like a reasonable thing to use (i.e., list(Whatever.select())). You could, generically, have a proxy class that batches the fetches, and caches the results temporarily (while it's waiting for your code to do another loop). There's no reason it has to have specific support for SQLObject either. Maybe such a thing already exists (probably does in *someone's* code... maybe on the ASPN recipe site). > To be honest, it's really the multiple COUNT calls that bother me as it > seems unnecessary. The multiple SELECT calls to retrieve the objects are > to be expected but can possibly be avoided. Yeah. That is weird. Ah... after some effort, I'm afraid I found why it happens. list() tries to get the length of the things it's converting. That's a pain in the but. Maybe I'll post to comp.lang.python about how to get around this. > > all = list(Entity.select()) > > for i in range(len(all)): > > print all[i] > > This is effectively how I am working around the "problem" for now, by > passing a list() rather than a SelectResults. > > > > > # or: > > all = Entity.select() > > for i, obj in enumerate(all): > > print obj > > I'm not sure I see the benefit of this. Doesn't SQLObject already use a > generator to walk the result of the query? Wouldn't this just wrap one > generator in another? That's how enumerate is supposed to work. It's for when the underlying code really wants to get an index along with the object. If that code you're using *doesn't* use that index, then that's just bad programming, and it should iterate directly. Ian |