'filter.py', '-l', 'seqs.data', '-i', 'fasta', '-c', '-t', '-s', 'output.data', '

advertisement
Parsing command line arguments
Assume you want a command line based version of your project:
• Call it with different arguments indicating what files to operate on and
what tasks to perform. Something like
python filter.py –l seqs.data –i fasta –c –t –s
output.data –o gde
meaning
load sequences from file seqs.data, input format is fasta, perform
codon translation and trypsin cleaving, save result in file
output.data, writing sequences in gde format
2
Parsing command line arguments
• We need to parse sys.argv to get all the info:
sys.argv :
[ "filter.py", "–l", "seqs.data", "–i",
"fasta", "–c", "–t", "–s", "output.data",
"–o", "gde" ]
• Python has help: the getopt module
3
getopt method takes
"pure" argument list
and string of legal
options, returns
option/value list and
unused arguments
If an unknown option is given,
a GetoptError is raised
filter.py
Typical way of
collecting option
information
4
threonine:~...ExamplePrograms% python filter.py -l seqs.data -i fasta -c
-t -s output.data -o gde
Argv: ['filter.py', '-l', 'seqs.data', '-i', 'fasta', '-c', '-t', '-s',
'output.data', '-o', 'gde']
Options: [('-l', 'seqs.data'), ('-i', 'fasta'), ('-c', ''), ('-t', ''),
('-s', 'output.data'), ('-o', 'gde')]
Unused arguments: []
threonine:~...ExamplePrograms% python filter.py -l seqs.data -i fasta
bad_arg -c -t -s output.data -o gde
Argv: ['filter.py', '-l', 'seqs.data', '-i', 'fasta', 'bad_arg', '-c', 't', '-s', 'output.data', '-o', 'gde']
Options: [('-l', 'seqs.data'), ('-i', 'fasta')]
Unused arguments: ['bad_arg', '-c', '-t', '-s', 'output.data', '-o',
'gde']
5
Profiling Python programs
• Profiling: to get info of how the running time is spent
• Module profile
• Can be used like this:
import profile
< your original code >
def main():
< invoke code >
Point of entry – the point
from where the program
is started
profile.run( "main()" )
6
dirdraw_profiler.py
Much work;
done by
calling
other
functions
Put main ("starting")
code in point of entry
function
Call the run method of the profile
module with the point of entry
function call as argument
7
+---Solutions
+---------Exercises+
|
+-----Project
|
+------------Slides+------Images
---PBI+
+--------------Mail
|
+----------NoAccess
|
+---ExamplePrograms
get_name: 18 calls, 9 nodes..??
9 calls in assign_depth_r,
9 calls in tostring
5267 function calls (5251 primitive calls) in 0.180 CPU seconds
Ordered by: standard name
ncalls
1
18
1
18
9/1
1
9/1
1
18
1284
1284
18
1
0
1302
1284
18
tottime
0.000
0.000
0.000
0.030
0.000
0.000
0.000
0.000
0.000
0.100
0.010
0.000
0.010
0.000
0.010
0.020
0.000
percall
0.000
0.000
0.000
0.002
0.000
0.000
0.000
0.000
0.000
0.000
0.000
0.000
0.010
0.000
0.000
0.000
cumtime
0.170
0.000
0.170
0.170
0.110
0.110
0.060
0.170
0.000
0.130
0.010
0.000
0.180
0.000
0.010
0.030
0.000
percall
0.170
0.000
0.170
0.009
0.110
0.110
0.060
0.170
0.000
0.000
0.000
0.000
0.180
filename:lineno(function)
<string>:1(?)
dirdraw_profiler.py:23(get_name)
dirdraw_profiler.py:29(dirdraw)
dirdraw_profiler.py:9(get_sons)
drawtree_converter.py:11(assign_depth_r)
drawtree_converter.py:3(assign_depth)
drawtree_converter.py:35(tostring)
drawtree_converter.py:89(drawtree)
posixpath.py:159(islink)
posixpath.py:184(isdir)
posixpath.py:56(join)
posixpath.py:74(split)
profile:0(dirdraw())
profile:0(profiler)
0.000 stat.py:29(S_IFMT)
0.000 stat.py:45(S_ISDIR)
0.000 stat.py:60(S_ISLNK)
8
Profiling used to compare functions
Recall the two
different versions
of the gcd
function:
Which is more
efficient?
9
gcd_profiler.py
10
testing gcd_v1
1000003 function calls in 16.890 CPU seconds
Ordered by: standard name
ncalls tottime
1
0.000
1000000
9.270
1
7.580
0
0.000
1
0.040
gcd_v1, testset ))
percall
0.000
0.000
7.580
0.040
cumtime
16.850
9.270
16.850
0.000
16.890
percall
16.850
0.000
16.850
filename:lineno(function)
<string>:1(?)
gcd_function.py:3(gcd)
gcd_profiler.py:7(gcd_profile)
profile:0(profiler)
16.890 profile:0(solutions = gcd_profile(
testing gcd_v2
1000003 function calls in 18.560 CPU seconds
Ordered by: standard name
ncalls tottime
1
0.000
1000000
10.750
1
7.810
0
0.000
1
0.000
gcd_v2, testset ))
percall
0.000
0.000
7.810
0.000
cumtime
18.560
10.750
18.560
0.000
18.560
percall
18.560
0.000
18.560
filename:lineno(function)
<string>:1(?)
gcd_function2_test.py:3(gcd)
gcd_profiler.py:7(gcd_profile)
profile:0(profiler)
18.560 profile:0(solutions = gcd_profile(
First version slightly more efficient, second version shorter and more neat ;-)
11
Importing modules from strange places
If you need to import a module located in some place
where Python doesn't automically look, extend the
sys.path
path.py
Say you want to import a module from /users/chili/PBI
called invent_new_words:
12
['/users/chili/public_html/PBI/ExamplePrograms', '/users/chili/usr/MySQLpython-0.9.1', '/users/chili/BiRC/MolBio/Data',
'/users/chili/usr/lib/python', '/users/chili/usr/include/python',
'/users/chili/BiRC/MolBio/Data/PrimerDesign',
'/users/chili/PBI/ExamplePrograms', '/usr/local/lib/python23.zip',
'/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
'/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
'/usr/local/lib/python2.3/site-packages']
['/users/chili/public_html/PBI/ExamplePrograms', '/users/chili/usr/MySQLpython-0.9.1', '/users/chili/BiRC/MolBio/Data',
'/users/chili/usr/lib/python', '/users/chili/usr/include/python',
'/users/chili/BiRC/MolBio/Data/PrimerDesign',
'/users/chili/PBI/ExamplePrograms', '/usr/local/lib/python23.zip',
'/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
'/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
'/usr/local/lib/python2.3/site-packages', '/users/chili/PBI']
mangedoblesnobrød
slalomestjerneskruetrækker
slidsevertikal
påskrivehavkat
hobewagon
indtelefonerekobberstikteknik
hindrekludeklip
rekompensereålænding
prædisponeredirttrack
sjoflealternativ
(Program invents new words by
putting a random verb in front of a
random noun)
13
Download