[Tutor] R: Tutor Digest, Vol 138, Issue 26 Re: Problem on select esecution of object in a class (Alan Gauld)

Steven D'Aprano steve at pearwood.info
Mon Aug 10 05:15:15 CEST 2015


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.

See below for my answer:


On Wed, Aug 05, 2015 at 10:33:01PM +0200, jarod_v6--- via Tutor wrote:
> 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 ...
> 
> At the moment the only way I found is this:
>  for cmd in cmdset: 
> 		 	step = cmd()
> 		 	for i in step:
> 		 		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


More information about the Tutor mailing list