A few beginning questions

Bengt Richter bokr at oz.net
Mon Jul 14 16:51:20 EDT 2003


On Mon, 14 Jul 2003 14:03:17 GMT, Harry George <harry.g.george at boeing.com> wrote:

>"richardc" <richardc at hmgcc.gov.uk> writes:
>
>> Ive just started playing with Python and have a couple of questions.
>> 
>> Im looking for an agument parsing library (from the command line), Ive come
>> across optik and argtools.  What other ones are there are any of them any
>> good, is there a 'standard' lib for doing this.
>> 
>> Also how should I '#define' magic numbers in Python.  I spent ages looking
>> around for a 'define' statement or anything that will allow me to create a
>> constant value 'object'.  Am I missing something very obvious ?
>> 
>> Also, what 'IDE/editor' do people recomend for MAC OSX, so far Ive found
>> 'Hydra' to be the best as it works on plain files and has Python syntax
>> highlighting... what else is there and are any of them any good.
>> 
>> Has nobody written an editor/IDE in Python with wxWindows so that it runs on
>> all platforms ?
>> 
>> Many thanks
>> 
>> Rich
>> 
>> 
>
>1. args: I use getopt, which does the job.  The idiom is:
>if __name__ == '__main__':
>    opts,pargs=getopt.getopt(sys.argv[1:],'hvd',
>                 ['help','version','debug',
>                  'bb='])
>    for opt in opts:
>        if opt[0]=='-h' or opt[0]=='--help':
>            print modname+": version="+__version__
>            usage()
>            sys.exit(0)
>        elif opt[0]=='-v' or opt[0]=='--version':
>            print modname+": version="+__version__
>            sys.exit(0)
>        elif opt[0]=='-d' or opt[0]=='--debug':
>            debug_p=1
>        elif opt[0]=='--bb':
>            opt_b=opt[1]
>
You know, I don't know how much that buys vs the simple direct method. I haven't
tried getopt. Is there something I can't easily do with about the same LOC as below?

if __name__ == '__main__':
    import sys
    args = sys.argv[1:]
    while args:
        opt = args.pop(0)
        if opt=='-h' or opt=='--help':
            print modname+": version="+__version__
            usage()
            raise SystemExit(0)
        elif opt=='-v' or opt=='--version':
            print modname+": version="+__version__
            raise SystemExit(0)
        elif opt=='-d' or opt=='--debug':
            debug_p=1
        elif opt=='--bb':
            opt_b= int(args.pop(0)) # note that this way it's also easy to do conversions like int
            if not 0 <= opt_b <3:
                raise ValueError, 'Error: opt_b must be 0, 1, or 2 (easy to validate in context also)'
        else:
            #you can process non-opt args as file names or whatever and continue looping,
            # or you could break here and use the rest of what is in args however you want.
            # or break on an explicit '--' and print arg-error messages otherwise, etc. etc.










>2. #define: Use ALLCAPS.  I put these all in a globals section at the
>    top of the module in the order: docstring, imports, globals,
>    utilities, common functions, classes, "main", and "if
>    __name__=='__main__':"
>
>    Also, if you have global variables, we use leading "g_"
>    (e.g,"g_myglobaldata"), so they are easy to find if encountered in
>    the body of the module.
>
>-- 
>harry.g.george at boeing.com
>6-6M31 Knowledge Management
>Phone: (425) 294-8757

Regards,
Bengt Richter




More information about the Python-list mailing list