Help please: How to assign an object name at runtime

c0chong at gmail.com c0chong at gmail.com
Wed Jun 29 20:55:44 EDT 2005


Good day:
Probably the answer to my question is staring me in the face, but the
solution escapes me.

The following is the input line of the file: SoftDict-.csv:
  ca1017,GRPHScriptSet,ADD/REM,Adobe Acrobat 4.0=2005/06/14

I expected an instance of Machine() to be created with a name ca1017.

Instead, an object is assigned to l[0] named:
   <__main__.Machine instance at 0x01282558>

-----------------------------
Here is my code:

class Machine:
    def __init__(self):
        self.software = []# Holds attributes of the instance
    def add(self, sware):
        self.software.append(sware)# Append attribute
        return self.software
    def show(self):
        return self.software
    def __call__(self):
        return [ e for e in self.software]
    def cnt(self):
        return '%s' % (len(self.software))
    def installed(self, sware):
        if sware in self.software:
            return True
        else:
            return False

class FileList:
    def __init__(self, filename):
        self.file = open(filename, 'r')     # open and save file
    def __getitem__(self, i):               # overload indexing
        line = self.file.readline()
        if line:
            return line                     # return the next line
        else:
            raise IndexError                # end 'for' loops, 'in'
    def __getattr__(self, name):
        return getattr(self.file, name)     # other attrs


if __name__ == '__main__':

    import sys, fileinput, os, string	# when run, not imported

    for line in FileList('SoftDict-.csv'): # Treat .csv as a text
        if len(line)==1: break	# Quit processing; end of file
        l=line.split(',')# Split the line into constituent parts
        l[0]=Machine()	# Create a Machine() object named: ca1017
-------------------------------------------

That's it.  I evidently have no idea what I am doing.

Thanks for your help.




More information about the Python-list mailing list