[Tutor] [LONG] help on self

Arne Mueller a.mueller@icrf.icnet.uk
Tue, 13 Apr 1999 17:19:11 +0100


Hi Again,

one of my biggest problems in object oriented python is 'self' ...

As far as I know it's a referecne to the calling object.

Here's a program I'm writing. I cannot provide the complete example,
because the datafile which is necessaty to run the program is rather
VERY big :-(

#!/eureka/bmm/mueller/bin/python

from re import *
import sys

###
### Class definitions
###

###############################################################################

### token or 'keyword' I'm looking for in text files
### a token is associated with it's match object and
### a function that can be executed if a match has
### been detected
class Token:
    
    def __init__(self, token, action):
        self.token = compile(token)
        self.action = action # reference to a function
        self.m = None # regex match object
        self.string = None # the string where 'token' matches

    def match(self, string):
        self.string = string
        self.m = self.token.search(string)
        if self.m:
            return self
        else:
            return None

###############################################################################

### sort of pseudo parser that's used to look for keywords in
### text files
class Parser:

    def __init__(self, f):
        self.f = f
        self.cont = compile('\S+')

    def parserError(self, token=None):
        if token:
            sys.stderr.write('Parser Error at: %s', token.pattern)
        else:
            sys.stderr.write('unknown parser Error!')
        sys.exit(1)

    ### retrun next non empty line
    def nextContensLine(self):
        while( 1 ):
            l = self.f.getLine()
            if not l or self.cont.search(l):
                return l

    ### looking for the current token - note, 'tokens'
    ### has to be defined in a subclass!
    def searchToken(self):
        l = self.nextContensLine()
        while( l ):
            for i in self.tokens:
                if i.match(l):
                    return i
            l = self.nextContensLine()
        self.parserrError()

###############################################################################

### parsing an 'Iteration' (an iteration is a text block)
### datastructure that saves results is not implemented yet
class Iteration(Parser):

    def __init__(self, f):
        self.f = f
        self.tokens = []
        Parser.__init__(self, f)

    def parseIteration(self):
        print 'parsing iterations ...'
        self.tokens = [ Token('^Searching\.*done$', self.parseHeader) ]
        t = self.searchToken()
	# here's a problem:
        # 't' is a 'Token' object so I assume 't'
	# provides everything of the 'Token' class
        t.action()
        
    def parseHeader(self):
        print 'parsing header ...'
	# now we'll get an error, because self doesn't know anything
	# about the attribute 'string' - but why? This function was called
	# by an 'Token' object/instance!
	print self.string
        self.tokens = [ Token('^Results from round (\d+)$',
self.parseOld),
                        Token('^Sequences producing', self.parseOld) ]
        
    def parseOld(self):
        print 'parsing old hits ...'

    def parseNew(self):
        print 'parsing new hits ...'


###
### Testing ...
###

f = IO('/v8/tmp/MG001.blast')
i = Iteration(f)
i.parseIteration()

-------


running the above programm results in

parsing iterations ...
parsing header ...
Traceback (innermost last):
  File "./Blast.py", line 130, in ?
    i.parseIteration()
  File "./Blast.py", line 109, in parseIteration
    t.action()
  File "./Blast.py", line 113, in parseHeader
    print self.string
AttributeError: string


Can anybody help with that strange behaviour? I think it's a 'self'
problem, so maybe you can teach me how to understand (my)'self'?

	Thanks very much,

	Arne

ps: if the above code is too confusing I may be able to generate a more
simple example that demonstrate  the 'self' problem ...