[CentralOH] Help with learning how to use a class (please don't laugh :) )
Brian Costlow
brian.costlow at gmail.com
Wed Jul 9 18:06:29 CEST 2008
Eric,
A couple of other things I noticed.
You use one of these two constructs in all the methods.
for disk in server.Win32_LogicalDisk():
for disk in server.Win32_LogicalDisk():
if disk.DriveType == 3:
Since it doesn't seem these would change for this script between the object
creation and calling the methods, I'd refactor them into the init as well.
class SysInfo(object):
def __init__(self, hostname):
self.host = hostname
server = wmi.WMI(self.host)
self.disks = [disk for disk in server.Win32_LogicalDisk()]
self.localDisks [ disk for disk in self.disks if disk.DriveType ==
3]
Then you can iterate the lists in your methods.
def GetStorage(self):
for disk in self.localDisks:
Also, it won't matter much in a one-pager, but if you were doing anything
bigger, this might bite you. It looks like you're creating a global variable
for the outfile, then referring to that implicitly from inside an object
method. Typically, Id try to open/close that resource inside the method that
uses it. If it had to come from outside, Id pass it explicitly just so the
code was clearer.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20080709/dd0495b6/attachment.htm>
More information about the CentralOH
mailing list