Re: [SQLObject] Indexed access to a SelectResults
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: Matt G. <ma...@po...> - 2003-07-17 00:45:03
|
On Thu, 2003-07-17 at 01:05, Ian Bicking wrote:
> 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())).
Perhaps you're right.
The best solution is probably to change the code I'm using to iterate
over the results in the normal manner. This code in question could
actually take a slice first which would work really nicely with
SQLObject. Something for me to look into.
>
> 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).
Now that's not a bad idea, thanks.
>
> > 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.
Are you sure it's that simple? I was just playing with exactly this idea
too and wrote the following code to experiment:
class Test:
def __init__(self):
self.counter = 0
def __iter__(self):
return self
def next(self):
self.counter = self.counter + 1
if self.counter > 5:
raise StopIteration()
return self.counter
t = Test()
a = list(t)
print a # ok up to here
print len(t) # this fails
The fact that creating the list did not fail implies that it is not
trying to get the length of t. I dunno though, it's very late here so I
may be missing something obvious.
- Matt
--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: ma...@po...
|