473,386 Members | 1,736 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,386 software developers and data experts.

Is this optparse object abuse?

Hello,

I've recently found it convenient to do something like this:

options = optparse_function(sys.argv[1:])

##print options =>
##{option_one:4, option_two:[5, 3, 7, 8, 6], option_three:'/home/files'}

#(Note that this is not a dictionary, even though it looks like one; it's how
#an optparse instance reports what it's holding.)

def function_one(options):
foo = options.option_one
do stuff with foo

def function_two(options):
foo = options.option_one
bar = options.option_two
options.old_option_two = bar
bar = bar * foo
options.option_two = bar

def function_three(options):
blop = options.old_option_two
blip = options.option_three
do stuff with blip and blop
....

In other words, using the optparse object to hold as attributes everything
needed by all the functions and methods in the module, and simply passing it
holus bolus to all them and just pulling out what's actually needed inside
the function, even adding new attributes or reassigning old ones along the
way.

I find it convenient (esp. when there are a lot of options spread over a lot
of functions) because I don't need to fuss about with positional arguments,
keyword dictionaries, default values etc., and it's also easy to pass new or
altered stuff from one function or method to another without polluting the
namespace, as you only get the names out of the object when you assign a name
to the attribute inside a function or method, and vice-versa. And adding
a "feature" to a function is as easy as typing "options.blah".

And if this works, why not use a generic object for the same purpose when
options are not involved?

However, before I get too excited: it does seem too easy, and I have no idea
how these objects are implemented - for all I know I'm using a truck to
deliver a ping-pong ball.

My question is: is this horribly inefficient or otherwise wrong?

Thanks,

John O'Hagan
Nov 16 '08 #1
1 1894
On Sun, 16 Nov 2008, Bruno Desthuilliers wrote:
John O'Hagan a écrit :
[...]
>
In other words, using the optparse object to hold as attributes
everything needed by all the functions and methods in the module, and
simply passing it holus bolus to all them and just pulling out what's
actually needed inside the function, even adding new attributes or
reassigning old ones along the way.

Congratulations, you just reinvented globals and spaghetti-code.
[..]
I'm aware of the potential for such problems, which is why I posted this
question hoping for constructive advice.
>
My question is: is this horribly inefficient or otherwise wrong?

The only thing I can say is that I hope I'll *never* have to maintain
your code.
Regards,

John O'Hagan

Nov 17 '08 #2

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

Similar topics

1
by: Eric O. Angell | last post by:
Say I have class Header: def __init__(self): self.foo = 0 # ... more fields and I have some commandline options that can specify better values for things in the header, such as foo. If I try...
7
by: Henry Ludemann | last post by:
I've been writing an optparse alternative (using getopt) that is at a stage where I'd be interested in people's opinions. It allows you to easily creating command line interfaces to existing...
1
by: sector119 | last post by:
Hi I use optparse with callback action, my callback function return some value, but optparse does not store this value, options.callback_dest always is None. How can I store callback function...
5
by: Sébastien Boisgérault | last post by:
Any idea why the 'options' object in # optparse stuff (options, args) = parser.parse_args() is not/couldn't be a real dict ? Or why at least it does not support dict's usual methods ? The...
4
by: Count László de Almásy | last post by:
Is there a standard way with optparse to include a blurb of text after the usage section, description, and the list of options? This is often useful to include examples or closing comments when...
2
by: Tim Arnold | last post by:
Hi, I'm writing a command-line interface using optparse. The cli takes several options with a single action and several parameters to be used in the resulting worker classes. I've been passing...
0
by: Robert Kern | last post by:
Jeff Keasler wrote: If you code it up with unit tests and documentation, it has a good chance. But in the meantime, you can tell optparse to stop processing options using the standard "--"...
7
by: wannymahoots | last post by:
optparse seems to be escaping control characters that I pass as arguments on the command line. Is this a bug? Am I missing something? Can this be prevented, or worked around? This behaviour...
0
by: John O'Hagan | last post by:
Here's a strange one for you: I have a generator function which produces lists of numbers and takes options which influence the output. The generator contains a loop, and to enable the options...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.