120 likes | 214 Views
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
E N D
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
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
getopt method takes "pure" argument list and string of legal options, returns option/value list and unused arguments filter.py If an unknown option is given, a GetoptError is raised Typical way of collecting option information
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']
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 > profile.run( "main()" ) Point of entry – the point from where the program is started
Much work; done by calling other functions dirdraw_profiler.py 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
+---Solutions +---------Exercises+ | +-----Project | +------------Slides+------Images ---PBI+ +--------------Mail | +----------NoAccess | +---ExamplePrograms 5267 function calls (5251 primitive calls) in 0.180 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.170 0.170 <string>:1(?) 18 0.000 0.000 0.000 0.000 dirdraw_profiler.py:23(get_name) 1 0.000 0.000 0.170 0.170 dirdraw_profiler.py:29(dirdraw) 18 0.030 0.002 0.170 0.009 dirdraw_profiler.py:9(get_sons) 9/1 0.000 0.000 0.110 0.110 drawtree_converter.py:11(assign_depth_r) 1 0.000 0.000 0.110 0.110 drawtree_converter.py:3(assign_depth) 9/1 0.000 0.000 0.060 0.060 drawtree_converter.py:35(tostring) 1 0.000 0.000 0.170 0.170 drawtree_converter.py:89(drawtree) 18 0.000 0.000 0.000 0.000 posixpath.py:159(islink) 1284 0.100 0.000 0.130 0.000 posixpath.py:184(isdir) 1284 0.010 0.000 0.010 0.000 posixpath.py:56(join) 18 0.000 0.000 0.000 0.000 posixpath.py:74(split) 1 0.010 0.010 0.180 0.180 profile:0(dirdraw()) 0 0.000 0.000 profile:0(profiler) 1302 0.010 0.000 0.010 0.000 stat.py:29(S_IFMT) 1284 0.020 0.000 0.030 0.000 stat.py:45(S_ISDIR) 18 0.000 0.000 0.000 0.000 stat.py:60(S_ISLNK) get_name: 18 calls, 9 nodes..?? 9 calls in assign_depth_r, 9 calls in tostring
Profiling used to compare functions Recall the two different versions of the gcd function: 1 Which is more efficient? 2
testing gcd_v1 1000003 function calls in 16.890 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 16.850 16.850 <string>:1(?) 1000000 9.270 0.000 9.270 0.000 gcd_function.py:3(gcd) 1 7.580 7.580 16.850 16.850 gcd_profiler.py:7(gcd_profile) 0 0.000 0.000 profile:0(profiler) 1 0.040 0.040 16.890 16.890 profile:0(solutions = gcd_profile( gcd_v1, testset )) testing gcd_v2 1000003 function calls in 18.560 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 18.560 18.560 <string>:1(?) 1000000 10.750 0.000 10.750 0.000 gcd_function2_test.py:3(gcd) 1 7.810 7.810 18.560 18.560 gcd_profiler.py:7(gcd_profile) 0 0.000 0.000 profile:0(profiler) 1 0.000 0.000 18.560 18.560 profile:0(solutions = gcd_profile( gcd_v2, testset )) First version slightly more efficient, second version shorter and more neat ;-)
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 Say you want to import a module from /users/chili/PBI called invent_new_words: path.py
['/users/chili/public_html/PBI/ExamplePrograms', '/users/chili/usr/MySQL-python-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/MySQL-python-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)