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
I'd like to use a call such as:
do_stuff(args, **options)
This function signature is handy if you also need
sometimes to call 'do_stuff' from the Python interpreter.
Cheers,
SB 5 2535
Sébastien Boisgérault wrote: 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 ?
Well, it's not a real dict because the original API intends it to be
used as object attributes. However, if you need a dict, it's pretty
simple -- use vars() or .__dict__:
py> import optparse
py> p = optparse.OptionParser()
py> p.add_option('-x')
<Option at 0x11a01e8: -x>
py> options, args = p.parse_args(['-x', '0'])
py> options.x
'0'
py> vars(options)
{'x': '0'}
py> options.__dict__
{'x': '0'}
STeVe
Steven Bethard wrote: Sébastien Boisgérault wrote: 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 ? Well, it's not a real dict because the original API intends it to be used as object attributes.
Sure ;). But what are the pros of this choice ? The option __str__
mimicks the behavior of a dict. Why not a full interface support
of it ?
However, if you need a dict, it's pretty simple -- use vars() or .__dict__:
Agreed. 100%.
SB
Sébastien Boisgérault wrote: Steven Bethard wrote:
Sébastien Boisgérault wrote:
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 ?
Well, it's not a real dict because the original API intends it to be used as object attributes.
Sure ;). But what are the pros of this choice ? The option __str__ mimicks the behavior of a dict. Why not a full interface support of it ?
Well one reason might be that it's easy to convert from an object's
attributes to a dict, while it's hard to go the other direction:
py> options.x, options.y
('spam', 42)
py> vars(options) # convert to dict
{'y': 42, 'x': 'spam'}
versus
py> options['x'], options['y']
('spam', 42)
py> o = ??? # convert to object???
....
py> o.x, o.y
('spam', 42)
Though I had been working on a namespace module[1] with Nick Coghlan and
Carlos Ribeiro that provided such behavior:
py> options['x'], options['y']
('spam', 42)
py> o = namespace.Namespace(options)
py> o
Namespace(x='spam', y=42)
py> o.x, o.y
('spam', 42)
However, the namespace module is not part of the Python stdlib, so by
providing an object with attributes instead of a dict, optparse supports
(using only builtin functions) both users that want an object with
attributes and users that want a dict.
STeVe
[1] http://namespace.python-hosting.com/
Steven Bethard wrote: Well one reason might be that it's easy to convert from an object's attributes to a dict, while it's hard to go the other direction:
... py> options['x'], options['y'] ('spam', 42) py> o = ??? # convert to object??? ... py> o.x, o.y ('spam', 42)
"hard" == "slightly less easy"?
class Spam:
def __init__(self, d):
self.__dict__.update(d)
then
o = Spam(options)
or use the types module (if you have a classic class) import types class Spam: pass
.... o = types.InstanceType(Spam, {"x": 5, "y": 10}) o.x
5
My guess is the original intent was to make the command-line
parameters act more like regular variables. They are easier
to type (x.abc vs. x["abc"]) and the syntax coloring is different.
Andrew da***@dalkescientific.com
Andrew Dalke wrote: Steven Bethard wrote:
Well one reason might be that it's easy to convert from an object's attributes to a dict, while it's hard to go the other direction:
...
py> options['x'], options['y'] ('spam', 42) py> o = ??? # convert to object??? ... py> o.x, o.y ('spam', 42)
"hard" == "slightly less easy"?
No, sorry, "hard" -> "harder". Typo. For simple cases like this, it's
obviously not difficult, though it does take more work than the opposite
direction.
If you didn't catch the old discussions about some of the details, I
think they're under something like "generic objects" or "namespace
objects". You can check the archives if you're interested.
STeVe This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Hans-Joachim Widmaier |
last post by:
I was really pleased when the Optik module found its way into the
standard Python battery compartment, as it matched all my option
parsing requirements. But, as always, there's really nothing that...
|
by: washu |
last post by:
Hi,
I'm was going through the module help located on
http://docs.python.org/lib/optparse-store-action.html and I tried
modifying it a tiny bit and things don't work. If someone could tell me...
|
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...
|
by: Norbert Thek |
last post by:
Hi
I'm using Python 24 on Windows > (2k)
Is there an easy way to convince optparse to accept newline in the helpstring?
and more importand also in the 'desc' string. I tried everything
(from...
|
by: Karlo Lozovina |
last post by:
If I create a file with only one line:
---
from optparse import OptionParser
---
I get this when I try to run it from "DOS" prompt:
Traceback (most recent call last):
File "optparse.py",...
|
by: Bob |
last post by:
I'd like to setup command line switches that are dependent on other
switches, similar to what rpm does listed below. From the grammar below
we see that the "query-options" are dependent on the...
|
by: Steven Bethard |
last post by:
I feel like I must be reinventing the wheel here, so I figured I'd post
to see what other people have been doing for this. In general, I love
the optparse interface, but it doesn't do any checks...
|
by: Pupeno |
last post by:
Hello,
I am doing some extreme use of optparse, that is, extending it as explained
on
http://docs.python.org/lib/optparse-other-reasons-to-extend-optparse.html
I have subclassed OptionParser and...
|
by: mbeachy |
last post by:
Some rather unexpected behavior in the set_default/set_defaults
methods for OptionParser that I noticed recently:
<Option at 0x-483b3414: -r/--restart>
{'restart': None}
{'retart': False,...
|
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 "--"...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
| |