[Tutor] Execution of local data

Alan Gauld alan.gauld at btinternet.com
Wed Jan 28 00:31:29 CET 2015


On 27/01/15 22:47, jarod_v6 at libero.it wrote:
> I create a  list of local  object: but I can't execute as object only as string:
> ena = Rnaseq(options.configura, options.rst)
>
>
>          job_1 = ena.trimmomatic()
>
>          job_2 = ena.star()
>          job_3 = ena.wiggle()
>          job_4 = ena.rnaseqc()
>          job_5 = ena.picard_sort_sam()
>          job_6 = ena.cufflinks
>          job_7 = ena.merge_trimmomatic_stats()

Note that you are storing the results of the function calls here.
I suspect you want to store the function objects themselves (ie no 
parens after the name)

>          cmdset=[]
>          for item in locals().keys():
>              if item.startswith('job_'):
>                  cmdset.append(item)

But this looks like overkill, why not just create a list of commands 
directly?

>          cmdset.sort()
>          for i in range(0,len(cmdset)):
>               ena.prepare_run(cmdset[i])

Please get out of the habit of using indexes in Python for loopps.
This is better written as

for cmd in cmdset:
      ena.prepare_run(cmd)

>   for i in range(0,len(cmdset)):
>                      cmd=cmdset[i]
>                      print type(cmd)

same here:

for cmd in cmdset:
     print type(cmd)

> How can use the object created?

We must assume (since we can't see them) that the fuctions
above (ena.star etc) all return strings. If you remove the
parens you will get the function objects themselves.


But it all looks more complex than it need be.
Could something like this work:

ena = Rnaseq(options.configura, options.rst)
cmdset = [ ena.trimmomatic,
            ena.star,
            ena.wiggle,
            ena.rnaseqc,
            ena.picard_sort_sam,
            ena.cufflinks,
            ena.merge_trimmomatic_stats
          ]

for cmd in cmdset:
     cmd()

-- 
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