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

From: Adelein and Jeremy (adeleinandjeremy_at_yahoo.com)
Date: 03/29/04


Date: Mon, 29 Mar 2004 12:35:00 -0800 (PST)
To: python-list@python.org

I have recently completed a mini-project with the goal of rewriting
in Python Eric Raymond's fortune.pl script
(http://www.catb.org/~esr/fortunes/fortune.pl-unconfuse-apache),
except that instead of generating a sigline for mail, it prints a
fortune to screen in the traditional manner of the Unix fortune. I
have succeeded in terms of functionality, but because I am only a
novice programmer, I would appreciate any comments, criticism,
suggestions, alternative options, etc. that more experienced
programmers would be willing to give, whether technical or stylistic
in nature. Here is my code:

#! /usr/bin/env python

## fortune
## Jeremy Conn
## Version 0.9, 20020329
## GNU GPL http://www.fsf.org/licenses/gpl.html
## Description: Generates a random fortune for display onscreen from
## a single file of quotes which the user maintains; the
## quotes can be multi-line, and are separated by lines
## containing only a percent sign (same format as
## traditional fortune files).

import random
import re
import sys

FORTUNES_FILE = ".fortune"

# What file should we use?
if len(sys.argv) > 1:
    fortunes_file = sys.argv[1]
else:
    fortunes_file = FORTUNES_FILE

# Let's see if we can open that file for reading
try:
    fi = open(fortunes_file, 'r')
except:
    sys.exit("Cannot open fortunes file %s." % fortunes_file)

# Collect the file pointers to each fortune entry in the file
fp_entry = [0]
line = fi.readline()
while line != "":
    if re.match(r'^%$', line):
        fp_entry.append(fi.tell())
    line = fi.readline()

# Seek to a random entry
try:
    fi.seek(random.choice(fp_entry))
except:
    sys.exit("Cannot seek.")

# Add the entry to output message and then print it
fortune = ''
line = fi.readline()
while line != '':
    if re.match(r'^%$', line):
        break
    fortune += line
    line = fi.readline()
print fortune

# Return fp to beginning of file, close the file, and exit program
fi.seek(0,0)
fi.close()
sys.exit()

- Jeremy
adeleinandjeremy@yahoo.com

__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html



Relevant Pages

  • Re: Run-time error, type mismatch 13
    ... You should stop worrying about it if there's a programmer involved. ... their responsibility to fix it, whatever good fortune they might have had ... > The creator of this program claims he has not had anyone else have any ...
    (microsoft.public.vb.general.discussion)
  • Re: [Full-disclosure] Vunerability in yahoo webmail.
    ... Yahoo and Google security engineers! ... a crack commando unit was sent to social prison by a mailing ... they survive playing soldiers of fortune. ...
    (Full-Disclosure)
  • Re: ESRs fortune.pl redone in python - request for critique
    ... Adelein and Jeremy wrote: ... > in Python Eric Raymond's fortune.pl script ... > fortune to screen in the traditional manner of the Unix fortune. ... > # Collect the file pointers to each fortune entry in the file ...
    (comp.lang.python)