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

Donn Cave donn at u.washington.edu
Wed Mar 31 20:39:41 CEST 2004


In article <mailman.155.1080705831.20120.python-list at python.org>,
 Adelein and Jeremy <adeleinandjeremy at yahoo.com> wrote:

[... re "while line:" ]

> Yes, but I can't seem to get it through my thick skull that Python
> was designed well enough to allow me to use datatypes sensibly as
> booleans.

I guess that's true (or rather, True), but since you seem to
be interested in wringing all the insight you can out of this,
you might be interested in a classic point of view about datatypes
and booleans.  Laura Creighton articulated something like a
`booleans considered harmful' position in a classic series of
posts 2 years ago, when explicit Boolean values were initially
proposed for Python.

http://groups.google.com/groups?selm=mailman.1017755045.4308.python-list%
40python.org&output=gplain,
or if you'd like to see the whole thread, search comp.lang.python
for "PEP 285: Adding a bool type"

Her argument didn't carry, but it's interesting and in my opinion
very right on.

...
> So if I am understanding correctly, I want to do the following:
> 
> try:
>     fi = open(fortunes_file, 'r')
> except IOError:
>     sys.exit("Cannot open fortunes file %s." % fortunes_file)
> 
> Out of curiosity, what else could the error be mapped to, and how
> does that work, or where can I go to read up on such issues? Also, in
> this particular case, where I am simply exiting the program with an
> error message, could the error possibly be mapped to anything else,
> and if so, would it really matter?

I'm not sure I follow the mapping notion you're asking about,
so I'm just going to volunteer some stuff about exceptions.
Exceptions are the trickiest thing.  They naturally tend to be
unpredictable, and it's annoying to find out that your exception
handler has run into an exception itself while trying to process
the exception data it gets.  With that caveat in mind, IOError
carries more useful information than average.  For example, if
you're not satisfied with its str() coercion -

   try:
       fi = open(fortunes_file, 'r')
   except IOError, v:
       print v

[Errno 2] No such file or directory: '.fortunes'

... you can emulate the typical perror() style

       print '%s: %s' % (v.filename, v.strerror)

.fortunes: No such file or directory

You can't assume that just any exception you may get will have
a strerror attribute, most won't.  You can't assume that every
exception of a particular type will have the same number of
elements in its args attribute, etc.  So you really have to
approach an exception value with a long stick, then gloves and
safety glasses, but on the bright side you can squeeze useful
data out of them.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list