A little disappointed so far

Skip Montanaro skip at pobox.com
Sun May 18 21:18:59 EDT 2003


    >> I am not a huge fan of perl, because of its inbuilt "obfuscability",
    >> but I can quickly get things done, like stripping off the pathname of
    >> my program, so argv[0] is progname, not ./progname, or
    >> /usr/local/bin/progname.

    Erik> There's a function which does this:  os.path.split.

Graham,

Once you've got the basics of the language under your belt, which shouldn't
take long, you'll find that the greatest gains are to be made in familiarity
with the modules distributed with Python.  Whereas in Perl you might have a
very small number of "use" statements in a script, in Python, you'll
generally import a fair number of modules.  Erik's reference to
os.path.split is just the smallest tip of that iceberg.  If you want to use
regular expressions you import re.  Bits and pieces related to grubbies in
the interpreter ?  There's the sys module for that.  Operating system
interface?  Try the os module.  And so on.

I think you'll find this bookmark very useful:

    http://www.python.org/doc/current/modindex.html

    >> I'm just parsing some options (I don't like getopts, and parsing a
    >> command line ought to be easy).

    Erik> It seems curious to me that you're ignoring the standard and
    Erik> extraordinarily simple mechanism of getopt simply so that you can
    Erik> go through the bother of parsing command lines manually, and then
    Erik> complaining that Python is "long winded."  Sure, it's long winded
    Erik> if you try to do everything on your own.

Agreed.  There is no "perfect" options parsing system.  Still, using what's
there already will save you writing a lot of code.  Getopt is not difficult
for the standard Unix-style command line flag processing.  Here's a simple
example I grabbed from a script I've been working on recently:

    try:
        opts, args = getopt.getopt(args, "p:ao")
    except getopt.GetoptError, msg:
        usage(msg)
        sys.exit(1)

    pfile = None
    display_applied = False
    display_ordering = False
    for opt, arg in opts:
        if opt == "-p":
            pfile = arg
        elif opt == "-a":
            display_applied = True
        elif opt == "-o":
            display_ordering = True

If nothing you encounter is "perfect" and you decide you need perfection,
you'll wind up reimplementing a lot of stuff already in the standard
library.

Skip





More information about the Python-list mailing list