[Tutor] Inheritance help

sean_mcdaniel sean.m.mcdaniel at gmail.com
Wed Jul 2 18:03:50 CEST 2008


Hi Folks,

I can redefine the class and I get a "TypeError: __init__() takes exactly 1
argument (2 given)" error.  I'm creating a SinglePeakFit object and not a
FileParse one. Still puzzled...

Thanks,

Sean

New definition: 
"""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):
        """Initialize variables common to all parsers. Read the file to a
list."""
        self.filecontent = []
        self.dictionary = {}
        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):
        print "I am here!"
        super(FileParse,self).__init__()
        self.validkeys = \
                {"title":"string",
                "xtitle":"string",
                "ytitle":"string",
                "ztitle":"string",
                "npars":"int",
                "peaktype":"string",
                "peakpars":"listfloat",
                "backgroundtype":"string",
                "backgroundpars":"listfloat",
                "fitrange":"listfloat"}
        self.parsefile(filename)
        self.populate_dictionary(self.validkeys)
        print "Location 2."


-- 
View this message in context: http://www.nabble.com/Inheritance-help-tp18240495p18241051.html
Sent from the Python - tutor mailing list archive at Nabble.com.



More information about the Tutor mailing list