ESR's fortune.pl redone in python - request for critique

Peter Otten __peter__ at web.de
Tue Mar 30 14:43:05 EST 2004


Mel Wilson wrote:

> Or I could see (untested code)
> 
> 
> import random, sys
> 
> def fortunes (infile):
>     return infile.read().split ('\n%\n')
> 
> def findfortune (filename):
>     return random.choice (fortunes (file (filename, 'rt'))
> 
> if __name__ == '__main__':
>     print findfortune (sys.argv[1])

That's what I meant with "in Python I would suggest to relax and process the
whole file as a string or list of lines" in my above post. The point of my
solution is to never have (a) the whole file in memory and (b) to make a
random choice from a set of (initially) unknown size while you go through
it.

Peter




More information about the Python-list mailing list