[Grok-dev] hurry.query, batching the results
Sebastian Ware
sebastian at urbantalk.se
Tue Jul 3 13:03:43 EDT 2007
Just in case someone actually looks at the code... this should work a
bit better...
def searchResultsBatched(self, query, start_at=1, batch_size=None):
if start_at < 1: start_at = 0
else: start_at -= 1
results = query.apply()
if results is not None:
total = len(results)
if batch_size is None:
if start_at > 0: prev_batch = start_at
else: prev_batch = 0
next_batch = 0
end_at = total - 1
else:
if start_at + 1 < batch_size: prev_batch = start_at
else: prev_batch = batch_size
if total <= start_at + batch_size:
next_batch = 0
batch_size = total - start_at
elif total < start_at + 1 + 2 * batch_size:
next_batch = total - batch_size - start_at
else: next_batch = batch_size
end_at = start_at + batch_size - 1
outp = {'start_at': start_at + 1, 'end_at': end_at + 1,
'batch_size': batch_size, 'total': total,
'prev_batch': prev_batch, 'next_batch': next_batch}
uidutil = zapi.getUtility(IIntIds)
results = ResultSet(results, uidutil)
res = []
i = 0
for item in results:
if i >= start_at and i <= end_at:
res.append(item)
elif i > end_at: break
i += 1
outp['result'] = res
return outp
Mvh Sebastian
3 jul 2007 kl. 16.00 skrev Sebastian Ware:
> For what it's worth, I eventually added the following code to
> hurry.query.query. Maybe someone can do it a lot better :)
>
> def searchResultsBatched(self, query, start_at=1,
> batch_size=None):
> start_at -= 1
> results = query.apply()
> if results is not None:
> total = len(results)
> if batch_size is None:
> if start_at > 0: prev_batch = start_at
> else: prev_batch = 0
> next_batch = 0
> end_at = total - 1
> else:
> if start_at < batch_size: prev_batch = start_at
> else: prev_batch = batch_size
> if total - start_at < batch_size: next_batch =
> total - start_at
> else: next_batch = batch_size
> end_at = start_at + next_batch - 1
> outp = {'start_at': start_at + 1, 'end_at': end_at + 1,
> 'batch_size': batch_size, 'total': total,
> 'prev_batch': prev_batch, 'next_batch':
> next_batch}
> uidutil = zapi.getUtility(IIntIds)
> results = ResultSet(results, uidutil)
> res = []
> i = 0
> for item in results:
> if i >= start_at and i <= end_at:
> res.append(item)
> elif i > end_at: break
> i += 1
> outp['result'] = res
> return outp
>
>
>
>
> Mvh Sebastian
>
>
> 3 jul 2007 kl. 14.28 skrev Sebastian Ware:
>
>> It seems as though "result" is of type BTrees._IFBTree.IFSet,
>> maybe this doesn't support slicing etc. I did a work around but I
>> am worried that it instantiates a lot of objects. I'll be happy to
>> post it when I feel that it works.
>>
>> Mvh Sebastian
>>
>> 3 jul 2007 kl. 13.17 skrev Sebastian Ware:
>>
>>> Hi
>>>
>>> I am trying to batch the results of hurry.query but I get an
>>> error...
>>>
>>> TypeError: sequence index must be integer
>>>
>>> ...I am modifying the serachResult() method in effect doing this:
>>>
>>> def searchResults(self, query):
>>> results = query.apply()
>>> if results is not None:
>>> uidutil = zapi.getUtility(IIntIds)
>>> results = ResultSet(results[0:1], uidutil)
>>> return results
>>>
>>> ...but it chokes on the results[0:1] slicing. From what I
>>> understand of the rest of the code, it is only a list of
>>> integers, so I don't understand why I can't slice it.
>>>
>>> Any help would be much appreciated!
>>>
>>> Mvh Sebastian
>>>
>>>
>>> _______________________________________________
>>> Grok-dev mailing list
>>> Grok-dev at zope.org
>>> http://mail.zope.org/mailman/listinfo/grok-dev
>>
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
More information about the Grok-dev
mailing list