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

2008-06-05 Thread 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?

Regards
Marco

-- 
Marco Bizzarri
http://iliveinpisa.blogspot.com/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


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

2008-06-05 Thread Janko Hauser


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


PGP.sig
Description: This is a digitally signed message part
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


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

2008-06-05 Thread Marco Bizzarri
On Thu, Jun 5, 2008 at 1:28 PM, Janko Hauser [EMAIL PROTECTED] wrote:

 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

 ___
 Zope maillist  -  Zope@zope.org
 http://mail.zope.org/mailman/listinfo/zope
 **   No cross posts or HTML encoding!  **
 (Related lists -
  http://mail.zope.org/mailman/listinfo/zope-announce
  http://mail.zope.org/mailman/listinfo/zope-dev )



Hi, Janko.

Thanks for your answer, but there is something I do not understand:

if results:

an empty result from ZCatlog is false in a boolen condition?


Regards
Marco

-- 
Marco Bizzarri
http://iliveinpisa.blogspot.com/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


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

2008-06-05 Thread Tino Wildenhain

Hi,

Marco Bizzarri wrote:
...

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

return result_len


...


Thanks for your answer, but there is something I do not understand:

if results:

an empty result from ZCatlog is false in a boolen condition?


Yes, thats standard Python behavior, empty lists, dictionaries
and similar objects are assumed False when used as boolean.

This means you could even write it this way:

return (results and results.actual_result_count) or 0

in python2.5 (not yet supported by Zope) even:

return results.actual_result_count if results else 0

Cheers
Tino



smime.p7s
Description: S/MIME Cryptographic Signature
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


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

2008-06-05 Thread Marco Bizzarri
On Thu, Jun 5, 2008 at 3:35 PM, Tino Wildenhain [EMAIL PROTECTED] wrote:
 Hi,

 Marco Bizzarri wrote:
 ...

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

 return result_len

 ...

 Thanks for your answer, but there is something I do not understand:

 if results:

 an empty result from ZCatlog is false in a boolen condition?

 Yes, thats standard Python behavior, empty lists, dictionaries
 and similar objects are assumed False when used as boolean.

 This means you could even write it this way:

 return (results and results.actual_result_count) or 0

 in python2.5 (not yet supported by Zope) even:

 return results.actual_result_count if results else 0

 Cheers
 Tino




Tino, Janko, thanks for the clarification. I understand the standard
behaviour of list and tuples. But what a Catalog returns is not (if I
understand it correctly) a list or a tuple, even though it presents as
such. It is a subclass of Lazy. Is Lazy behaving like a list or a
tuple, in this respect?

Regards
Marco




-- 
Marco Bizzarri
http://iliveinpisa.blogspot.com/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )