Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67307 > unrolled thread
| Started by | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| First post | 2014-03-01 16:43 +0530 |
| Last post | 2014-03-01 17:47 +0530 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Python : parsing the command line options using optparse Ganesh Pal <ganesh1pal@gmail.com> - 2014-03-01 16:43 +0530
Re: Python : parsing the command line options using optparse Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-01 11:47 +0000
Re: Python : parsing the command line options using optparse Ganesh Pal <ganesh1pal@gmail.com> - 2014-03-01 17:47 +0530
| From | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| Date | 2014-03-01 16:43 +0530 |
| Subject | Re: Python : parsing the command line options using optparse |
| Message-ID | <mailman.7507.1393672393.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
>
>
>
> Thanks Peter and Simon for the hints it worked : ) without ' ='
>
> # Python corrupt.py -o INODE -p /ifs/1.txt -q SET -f 1
>
> Current Default Choice :
>
> Choice: INODE
> Choice: SET
> Choice: 1
>
>
>
>
Iam done with the command line parsing but got stuck while trying to
implement switch kind of behavior with dictionaries. So posting 2 more
questions
Question 1 :
Iam using the options.name directly for manipulations is this fine or do
I need to assign it to variable and then use it
Example:
Entered at cli #python corrupt.py -object_type INODE --path_name/ifs/1.txt
-operation_type SET
Initialize all the command line option and then use it
object_type = options.object_type
path_name = options.path_name
if object_type == 'LIN':
corrupt_inode()
elif object_type == 'DATA':
corrupt_data()
OR
if options.object_type == 'LIN':
corrupt_inode()
elif options.object_type == 'DATA':
corrupt_data()
elif options.object_type == 'INODE':
corrupt_data()
#output
#python corrupt.py -object_type INODE -p /ifs/1.txt -q SET -f 10
-m 10 -n 123 -l -c
Corrupted inode
_________________________________________________________________________________________________________
Question 2 :
I wanted to use dictionary to match the above if else behavior (we don't
have switch in python I guess ) and If else looks very untidy.
Is it possible to store the options.object_type as a key in the dictionary
and then based on the value entered in the command line invoke the
appropriate function
I tried creating a dictionary like this but Iam getting a wrong output
object_type_dictonary = { 'LIN' : corrupt_inode(),
'INODE' : corrupt_lin(),
'DATA' : corrupt_data(),
};
and then ran # python corrupt.py -object_type= inode ( This prints all
the values for me)
Example :
Corrupted inode
Corrupted LIN
Corrupted data
PS : If user enters object_type= inode it should execute corrupt_inode
and print corrupted inode
Any help on tips highly helpful :)
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-03-01 11:47 +0000 |
| Message-ID | <5311c8b7$0$29985$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #67307 |
On Sat, 01 Mar 2014 16:43:11 +0530, Ganesh Pal wrote:
> Iam done with the command line parsing but got stuck while trying to
> implement switch kind of behavior with dictionaries. So posting 2 more
> questions
You should start new threads for new questions. The subject line here has
nothing to do with the questions you ask.
> Question 1 :
>
> Iam using the options.name directly for manipulations is this fine or
> do I need to assign it to variable and then use it
It is perfectly fine to use options.name directly. There's no need to
save it to a temporary variable just to use it, unless doing this makes
your code easier to read.
> Question 2 :
>
> I wanted to use dictionary to match the above if else behavior (we don't
> have switch in python I guess ) and If else looks very untidy.
>
> Is it possible to store the options.object_type as a key in the
> dictionary and then based on the value entered in the command line
> invoke the appropriate function
Yes. You almost got it right here:
> object_type_dictonary = { 'LIN' : corrupt_inode(),
> 'INODE' : corrupt_lin(),
> 'DATA' : corrupt_data(),
> };
(By the way, this is Python, not C, and there is no need for redundant
semi-colons at the end of each line.)
The mistake you made is that you *called* the functions inside the dict.
Instead, just refer to the function object itself, without calling it:
object_type_dictonary = {
# no parens after the functions
'LIN' : corrupt_inode,
'INODE' : corrupt_lin,
'DATA' : corrupt_data,
}
Then, after you look up the object type, then and only then call the
function:
func = object_type_dictionary[object_type]
func()
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| Date | 2014-03-01 17:47 +0530 |
| Message-ID | <mailman.7511.1393676702.18130.python-list@python.org> |
| In reply to | #67310 |
[Multipart message — attachments visible in raw view] — view raw
On Sat, Mar 1, 2014 at 5:17 PM, Steven D'Aprano < steve+comp.lang.python@pearwood.info> wrote: > > You should start new threads for new questions. The subject line here has > nothing to do with the questions you ask. > > Sure Steven and thanks for replying and your suggestion for Question 2 ( same as Peter Otten ) worked and Iam closing this thread now :)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web