Hello,
I've recently found it convenient to do something like this:
options = optparse_functi on(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(op tions):
foo = options.option_ one
do stuff with foo
def function_two(op tions):
foo = options.option_ one
bar = options.option_ two
options.old_opt ion_two = bar
bar = bar * foo
options.option_ two = bar
def function_three( options):
blop = options.old_opt ion_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.bl ah".
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 1 1909
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the obvious (to me):
parser = optparse.OptionParser()
parser.add_option('--secret', type='string', dest='header', default=Header())
|
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 functions, using
flags (which are optional) and arguments. It will automatically print a
nicely formatted usage (eg: -h or --help), and easily & automatically
validates parameter existence and type.
You can download it, and read a bit more about it,...
|
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 return value or callback option value
like store action do?
I modify optparse.py Option::take_action def and add there:
|
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 next move after a parse_args is often to call
a method 'do_stuff' with the args and options and
|
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 the help
message is printed out. Many of the GNU commands do this.
It would look something like this:
% program --help
usage: program
| |
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 parameters from optparse to the workers in two ways:
(1) creating a Globals.py module, set parameters once in the cli code
and read it
when needed in the worker class methods. Something like this:
import Globals
|
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 "--" marker. For example:
$ cat mycommand.py
import optparse
parser = optparse.OptionParser()
|
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 doesn't occur with non-control characters.
For example, if this program (called test.py):
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-d", dest="delimiter", action="store")
|
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 to have a different value on each iteration, the options may
themselves be instances of the same generator, in the form of attributes of
an optparse object. In the body of the loop, the next() method is called on
each generator to get the new value...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |