Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Passing options between modules

Reply
Thread Tools

Passing options between modules

 
 
Chris
Guest
Posts: n/a
 
      05-21-2004
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
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      05-21-2004
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
 
Reply With Quote
 
 
 
 
David Goodger
Guest
Posts: n/a
 
      05-21-2004
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


 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      05-21-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      05-22-2004
David Goodger <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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/Coo.../Recipe/278844
which will make your life with optparse easier.
</shameless plug>


Michele Simionato
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between *options and options Sijo Kg Ruby 1 01-07-2009 05:51 AM
Passing Files between Server Modules oracle Java 1 04-22-2005 09:57 PM
RE: Variable passing between modules. Golawala, Moiz M (GE Infrastructure) Python 5 09-14-2004 01:31 PM
Variable passing between modules. Golawala, Moiz M (GE Infrastructure) Python 1 09-08-2004 09:37 PM



Advertisments