[Tutor] Help with lists

VanL van@lindbergs.org
Tue, 13 Mar 2001 00:18:07 -0700


Hello,

I'm stuck on what is probably a simple problem, but I can't figure
out how to solve it.

I am reading through some log files, and testing for some error
conditions.  The log files are formatted this way:
(begin)

Here is some header text, explaining the warning. This is several
lines long.  Each header has something that is unique.

Here is an error message.
Here is error message number 2.
Here is error message number 3.
Here is error message n.

Here is the beginning of the next header....
(end)

Notice the blank lines before and after the error block.

Now I am opening these files up and feeding them to this function:

def filter(logfile):
        thisfile = logfile.readlines()

        # Test for trigger strings -- these indicate the presence of
a specific
        # warning block.  The number after the string is the number
of lines
        # from the trigger line to the first blank line (before the
block of error
        # messages)

        test1 = 'Here is some header text, explaining the warning',
2
        test2 = 'some other trigger text in header two', 3

        for line in thisfile:

                # Get the results....
                result1 = string.find(line, test1[0])
                result2 = string.find(line, test2[0])

                # For each positive result, parse the error block to
see what
                # really went wrong.

                if (result1 != -1):
                        blockindex1 = (thisfile.index(line) +
test1[1])
                        if (find_bad_errors(logfile, blockindex1)):
                            return (0)

                if (result2 != -1):
                        blockindex2 = (thisfile.index(line) +
test2[1])
                        if (find_bad_errors(logfile, blockindex1)):
                            return (0)
        else:
                return (1)

Now, find_bad_errors goes like this:

def find_bad_errors(logfile, start):

        print "In function find_bad_errors"

        # start denotes the index of the blank line at the top of
the error block.
        # I want to search through each line, beginning at the index
of the top of the
        # error block.  If I find another blank line without
encountering a really bad
        # error, I'm done.

        block = logfile.readlines()
        blockstart = start
        for line in (block[(blockstart + 1):]):
                print "In for loop"
                if ((line == thisblock[startblock]) or
(re.match('^\s+$', line))):
                        print "All done, no errors."
                        return (0)

                if re.search('(notbad1|notbad2|notbad3|notbad4)',
line):
                        print line , 'Matched an error, but nothing
to worry about'
                        continue

                else:
                        print "I encountered a really bad error.
Fail"
                        return (1)


Now, here are my problems:

1. I never seem to be going into the for loop. The top print
statement ("In function find_bad_errors") always prints but the
second ("In for loop") never does.

2. If I rewrite the code so that it is forced to go through the
tests, I always fail because each line is getting split up into
1-character strings.  For example, you see that I test for
"notbad1".  I never find it, though, because all I see when I print
is
"
n
o
t
b
a
d
1

"

Could anybody help me?

Thanks,

Van