[Tutor] Inheritance help

sean_mcdaniel sean.m.mcdaniel at gmail.com
Wed Jul 2 17:40:03 CEST 2008


Hi folks,

I am having problems with classes and subclasses. Do the different __init__
statements have to have different argument lists? I expected my code to
print out "location 1" and "location 2" indicating that both __init__
statements had been successfully called, but it seems like only the
superclass __init__ is.

Any suggestions would be appreciated.

Thanks,

Sean

<code>
"""Contains various classes for parsing input files. Used for various
fitting routines.

Created: 1 July 2008
Last updated: 1 July 2008

"""
from __future__ import with_statement

class FileParse(object):
    """Base class for all parsing classes."""

    def __init__(self,filename):
        """Initialize variables common to all parsers. Read the file to a
list."""
        self.filecontent = []
        self.dictionary = {}
        self.parsefile(filename)
        print "Location 1"

    def __getitem__(self,key): 
        return self.dictionary[key]

    def __setitem__(self,key,value):
        self.dictionary[key]=value
    
    def parsefile(self,filename):
        """Parse a file. Ignore blank lines and comments. Save the results
to a list."""
        with open(filename,'r') as f:
            for l in f:
                if not l.startswith('#') and len(l)!=1: 
                    self.filecontent.append(l)

    def populate_dictionary(self, validkeys):
        """Convert the list of lines in the file to appropriate key,value
pairs and 
        populate the dictionary."""
        for inputline in self.parselist:
            print inputline
            key = inputline.split()[0]
            print values
            if key in self.validkeys:
                valuetype = validkeys[key]
                if valuetype=="string":
                    self.dictionary[key]="".join(values)
                if valuetype=="int":
                    self.dictionary[key]=int(values[0])
                if valuetype=="float":
                    self.dictionary[key]=float(values[0])
                if valuetype=="listfloat":
                    self.dictionary[key]=map(float,values)
                if valuetype=="listint":
                    self.dictionary[key]=map(int,values)
            else:
                badkeys.append(key)
        if len(badkeys)!=0:
            print "Could not parse keys: ", " ".join(badkeys)

class SinglePeakFit(FileParse):
    """Parses an input file used for fitting a single peak."""

    def __init___(self, filename):
        super(FileParser,self).__init__(filename)
        self.validkeys = \
                {"title":"string",
                "xtitle":"string",
                "ytitle":"string",
                "ztitle":"string",
                "npars":"int",
                "peaktype":"string",
                "peakpars":"listfloat",
                "backgroundtype":"string",
                "backgroundpars":"listfloat",
                "fitrange":"listfloat"}
        self.populate_dictionary(self.validkeys)
        print "Location 2."
</code>
-- 
View this message in context: http://www.nabble.com/Inheritance-help-tp18240495p18240495.html
Sent from the Python - tutor mailing list archive at Nabble.com.



More information about the Tutor mailing list