Sum up a property from several objects
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 ? (I am pretty sure there is - it's just me missing something basic here) Regards, Gitte Wange
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
On Sunday 08 July 2001 18:45, ender wrote:
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
Thanx !! Now I have the nicests stats of my computers at my website :-))) I have been wondering if it is possible in python to make system calls (I did it with PHP). But it was only for the machine that the script ran on. It found the machines uptime and printed it out. Could I - just philosophy - in python, write a script that calls different machines (perhaps via SNMP) and gets their uptime, hard ware numbers (such as RAM, CPU, Harddisk etc) ??? Regards, -- Gitte Wange Jensen Sys Admin, Developer and a lot more MMmanager.org Aps, Denmark Phone: +45 29 72 79 72 Email: gitte@mmmanager.org Web: www.mmmanager.org Quote of the day: It's entirely untested, but it looks good and compiles. Ship it! - Linus Torvalds
On Monday 09 July 2001 07:58, Gitte Wange wrote:
Now I have the nicests stats of my computers at my website :-)))
I have been wondering if it is possible in python to make system calls (I did it with PHP). But it was only for the machine that the script ran on.
yes, although personally i'm not a big fan of forking processes from zope, if you want examples in existing products, ZCVSMixin does this to wrap the commandline cvs and probably PHPObject. check out the popen(1-2-3) commands in the python lib reference on python.org
It found the machines uptime and printed it out.
Could I - just philosophy - in python, write a script that calls different machines (perhaps via SNMP) and gets their uptime, hard ware numbers (such as RAM, CPU, Harddisk etc) ???
yes, this is probably more elegant. there are three python snmp libraries, of which two are being developed, i think their all hosted on sourceforge, none of them support v3 afaik. if you're going to poll you should make your connections volatile attributes (start names with _v_) or you'll run afoul of persistence semantics. btw. there was a paper on using the www.sf.net/projects/pysnmp project at python9.org cheers kapil cheers kapil
At 06:04 09-07-2001 -0700, ender wrote:
On Monday 09 July 2001 07:58, Gitte Wange wrote:
Now I have the nicests stats of my computers at my website :-)))
I have been wondering if it is possible in python to make system calls (I did it with PHP). But it was only for the machine that the script ran on.
yes, although personally i'm not a big fan of forking processes from zope, if you want examples in existing products, ZCVSMixin does this to wrap the commandline cvs and probably PHPObject.
check out the popen(1-2-3) commands in the python lib reference on python.org
It found the machines uptime and printed it out.
Could I - just philosophy - in python, write a script that calls different machines (perhaps via SNMP) and gets their uptime, hard ware numbers (such as RAM, CPU, Harddisk etc) ???
yes, this is probably more elegant. there are three python snmp libraries, of which two are being developed, i think their all hosted on sourceforge, none of them support v3 afaik. if you're going to poll you should make your connections volatile attributes (start names with _v_) or you'll run afoul of persistence semantics.
btw. there was a paper on using the www.sf.net/projects/pysnmp project at python9.org
Thank again :-) I'm looking at the options right now. Not a Zope problem so I guess I'll look in the python community for answers :-) But MAYBE someday I'll be back with a Zope Product that quieries the SNMP and displays all sorts of things ... hmm .. perhaps using mrtg ? Well I dreaming right now - too busy developing other Zope stuff. But maybe when I have a holiday :-)) Good night - Gitte
participants (2)
-
ender -
Gitte Wange