[Tutor] class and methods/functions

Eric Walker ewalker at micron.com
Thu Oct 6 21:15:22 CEST 2005


On Thursday 06 October 2005 12:48 pm, Kent Johnson wrote
> > class TPROJ:
> >     import re
> >     import string
>
> Common usage is to put all the imports at the top of the module. There are
> circumstances where you will put them inside a function but not in this
> case.

I moved them all to the top before the class definition
>
> >     def display(self):#display method
> >         print self.BASENAME
> >         print self.PROJECT
> >         print self.REV
> >         print self.DESIGNATOR
> >         print self.TYPE
>
> It's your choice, but all caps for attribute values is unusual in Python.
>

Ok, I want to follow normal python protocol. I changed them to lower case.

> >     def __init__(self,value):#createMethod auto executes since it has __
> >         name = nameCheck(value)
>
> My guess is that nameCheck is the function that you moved out of the class.
> If you want it to be part of the class, you have to call it with
> self.nameCheck(value)
>
> >         if name:
> >             self.BASENAME = value
> >             self.PROJECT = value.split(':')[0]
> >             self.REV = value.split(':')[1]
> >             self.DESIGNATOR = "NOVALUE"
> >             self.TYPE = ('SW','TMOD','SWA','TMODA')#constant tuple
> >         if not name:
>
> could be else:
> >             print "found a bad name: %s" % value
> >
yes, I did remove this from the class. I really didn't need it in there. I 
moved the check outside under the main running function. To check the name 
before I even create the object.  The way I had it before it would create an 
object regardless if the name had a colon or not.

>   def nameCheck(self, value):
> >         import re
> >         tempREG = re.match('.*:.*',value)
> >         return str(tempREG) != 'None'
>
> Still a syntax error here!
>
Code is working as I thought it would. What syntax error do I have?

> Still recommend removing this try / except. Or you could wait until you
> actually get an exception and ask here for help ;-) cuz we will ask you for
> the traceback.
yes,
I did remove the try except. Man this group is great. I am going to be a 
python coder yet.....  current code sample follows:


import re
import string
import os

class TPROJ:    
    
    def display(self):#display method
        print self.basename
        print self.project
        print self.rev
        print self.designator
        print self.type
        
    def __init__(self,value):#createMethod auto executes since it has __
            self.basename = value
            self.project = value.split(':')[0] 
            self.rev = value.split(':')[1]
            self.designator = "NOVALUE"
            self.type = ('SW','TMOD','SWA','TMODA')#constant tuple
            
            
    
def nameCheck(value):
        tempREG = re.match('.*:.*',value)
        return str(tempREG) != 'None'
            
def getProjectNames():
    currentDir=os.getcwd()
    nameTable = {}
    temp=currentDir + '/TEMP'
    print temp
    os.chdir(temp)
    baseList=os.listdir(".")
    baseObjectList = []
    for name in baseList:
        if nameCheck(name):
            baseObjectList.append(TPROJ(name))

Python Newbie...



More information about the Tutor mailing list