Passing options between modules

C

Chris

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
 
P

Peter Hansen

Chris said:
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
 
D

David Goodger

Chris said:
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
 
L

Larry Bates

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.
 
M

Michele Simionato

David Goodger said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top