A little disappointed so far

Graham Nicholls graham at rockcons.co.uk
Sun May 18 20:09:26 EDT 2003


Hi.  This is NOT a troll, I promise ( and I realise that that probably makes
it highly likely to be one!)

Anyway, I've been getting into Python because I feel I should - I've looked
at it several times b4, but never had the time.  However, it has such a
virtuous feel to it (structured, OO, etc), that I felt I must make the
time. I'm afraid I'm sorely disappointed, and am hoping that its just me. 
Just as background, I've been scripting for 15-20 years or so of my Unix
life, and teach "Unix tools & Utilities" for Learningtree, so I'm not a
newbie.

Heres a few things:
1.  Documentation - there are no good, up to date books - "Learning Python"
is poor, IMO. A little better is  "Programming Python", and better still is
"Core Python", but this is out of date - just covering 2.0 in an appendix. 
I can print the pdf version of the tutorial - Windoze just says "document
can't be printed" and prints 3 pages.  I use Linux 99% of the time, but
windoze is easy to duplex print from - I just can;t be bothered to set it
up from CUPS. One example of why I found the 2 by Mark Lutz poor - try and
except and finally.  I spent quite a while trying to do

try:
        open a file
except:
        print open failure message
finally:
        close the file

His examples are long-winded in their documentation, and still don't make it
clear that except & finally are mutually exclusive.  Core Python is _much_
better, IMO.

2. Its all just so long winded, especially as a shelltool.  I (really)
_hate_ to say it, but perl seems so much easier, and more practical.  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. 


3. I loved the idea of indenting code driving the execution, till I wrote a
bug trying to do i=i+1 at the wrong level.  In C or perl, this would leap
out at me, but I missed it in python.

Have a look at this :

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

My tabstops are set to 2 in vi - thats what I like - sorry that the editor
has expanded the m out to 8 .

#!/usr/bin/env python

import string,os,sys
import re

TRUE=1
FALSE=0
BAD_ARGS=1
OPEN_FAIL=2

debug=FALSE
version="0.1"
progname=""

def main():
        """Main routine for extract_fonttable program"""
        global progname, version, debug

        pname_re=re.compile("^.*/")

        progname=sys.argv[0]
        infiles,outfile=setup(sys.argv[1:])
        print ("Reading files %s; creating file %s" % (infiles,outfile))

def setup(arglist):
        """     Process all command line args and check all files, etc
                        arglist already has been stripped of program name"""

        global progname, version, debug

        outfile=""
        flist=[]

        ok_opts="dvo"
        argc=len(arglist)

        if debug:
                print ("arglist=%s, argcount=%d" % ( arglist,argc))

        i=0
        while (i < argc):
                opt=arglist[i]

                if opt[0] == "-":               # i.e. its an option

                        option=opt[1:]
                        print ("Option %s found " % opt[1:] )

                        if option not in ok_opts:
                                usage("What is option %s" % option )
                                sys.exit(BAD_ARGS)

                        # Flag type options:
                        if option == 'd':
                                print ("%s DEBUG - debug activated" % progname)
                                debug=TRUE

                        if option == 'v':
                                verbose=TRUE
                                print ("%s version %s" % (progname,version))

                        # Others - take args
                        if option == 'o':               # Output file specifier
                                if len (opt) > 2:       # i.e. there's no space after -o
                                        print "Outfile is %s" % opt[2:]
                                        outfile=opt[2:]
                                else:
                                        i=i+1
                                        try: 
                                                outfile=arglist[i]
                                                if outfile[0] == '-':
                                                        raise IndexError
                                        except IndexError:
                                                usage("Option -o requires a filename")
                                                sys.exit(BAD_ARGS)
                                
                else:   # Not an option, so assume it must be a filename
                        flist.append(opt)

                                # Try to open the file
                        try:
                                        infile=open(opt,"r")
                        except IOError:
                                print ("Error opening file %s" % opt)
                                sys.exit(OPEN_FAIL)
                        else:
                                infile.close()
                                
                
                i=i+1

        # Check we've got an output file
        if outfile=="":
                usage ("No output file specified")
                exit(BAD_ARGS)

        # and at least one input file
        if len(flist)==0:
                usage ("No input file specified")
                exit(BAD_ARGS)
        
        return flist,outfile
###############################################################################

def usage(errmsg):
        """Print an error message, then a usage string, and return"""
        global progname, version, debug

        umsg="""
exft attempts to extract the font table from an rtf file or files.  It is 
called thusly:

        exft -h(elp) -d(ebug) -o(utput)[out_fname]
        """

        print errmsg
        print umsg
        return
###############################################################################

if __name__ == '__main__': # ie we're _not_ a module
        main()


A few things seem very hard - so, should I persevere?  I'm reasonably smart,
and have always been attracted to Python (except in that I can't _stand_
Monty Python, and find people who can quote it verbatim extremely annoying
;-).  I liked the indentation thing, till it bit me.  I suppose I could try
bigger tabstops.  I like the OO. But it just seems too slow, where I picked
up perl very quickly.  I get the impression I could do "big stuff" with
Python, where I wouldn't with perl (I'd use C or C++).

So no flames please - enthuse me some more!

Graham 
-- 
Graham Nicholls
All round good guy.




More information about the Python-list mailing list