highscores list

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Dec 8 17:57:34 EST 2007


Bruno Desthuilliers a écrit :
(snip)
> # hiscores.py
> import sys
> 
> def _read_scores(path):
>     f = open(path)
>     # we don't expect a huge file so it's simpler to
>     # read it all in memory
>     lines = f.readlines()
>     f.close()
> 
>     scores = []
>     for line in filter(None, map(str.strip, lines)):
>         try:
>             score, name = line.split(':')
>             score = int(score)
>         except ValueError, e:
>             # either the lines was not score:name or
>             # score wasn't a proper value for an int
>             err = "File %s : incorrect file format" \
>                   % path
>             raise ValueError(err)
>         else:
>             scores.append((score, name))
> 
>     # supposed to be already sorted, but we want to be sure.
>     # NB : natural sort will do the RightThing(tm) here
>     scores.sort()

oops ! missing line here:

       score.reverse()

>     return scores



More information about the Python-list mailing list