[Zope] Counting the elements of a result in a ZCatalog

Janko Hauser jh at zscout.de
Thu Jun 5 07:28:43 EDT 2008


Am 05.06.2008 um 12:56 schrieb Marco Bizzarri:

> Hi all.
>
> I need to query a ZCatalog, and I would like to know how many elements
> are there. I'm working from inside a python product, so, I could do
> something like:
>
> results = Catalog(criteria)
> return len(results)
>
>
> But this creates a number of objects which are completly useless for
> me. After all, I'm interested in just the length, not in the objects.
>
> I could do something like this:
>
> results = Catalog(criteria, sort_limit=1)
> return results.actual_result_count
>
> since I should have a LazyMap, which does not (should not) actually
> load all the objects (at least, I hope so).
>
> This does not work if I have no result. So, what I should do would be:
>
> results = Catalog(criteria, sort_limit=1)
> if len(results) == 0:
>    return 0
> return results.actual_result_count
>
> Am I missing something?

Yes, you do the expensive operation just for the test, so there is no  
benefit.

if result:
    result_len = results.actual_result_count
else:
    result_len = 0

return result_len

HTH,

__Janko
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 163 bytes
Desc: This is a digitally signed message part
Url : http://mail.zope.org/pipermail/zope/attachments/20080605/2ba77405/PGP.bin


More information about the Zope mailing list