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.
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Introduction To MATLAB Programming
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 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.
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.
Linux & Shell Scripting Small Group Lecture 4 How to Learn to Code Workshop group/ Erin.
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.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
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.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python Functions.
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
Python Let’s get started!.
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.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
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.
Introduction to java (class and object). Programming languages: –Easier to understand than CPU instructions –Needs to be translated for the CPU to understand.
Multimedia Summer Camp
How to python source: Web_Dev fan on pinterest.
Python’s Modules Noah Black.
Python Lesson 12 Mr. Kalmes.
(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.
CMSC201 Computer Science I for Majors Lecture 16 – Classes and Modules
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Python 17 Mr. Husch.
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
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
Basic Python Review BCHB524 Lecture 8 BCHB524 - Edwards.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Lab 4: Introduction to Scripting
Beginning Python Programming
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
Python Modules.
Getting Started in Python
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 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

3 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

4 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

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

6 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

7 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

8 +---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

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

10 gcd_profiler.py

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

12 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 :

13 ['/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)