473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing options between modules

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
Jul 18 '05 #1
4 2145
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
Jul 18 '05 #2
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
Jul 18 '05 #3
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" <ia*******@hotmail.com> wrote in message
news:93**************************@posting.google.c 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

Jul 18 '05 #4
David Goodger <go*****@python.org> wrote in message news:<ma*************************************@pyth on.org>...
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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roberts | last post by:
Hi, I managed to get the Java extension working on php 4.3.10 after following various bits of advice from http://www.thelinuxpimp.com/main/modules.php?op=modload&name=News&file=article&sid=419...
5
by: Tom Willis | last post by:
How are the expert pythoneers dealing with config files? Is there anything similair to .net's config files or java's .properties? A quick search on google didn't return anything that looked...
6
by: chung.ley | last post by:
Hi, Another newbie question... I have to write a program that process a huge amount of data; the end results will be a number of different reports. I will prefer to write a separate...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
1
by: Kevin Lyons | last post by:
Hello, I am trying to get all of my form elements passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html to the following URL:...
2
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have no...
16
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
7
by: arkam | last post by:
Well everything is in the title ! Arkam
7
by: Leo Breebaart | last post by:
I have another question where I am not so much looking for a solution but rather hoping to get some feedback on *which* solutions people here consider good Pythonic ways to approach a issue. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.