Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 A note on the import statement >>> import mymodule mymodule evaluated >>> from mymodule import three_columns mymodule evaluated >>> print three_columns(

Similar presentations


Presentation on theme: "1 A note on the import statement >>> import mymodule mymodule evaluated >>> from mymodule import three_columns mymodule evaluated >>> print three_columns("— Presentation transcript:

1 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 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 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 4 +---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 :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

5 5 Profiling used to compare functions Recall the two different versions of the gcd function: Which is more efficient?

6 6 gcd_profiler.py

7 7 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 :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 :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 ;-)

8 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 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 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 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 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 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']


Download ppt "1 A note on the import statement >>> import mymodule mymodule evaluated >>> from mymodule import three_columns mymodule evaluated >>> print three_columns("

Similar presentations


Ads by Google