newbie: datastructure `dictionary' question

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Sep 9 14:51:29 EDT 2006


jason a écrit :

Just some more suggestions:

> def parselog(data):
>     other = 0
>     records = {}
> 
>     for line in string.split(data, '\n'):
       for line in data.split('\n'):
>         str = line.strip()
This will shadow the builtin 'str' type. You could reassign to 'line' 
instead, or manage to get stripped lines already:
       for line in map(str.strip, data.split('\n'):

>         if str[:4] == 'From':
>             mfrom, datum = extrfrom(str), extrdate(str)
>             print datum, mfrom

Mixing processing with IO may not be a good idea...

>         elif str[:4] == 'Fold':

           line_type = line[:4]
           if line_type == 'From':
              # code here
           elif line_type ==  'Fold':
>             folder = extrfolder(str[8:])
>             records = {folder : { 'date' : datum, 'mesgbytes' : extrmsize(str[8:]), 'mesgcount' : 1}}
You now know that it should be:
               records[folder] = {...}


>         else:
>             other += 1
> 
>     displrec(records)
> 

As a last note, you may want to pay more attention to your namings... 
ie, 'display_records()' is much more readable than 'displrec()' and 
still not to long to type !-)

My 2 cents (and a half)



More information about the Python-list mailing list