command-line args

Discussion in 'Python' started by Michael, Apr 24, 2004.

  1. Michael

    Michael Guest

    What's the perfered method of passing command-line args throughout a
    Python program given that globals don't seem to really exist? I thought
    of writing them out to a python file and then importing them when
    needed.. but that seems like it'd only work for a single process.. not
    in instances where more than one user is running the program. There
    isn't anything like a virtual file space that could be used for this?
    Anyway to set a global variable?
     
    Michael, Apr 24, 2004
    #1
    1. Advertising

  2. Michael

    Peter Hansen Guest

    Michael wrote:

    > What's the perfered method of passing command-line args throughout a
    > Python program given that globals don't seem to really exist? I thought
    > of writing them out to a python file and then importing them when
    > needed.. but that seems like it'd only work for a single process.. not
    > in instances where more than one user is running the program. There
    > isn't anything like a virtual file space that could be used for this?
    > Anyway to set a global variable?


    Create an empty module called "globals" and import that wherever
    needed. Populate it with settings from the command-line parsing
    stage. Get fancier and put defaults in there to begin with, and
    override them only if specified in the command-line parsing
    area. Flavour as needed...

    (That's just one option, but in many ways the simplest. Some of
    us also use a simple "bag" type of object which we pass around
    as required, but that's more awkward with really large applications.)

    -Peter
     
    Peter Hansen, Apr 24, 2004
    #2
    1. Advertising

  3. Michael

    Michael Guest


    > Create an empty module called "globals" and import that wherever
    > needed. Populate it with settings from the command-line parsing
    > stage. Get fancier and put defaults in there to begin with, and
    > override them only if specified in the command-line parsing
    > area. Flavour as needed...


    What do you do about multiple processes (of the same program) running at
    once? Save to /tmp/<pid>/globals.py or something like that?

    > (That's just one option, but in many ways the simplest. Some of
    > us also use a simple "bag" type of object which we pass around
    > as required, but that's more awkward with really large applications.)


    I've done that before and yeh it is really a hassle with anything very
    large.
     
    Michael, Apr 24, 2004
    #3
  4. Michael

    Tim Daneliuk Guest

    Michael wrote:

    > What's the perfered method of passing command-line args throughout a
    > Python program given that globals don't seem to really exist? I thought
    > of writing them out to a python file and then importing them when
    > needed.. but that seems like it'd only work for a single process.. not
    > in instances where more than one user is running the program. There
    > isn't anything like a virtual file space that could be used for this?
    > Anyway to set a global variable?
    >


    If I may be so immodest, I have just released this as another way
    to set program options:

    http://www.tundraware.com/Software/tconfpy/

    Keep your options in a text file with each option in the form:

    option = value

    Call tconfpy.ParseConfig("myconfigfile") and you'll get back
    (among other things) a populated symbol table with each option as
    one of the keys...

    --
    ----------------------------------------------------------------------------
    Tim Daneliuk
    PGP Key: http://www.tundraware.com/PGP/
     
    Tim Daneliuk, Apr 25, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Varn
    Replies:
    2
    Views:
    642
    Ken Varn
    Jun 22, 2005
  2. Replies:
    3
    Views:
    506
    David Eppstein
    Sep 17, 2003
  3. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    710
    Pierre Fortin
    Sep 18, 2004
  4. er
    Replies:
    2
    Views:
    520
  5. Andrew Tomazos
    Replies:
    5
    Views:
    600
Loading...

Share This Page