[Tutor] R: Tutor Digest, Vol 138, Issue 26 Re: Problem on select esecution of object in a class (Alan Gauld)
Alan Gauld
alan.gauld at btinternet.com
Mon Aug 10 10:12:27 CEST 2015
On 05/08/15 21:33, jarod_v6--- via Tutor wrote:
> 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
More information about the Tutor
mailing list