[CentralOH] Help with learning how to use a class (please don't laugh :) )

Eric Lake ericlake at gmail.com
Wed Jul 9 17:18:41 CEST 2008


Actually that did help and I saw another typo. I was getting 0.0 back
but that is because I was using free = GetTotalDiskSpace(self.drive)
when it should have been free = GetFreeDiskSpace(self.drive)

On Wed, Jul 9, 2008 at 11:14 AM, Eric Lake <ericlake at gmail.com> wrote:
> That seems to be the fix there. If I have something like the following:
>
>    def GetTotalDiskSpace(self, drive):
>        self.drive = drive
>        for disk in self.server.Win32_LogicalDisk():
>            if disk.Name == self.drive:
>                total = long(disk.Size) /1073741824.0
>        return total
>
>    def GetFreeDiskSpace(self, drive):
>        self.drive = drive
>        for disk in self.server.Win32_LogicalDisk():
>            if disk.Name == self.drive:
>                free = long(disk.FreeSpace) /1073741824.0
>        return free
>
>    def GetUsedDiskSpace(self, drive):
>        self.drive = drive
>        total = GetTotalDiskSpace(self.drive)
>        free = GetTotalDiskSpace(self.drive)
>        used = (total - free)
>        return used
>
> should I change the:
>
> total = GetTotalDiskSpace(self.drive)
>
> to
>
> total = self.GetTotalDiskSpace(self.drive)
>
> On Wed, Jul 9, 2008 at 11:07 AM, Steven Huwig <steven_h at acm.org> wrote:
>> How about putting initialization code in the __init__ method instead?
>>
>>   def __init__(self, hostname):
>>       self.host = hostname
>>       self.server = wmi.WMI(self.host)
>>
>> and replace all references to server with self.server .
>>
>>
>> On Wed, Jul 9, 2008 at 10:55 AM, Eric Lake <ericlake at gmail.com> wrote:
>>> The output above was from a run of the script with
>>> GetLocalDrives(self) set. I had to change it to the following to make
>>> it work:
>>>
>>> import sys
>>> import wmi
>>>
>>> class SysInfo(object):
>>>    def __init__(self, hostname):
>>>        self.host = hostname
>>>
>>>    def GetLocalDrives(self):
>>>        server = wmi.WMI(self.host)
>>>        driveList = []
>>>        for disk in server.Win32_LogicalDisk():
>>>            if disk.DriveType == 3:
>>>                driveList.append(str(disk.Name))
>>>        return driveList
>>>
>>> I would like to not have to do 'server = wmi.WMI(self.host)' in every
>>> method. Once I call mach = diskspace.SysInfo('localhost') I would like
>>> the wmi stuff to be set.
>>>
>>> On Wed, Jul 9, 2008 at 10:26 AM, Sam Corder <samus at codeargyle.com> wrote:
>>>> Change def GetLocalDrives()
>>>> to
>>>> def GetLocalDrives(self)
>>>> and it will work.  All methods attached to an instance of a class need to take self as the first parameter.
>>>>
>>>> ----- Original Message -----
>>>> From: Eric Lake <ericlake at gmail.com>
>>>> To: centraloh at python.org
>>>> Sent: Wednesday, July 9, 2008 10:23:19 AM GMT-0500 Auto-Detected
>>>> Subject: [CentralOH] Help with learning how to use a class (please don't laugh :) )
>>>>
>>>> I am trying to write a script that I can use to gather information
>>>> about the windows servers we have in our network. I started off with
>>>> it being just a procedural script and I am now trying to put all of
>>>> the functions in a class. This is still very much a work in progress.
>>>> What can I do to improve on this / make it work? Any help would be
>>>> appreciated. When I run the following from ipython to test it against
>>>> my local machine I get this output:
>>>>
>>>> In [64]: server = diskspace.SysInfo('localhost')
>>>>
>>>> In [65]: server.GetLocalDrives()
>>>> ---------------------------------------------------------------------------
>>>> TypeError                                 Traceback (most recent call last)
>>>>
>>>> C:\Documents and Settings\elake\Desktop\<ipython console> in <module>()
>>>>
>>>> TypeError: GetLocalDrives() takes no arguments (1 given)
>>>>
>>>>
>>>>
>>>> import sys
>>>> import wmi
>>>>
>>>> class SysInfo(object):
>>>>    def __init__(self, hostname):
>>>>        self.host = hostname
>>>>        server = wmi.WMI(self.host)
>>>>
>>>>    def GetLocalDrives():
>>>>        driveList = []
>>>>        for disk in self.Win32_LogicalDisk():
>>>>            if disk.DriveType == 3:
>>>>                driveList.append(str(disk.Name))
>>>>        return driveList
>>>>
>>>>    def GetTotalDiskSize(drive):
>>>>        for disk in server.Win32_LogicalDisk():
>>>>            if disk.Name == drive:
>>>>                total = long(disk.Size) /1073741824.0
>>>>        return total
>>>>
>>>>    def GetFreeDiskSpace(drive):
>>>>        for disk in server.Win32_LogicalDisk():
>>>>            if disk.Name == drive:
>>>>                free = long(disk.FreeSpace) /1073741824.0
>>>>        return free
>>>>
>>>>    def GetUsedDiskSpace(drive):
>>>>        total = GetTotalDiskSpace(drive)
>>>>        free = GetTotalDiskSpace(drive)
>>>>        used = (total - free)
>>>>        return used
>>>>
>>>>    def GetStorage(self):
>>>>        server = wmi.WMI(self.host)
>>>>        print "Gather information on %s\n" % self.host
>>>>        outfile.write("%s ::\n" % self.host)
>>>>        for disk in server.Win32_LogicalDisk():
>>>>            if disk.DriveType == 3:
>>>>                total = long(disk.Size) /1073741824.0
>>>>                free = long(disk.FreeSpace) /1073741824.0
>>>>                used = (total - free)
>>>>                per_used = (used * 100) / total
>>>>                outfile.write("\tDrive %s\n" % disk.Name)
>>>>                outfile.write("\t\tTotal Size: %3.2f GB\n" % total)
>>>>                outfile.write("\t\tFree Space: %3.2f GB\n" % free)
>>>>                outfile.write("\t\tUsed Space: %3.2f GB\n" % used)
>>>>                outfile.write('\t\tPercent Used: %.2f \n' % per_used)
>>>>
>>>>    def GetNodeName():
>>>>        for os in server.Win32_OperatingSystem():
>>>>            activeNode = server.CSName
>>>>            return activeNode
>>>>
>>>> if __name__ == '__main__':
>>>>
>>>>    infile = open('san.txt', 'r')
>>>>    outfile = open('test4.txt', 'w')
>>>>
>>>>    for host in infile.readlines():
>>>>        host = host.strip()
>>>>        try:
>>>>            server = SysInfo(host)
>>>>            server.GetStorage()
>>>>            outfile.write("\n")
>>>>        except KeyboardInterrupt:
>>>>            print "The user canceled the job.\n"
>>>>            sys.exit(0)
>>>>        except:
>>>>            outfile.write("%s does not appear to be available.\n\n" % host)
>>>>
>>>>    print "Done!\n"
>>>>
>>>>    infile.close()
>>>>    outfile.close()
>>>>
>>>> --
>>>> Thanks,
>>>>
>>>> Eric Lake
>>>> _______________________________________________
>>>> CentralOH mailing list
>>>> CentralOH at python.org
>>>> http://mail.python.org/mailman/listinfo/centraloh
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Thanks,
>>>
>>> Eric Lake
>>> _______________________________________________
>>> CentralOH mailing list
>>> CentralOH at python.org
>>> http://mail.python.org/mailman/listinfo/centraloh
>>>
>>
>
>
>
> --
> Thanks,
>
> Eric Lake
>



-- 
Thanks,

Eric Lake


More information about the CentralOH mailing list