command-line args

Cameron Laird claird at lairds.com
Sat Apr 24 18:32:09 EDT 2004


In article <mailman.5.1082827225.27400.python-list at python.org>,
Peter Hansen <peter at engcorp.com> wrote:
			.
			.
			.
>What I mean is this.
>
>You create globals.py ahead of time, the usual way with a text
>editor.  It can contain defaults if you wish, or be empty. E.g.:
>
>(globals.py)
>logging = False
>userName = None
>timeout = 5.0
>
>Then you simply import this where you are doing the command-line
>argument parsing:
>
>import globals, getopt
>opts, args = getopt.getopt(sys.argv[1:], 'at:fse:')  # or whatever
>for opt, val in opts:
>    if opt = '-t':
>        globals.timeout = float(val)
>    # etc etc
>
>
>Then, elsewhere where you need to use the values, just do another
>import.
>
>(some code that needs to know the timeout)
>
>import globals, time
>time.sleep(globals.timeout)
># or whatever
>
>
>There is no reason to create the .py file on the fly...
>
>(This works because after the first import of a module inside an
>application, subsequent imports do *not* re-read the .py file,
>but simply get a reference to the already-imported module object
>from the sys.modules dictionary.)
			.
			.
			.
You seem to have given Michael everything he needed.  I don't
get it, though.  Why must "some code that needs to know the
timeout" import globals?  In what context would it not already
be present?
-- 

Cameron Laird <claird at phaseit.net>
Business:  http://www.Phaseit.net



More information about the Python-list mailing list