Passing options between modules

Discussion in 'Python' started by Chris, May 21, 2004.

  1. Chris

    Chris Guest

    I'm trying to come up with a not-so-ugly manner of passing many
    command-line options between modules.

    I have a Steering.py file, which contains my main() and handles the
    getopts getting of several command-line options. Since having 5-6
    additional parameters for every class instantiation or function call
    is obviously an ugly way to handle this, what would you folks
    recommend as a superior option? Maybe passing a dict of options to
    each object/function?

    Help on this topic is greatly appreciated.

    Go Sox,
    -cjl
     
    Chris, May 21, 2004
    #1
    1. Advertising

  2. Chris

    Peter Hansen Guest

    Chris wrote:

    > I'm trying to come up with a not-so-ugly manner of passing many
    > command-line options between modules.


    In a pinch (i.e. if the rest of your application is such that
    you don't have a cleaner alternative), create an empty module
    called something like Globals.py and set values in its namespace
    to reflect the command line options. Then any module that
    needs it can just "import Globals" to get access to the data.

    All the usual "don't do that" cautions about globals apply,
    including how they can make a mess of your code, make it
    very hard to tell how data gets passed around, reduce
    maintainability, etc.

    Without knowing more about the structure and purpose of
    the code, it's hard to suggest something that is clearer
    more suitable and would definitely work.

    -Peter
     
    Peter Hansen, May 21, 2004
    #2
    1. Advertising

  3. Chris wrote:
    > I'm trying to come up with a not-so-ugly manner of passing many
    > command-line options between modules.


    Use optparse.py. It's standard in Python 2.3, or get it from
    http://optik.sf.net for older versions of Python.

    The result of parsing command-line options is a "Values" object,
    with standard dotted-attribute access. For example, for
    "--an-option", you'd say "options.an_option". It's easy to pass
    the single Values object around.

    -- David Goodger
     
    David Goodger, May 21, 2004
    #3
  4. Chris

    Larry Bates Guest

    One suggestion would be:

    Create small class, something like:

    class globaloptions:
    pass

    Then assign your options to attributes of this
    class:

    globaloptions.option1=value
    globaloptions.option2=value
    ....
    globaloptions.optionn=value

    Then pass globaloptions class to everyone else:

    a=Aclass(opt1, opt2, globaloptions)
    bFunction(globaloptions)
    ....

    Larry Bates
    Syscon, Inc.

    "Chris" <> wrote in message
    news:...
    > I'm trying to come up with a not-so-ugly manner of passing many
    > command-line options between modules.
    >
    > I have a Steering.py file, which contains my main() and handles the
    > getopts getting of several command-line options. Since having 5-6
    > additional parameters for every class instantiation or function call
    > is obviously an ugly way to handle this, what would you folks
    > recommend as a superior option? Maybe passing a dict of options to
    > each object/function?
    >
    > Help on this topic is greatly appreciated.
    >
    > Go Sox,
    > -cjl
     
    Larry Bates, May 22, 2004
    #4
  5. David Goodger <> wrote in message news:<>...
    > Chris wrote:
    > > I'm trying to come up with a not-so-ugly manner of passing many
    > > command-line options between modules.

    >
    > Use optparse.py. It's standard in Python 2.3, or get it from
    > http://optik.sf.net for older versions of Python.
    >
    > The result of parsing command-line options is a "Values" object,
    > with standard dotted-attribute access. For example, for
    > "--an-option", you'd say "options.an_option". It's easy to pass
    > the single Values object around.
    >
    > -- David Goodger


    I second this.
    <shameless plug>
    You may also want to look at
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278844
    which will make your life with optparse easier.
    </shameless plug>


    Michele Simionato
     
    Michele Simionato, May 22, 2004
    #5
    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. Cliff  Martin
    Replies:
    1
    Views:
    3,042
    Larry Smith
    Jan 31, 2007
  2. jacksu
    Replies:
    0
    Views:
    512
    jacksu
    Oct 9, 2007
  3. Utkado
    Replies:
    2
    Views:
    2,118
  4. Bryan Richardson
    Replies:
    6
    Views:
    289
    Bryan Richardson
    Feb 25, 2008
  5. Sijo Kg
    Replies:
    1
    Views:
    102
    Justin Collins
    Jan 7, 2009
Loading...

Share This Page