1 A note on the import statement >>> import mymodule mymodule evaluated >>> from mymodule import three_columns mymodule evaluated >>> print three_columns( 'x' ) x x x >>> from mymodule import matrix mymodule evaluated >>> print matrix( 'x' ) x x x Whole module's code evaluated for any kind of import Module "lives in its own namespace": its functions can see each other even if you can't
2 Profiling Python programs Profiling: to get info of how the running time is spent Module profile Can be used like this: import profile def main(): profile.run( "main()" ) Point of entry – the point from where the program is started
3 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 Much work; done by calling other functions
4 +---Solutions Exercises+ | Project | Slides Images ---PBI Mail | NoAccess | +---ExamplePrograms 5267 function calls (5251 primitive calls) in CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) :1(?) dirdraw_profiler.py:23(get_name) dirdraw_profiler.py:29(dirdraw) dirdraw_profiler.py:9(get_sons) 9/ drawtree_converter.py:11(assign_depth_r) drawtree_converter.py:3(assign_depth) 9/ 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) stat.py:29(S_IFMT) stat.py:45(S_ISDIR) stat.py:60(S_ISLNK) get_name : 18 calls, 9 nodes..?? 9 calls in assign_depth_r, 9 calls in tostring
5 Profiling used to compare functions Recall the two different versions of the gcd function: Which is more efficient?
6 gcd_profiler.py
7 testing gcd_v function calls in CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) :1(?) gcd_function.py:3(gcd) gcd_profiler.py:7(gcd_profile) profile:0(profiler) profile:0(solutions = gcd_profile( gcd_v1, testset )) testing gcd_v function calls in CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) :1(?) gcd_function2_test.py:3(gcd) gcd_profiler.py:7(gcd_profile) profile:0(profiler) profile:0(solutions = gcd_profile( gcd_v2, testset )) First version slightly more efficient, second version shorter and more neat ;-)
8 path.py 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 :
9 ['/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)
10 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
11 Parsing command line arguments As we've seen, command line arguments are retrieved via sys.argv list I.e., 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
12 filter.py Typical way of collecting option information If an unknown option is given, a GetoptError is raised getopt method takes "pure" argument list and string of legal options, returns option/value list and unused arguments
13 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']