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

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handling – improves program clarity by removing.
1 A note on the import statement >>> import mymodule mymodule evaluated >>> from mymodule import three_columns mymodule evaluated >>> print three_columns(
1 Drawing phylogenies We've seen several tree data structures but we still can't draw a tree  In today's first exercise we write a drawtree module for.
1 Parsing XML sequence? We have i2xml filter (exercise) – we want xml2i also Don’t have to write XML parser, Python provides one Thus, algorithm: – Open.
1 Sequence formats >FOSB_MOUSE Protein fosB. 338 bp MFQAFPGDYDSGSRCSSSPSAESQYLSSVDSFGSPPTAAASQECAGLGEMPGSFVPTVTA ITTSQDLQWLVQPTLISSMAQSQGQPLASQPPAVDPYDMPGTSYSTPGLSAYSTGGASGS.
1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handler catches a raised/thrown exception try.
1 Drawing phylogenies We've seen several tree data structures but we still can't draw a tree  In the tree drawing exercise we write a drawtree function.
1 Extensible Markup Language: XML HTML: widely supported protocol for formatting data XML: widely supported protocol for describing data XML is quickly.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Python programs How can I run a program? Input and output.
Introduction to Python
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.
Sorting and Modules. Sorting Lists have a sort method >>> L1 = ["this", "is", "a", "list", "of", "words"] >>> print L1 ['this', 'is', 'a', 'list', 'of',
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
Built-in Data Structures in Python An Introduction.
Python Functions.
Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
NLTK & Python Day 8 LING Computational Linguistics Harry Howard Tulane University.
GE3M25: Computer Programming for Biologists Python, Class 5
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
CS2021 Python Programming Week 3 Systems Programming PP-Part II.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Multimedia Summer Camp
How to python source: Web_Dev fan on pinterest.
Python Lesson 12 Mr. Kalmes.
Computer Science 210 Computer Organization
(optional - but then again, all of these are optional)
(optional - but then again, all of these are optional)‏
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Python Lesson 12 Mr. Kalmes.
Functions.
Engineering Innovation Center
Learning to Program in Python
Python 17 Mr. Husch.
Topics Introduction to File Input and Output
CMSC201 Computer Science I for Majors Lecture 16 – Classes and Modules
Python 17 Mr. Husch.
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Python programming exercise
Python – a HowTo Peter Wad Sackett and Henrike Zschach.
Advanced Python Concepts: Exceptions
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Advanced Python Concepts: Exceptions
Lab 4: Introduction to Scripting
Topics Introduction to File Input and Output
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
def-ining a function A function as an execution control structure
Python Modules.
Presentation transcript:

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