[Zope] Sum up a property from several objects

ender kthangavelu@earthlink.net
Sun, 8 Jul 2001 09:45:22 -0700


On Sunday 08 July 2001 14:13, Gitte Wange wrote:
>>Hello,
>>
>>I have a folder contaning some objects.
>>These objects all have a property named CPU (it's an int).
>>
>>Now I want to iterate through the folder's objects and sum up the value of
>>this CPU property.
>>
>>I have tried with this - IMHO - really bad code:
>>
>><dtml-with my_computers>
>> <dtml-in "objectValues()">
>>  <dtml-if sequence-start>
>>   <dtml-call "REQUEST.set('new_cpu',CPU)">
>>  <dtml-else>
>>   <dtml-let cpu="new_cpu + CPU">
>>    <dtml-call "REQUEST.set('new_cpu',cpu)">
>>   </dtml-let>
>>  </dtml-if>
>> </dtml-in>
>></dtml-with>
>>
>>But I get this error:
>>Error Type: KeyError
>>Error Value: cpu
>>
>>Any other way to do this ?

this sort of thing is ripe for a python script

mycomps = container.my_computers
sum = 0
for obj in mycomps.objectValues():
     sum = obj.cpu + sum
return sum

it can be done via dtml as well but its just ugly... IMO

kapil