Holy macaroni I stink at this

Mark McEahern marklists at mceahern.com
Tue Feb 11 21:44:35 CET 2003

[Hutchins, Mike]
> Ok, I am trying my damnedest to learn python and programming in general.
> I have this amazingly ugly script I am trying to write.
> It works as long as there are no errors. Can anyone point me to a
> resource that may be a little more insightful for trapping errors and
> what to do with them?
> I can post my "amazingly ugly code"(tm) if anyone wants a good laugh.
> :-)

Hey, welcome to the club!  I would encourage you to keep the spirit that led
you to solve the problem at hand.  Sounds to me like you just need
refinement around the edges.  For that, I'd suggest:

1) Consider writing tests before you code.  I've never regretted the few
times I've successfully done this.  Look up the unittest module.  Mark
Pilgrim's Dive Into Python has a good chapter on unit testing.

2) Identify the runtime errors you expect and trap them with try/except
where appropriate.

For instance, suppose you're writing a program that takes a filename
parameter; you can display a usage message if the parameter is missing:

import sys
import os

usage = '%s filename\n' % os.path.basename(sys.argv[0])
    filename = sys.argv[1]
except IndexError:

// m


More information about the Python-list mailing list