[Tutor] please help

alan.gauld@bt.com alan.gauld@bt.com
Fri, 30 Aug 2002 13:13:19 +0100


> This is what I have 'not great but on my way"

> import string
> def getStuff(file):
>    for line in pppfile:

Where is pppfile defined? If its really a file you 
can't just do "for line in" you need to read it into 
a sequence first - usually a list.

     pppfile = file.readlines()
     for line in pppfile:
        etc...

However ppp file is now a bad name since its not really 
a file its a list so ppplist mifght be better....
   
>         closeline = 0

Now you set the same variables to zero for each line. 
You only want to initialise themmonce - outside the loop.

>         error.count = 0

Where are error, second etc defined?

>     if find("IPCP", line) > 0
>        connectline = split(line)

You need a colon at the end of the if...
You only imported the name 'string' so you need to use 
string.find(), string.split() etc

>         continue

You don't need the continue because the if statement 
isn't part of the loop(check the indentation level).
If it was you still don't need continue because the 
loop will automatically continue at the end 
- thats what loops do!

>     if find("ERROR", line) > 0
>        error.count = error.count + 1
>       continue

I assume all of these if statements should be inside 
the loop? They aren't at the moment...

Rather than using if/continue,if/continue you would be 
better to use the if/elif structure:

    if string.find(xxxx) > 0:
       # do something
    elif string.find(yyyy) > 0:
       # do another
    elif string.find(zzzz) > 0:
       # again
    else: # handle unexpected lines here....

>        print connectline[1] "" connectline[2]

You need commas to separate the items to be printed
         print connectline[1], "", connectline[2]

>        print close

Not sure what this is supposed to do?
close is a builtin function...?

>        print summary

Similarly where do you define summary?

Try starting small and just open the file, then go through
it and print out the lines containing the interesting bits.
(<Maybe even just testing for one interesting bit first, 
then add another etc.

Once you get that working you can add the counters etc.

Then in another program write some code to format data 
the way you want it. Finally stitch the two programs 
together to get what you want. Use the python interpreter 
prompt to experiment - get it working in the prompt 
first then put it in a file.

Python encourages experimenting and building piece 
by piece - often called "bottom up" design/programming.

HTH

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld