command-line args

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    command-line args

    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?

  • Peter Hansen

    #2
    Re: command-line args

    Michael wrote:
    [color=blue]
    > 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?[/color]

    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

    Comment

    • Michael

      #3
      Re: command-line args

      [color=blue]
      > 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...[/color]

      What do you do about multiple processes (of the same program) running at
      once? Save to /tmp/<pid>/globals.py or something like that?
      [color=blue]
      > (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.)[/color]

      I've done that before and yeh it is really a hassle with anything very
      large.

      Comment

      • Tim Daneliuk

        #4
        Re: command-line args

        Michael wrote:
        [color=blue]
        > 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?
        >[/color]

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



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

        option = value

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

        --
        ----------------------------------------------------------------------------
        Tim Daneliuk tundra@tundrawa re.com
        PGP Key: http://www.tundraware.com/PGP/

        Comment

        Working...