开发者

How can I give parameters to a function of a python script through the command-line?

Currently this is my python script (TagGen) which has one function:

def SJtag(file,len_tag):

    import csv
    reader = csv.reader(open(file), dialect='excel-tab' )
    for row in reader:
        qstarts = row[1].split(",")[1:-1] 
        n = len_tag/2
        for i in qstarts:  
            name = row[0]

            start = int(i)-n
            if start<0:
                start = 0      

            end = int(i)+n
            if end>len(row[2]):
                end=len(row[2])

            tag = row[2][start:end]    
            print name, i, tag, len(tag)

SJtag("QstartRefseqhg19.head",开发者_JS百科80)

I want to give the file and len_tag parameters of the SJtag function using bash comand line, something like this:

python ./TagGen QstartRefseqhg19.head 80

How can I do this, or some thing similar?

Thanks for your help!


You could use the argparse module for that.

From the docs,

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
           help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
           const=sum, default=max,
           help='sum the integers (default: find the max)')

args = parser.parse_args()
print args.accumulate(args.integers)

If you are using a version of python earlier than 2.7, you should use the optparse module, to achieve a similar effect.


sys.argv is the arguments list, with the first element being the script name. It's a list of strings, so if any of the parameters are numbers, you'll have to convert them using int() or float().

So, if you called a script like so:

$ python myscript.py 1 foo bar baz

sys.argv would be this:

["myscript.py", "1", "foo", "bar", "baz"]

In your case, you could make your script this:

import sys
import csv

def SJtag(file,len_tag):
    reader = csv.reader(open(file), dialect='excel-tab' )
    for row in reader:
        qstarts = row[1].split(",")[1:-1] 
        n = len_tag/2
        for i in qstarts:  
            name = row[0]

            start = int(i)-n
            if start<0:
                start = 0      

            end = int(i)+n
            if end>len(row[2]):
                end=len(row[2])

            tag = row[2][start:end]    
            print name, i, tag, len(tag)

if __name__ == '__main__':
    SJtag(sys.argv[1], int(sys.argv[2]))


I'd recommend argparse and a nice wrapper for it called plac. In your case all you'd need to do is:

if __name__ == '__main__':
    import plac; plac.call(SJtag)

...and placwill handle everything, as it is able to figure out the command-line arguments to use from the signature of your function. Very cool, check out the docs for more (it can do a lot more).


Just refer to this.

http://www.dalkescientific.com/writings/NBN/python_intro/command_line.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜