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

Eric Lake ericlake at gmail.com
Wed Jul 9 16:55:32 CEST 2008


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


More information about the CentralOH mailing list