Discussion:
[Tutor] R: Tutor Digest, Vol 138, Issue 26 Re: Problem on select esecution of object in a class (Alan Gauld)
Steven D'Aprano
2015-08-10 03:15:15 UTC
Permalink
Hi Jarod,

I'm not sure if you have received an answer to your question.

It might help if you use a subject line that describes your question.
Thanks so much fro the help. What I want to do is to obtain a selection of the
function I want to run.
ena = Rnaseq(options.configura, options.rst, options.outdir)
cmdset = [ ena.trimmomatic,
ena.star,
ena.merge_trimmomatic_stats
]
ena.show()
1 ena.trimmomatic
2 ena.star
3 ena.merge_trimmomatic_stats
The class RNaseq have multiple function. I want a way to run or from 1 to 3 or
from 2 to 3 or only the 2 o 3 step.
...
parser.add_option("-s", "--step",action="store", dest="steps",type="string",
help=" write input file: %prg -o : directory of results ")
python myscript -s 1,3 ...
step = cmd()
print i.command
but is not elegant so I want to know more what is the right way to generate a
execution f the function of the class by select which is the step we want to
start.
This is hard to answer since you don't tell us directly what cmdset is,
but I think I can guess. Something similar to this should work:


cmdset = ena.cmdset
options = "1,3" # for example
options = options.replace(",", " ").split()
# options now has ["1", "3"]
options = [int(s) for s in options]
# options now has [1, 3], integers, not strings

for opt in options:
cmd = cmdset[i-1] # remember Python starts counting at 0, not 1
print cmd()



Does that help?
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-10 08:12:27 UTC
Permalink
ena = Rnaseq(options.configura, options.rst, options.outdir)
cmdset = [ ena.trimmomatic,
ena.star,
ena.merge_trimmomatic_stats
Steven has shown you one option using a list.
I tend to prefer dictionaries for this kind of thing because
you can use a user-friendly key. So your cmdset might look like:

cmdset = { 'trim': ena.trimmomatic,
'star': ena.star,
'merge': ena.merge_trimmomatic_stats
}

You can then create a menu using the keys:

for key in cmdset: print(key)
cmd = input('command? ')

And call the command with

cmdset[cmd]()

If the commands have different parameter requirements
you might need a helper function to gather those params
too - it can also be in the dictionary:

cmdset = { 'trim': [ena.trimmomatic, get_trim_par],
'star': [ena.star, get_star_par],
'merge': [ena.merge_trimmomatic_stats, None]
}

So calling now becomes

if cmdset[cmd][1]:
cmdset[cmd][0](cmdset[cmd[1]())
else:
cmdset[cmd][0]()

And that can be put into a loop for multiple steps:

for cmd in steps:
if cmdset[cmd[[1]: etc...

If you need to get the steps from the command line
then you need to adapt that pattern to gather the
params in another way - possibly via a data file
(which could also be specified on the command line)
or by using defaults.


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...