CS Programming Languages: Python

Similar documents
Programming in Python Advanced

Introduction to python

What we already know. more of what we know. results, searching for "This" 6/21/2017. chapter 14

Exceptions & a Taste of Declarative Programming in SQL

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

LECTURE 4 Python Basics Part 3

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

CIS192 Python Programming

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Outline. the try-except statement the try-finally statement. exceptions are classes raising exceptions defining exceptions

Part IV. More on Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

CIS192 Python Programming

CIS192 Python Programming

CIS192 Python Programming

COMS Programming Languages Python: Lecture 4. Kangkook Jee

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Lecture #12: Quick: Exceptions and SQL

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python File Modes. Mode Description. Open a file for reading. (default)

Table of Contents EVALUATION COPY

Introduction to: Computers & Programming: Exception Handling

An Introduction to Python

FILE HANDLING AND EXCEPTIONS

PTN-102 Python programming

The WSGI Reference Library

Python INTRODUCTION: Understanding the Open source Installation of python in Linux/windows. Understanding Interpreters * ipython.

Modules and scoping rules

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Python 3 Quick Reference Card

Class extension and. Exception handling. Genome 559

Files. Files need to be opened in Python before they can be read from or written into Files are opened in Python using the open() built-in function

PYTHON TRAINING COURSE CONTENT

Exceptions & error handling in Python 2 and Python 3

Exception Handling. Genome 559

MEIN 50010: Python Flow Control

STSCI Python Introduction. Class URL

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Cross-platform daemonization tools.

Chapter 9: Dealing with Errors

Class extension and. Exception handling. Genome 559

CS 11 python track: lecture 2

Exceptions CS GMU

#11: File manipulation Reading: Chapter 7

LECTURE 9 The Standard Library Part 3

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

Textbook. Topic 8: Files and Exceptions. Files. Types of Files

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

CSc 120. Introduction to Computer Programming II. 07: Excep*ons. Adapted from slides by Dr. Saumya Debray

Python Tutorial. Day 2

At full speed with Python

OstrichLib Documentation

Programming in Python

Python source materials

CGI Architecture Diagram. Web browser takes response from web server and displays either the received file or error message.

Try and Error. Python debugging and beautification

Python. Karin Lagesen.

Abstract Data Types Chapter 1

Exceptions and File I/O

Embedding a Python interpreter into your program

What is an Exception? Exception Handling. What is an Exception? What is an Exception? test = [1,2,3] test[3]

Chapter 1 - Introduction. September 8, 2016

pygtrie Release Jul 03, 2017

databuild Documentation

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Idioms and Anti-Idioms in Python

stdin, stdout, stderr

This course is designed for anyone who needs to learn how to write programs in Python.

Control Structures 1 / 17

NLTK chapter 2, 4 (approximately) NLTK programming course Peter Ljunglöf

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

How to write a well-behaved Python command line application. PyCon AU 2012 Tutorial Graeme Cross

The Big Python Guide

Large-Scale Networks

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

A Little Python Part 2

Python. Tutorial Lecture for EE562 Artificial Intelligence for Engineers

Python for Astronomers. Errors and Exceptions

Introduction to Python! Lecture 2

Reading and writing files

ECE 364 Software Engineering Tools Lab. Lecture 8 Python: Advanced I

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

Introduction to Python

LECTURE 7. The Standard Library Part 1: Built-ins, time, sys, and os

Some material adapted from Upenn cmpe391 slides and other sources

Senthil Kumaran S

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern

MEIN 50010: Python Strings

A Little Python Part 1. Introducing Programming with Python

Accelerating Information Technology Innovation

Programming to Python

TH IRD EDITION. Python Cookbook. David Beazley and Brian K. Jones. O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Tokyo

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Exception Handling and Debugging

Python. Executive Summary

Transcription:

CS 3101-1 - Programming Languages: Python Lecture 5: Exceptions / Daniel Bauer (bauer@cs.columbia.edu) October 08 2014 Daniel Bauer CS3101-1 Python - 05 - Exceptions / 1/35

Contents Exceptions Daniel Bauer CS3101-1 Python - 05 - Exceptions / 2/35

Exceptions Daniel Bauer CS3101-1 Python - 05 - Exceptions / 3/35

Programs are Error Prone 1. Syntax errors. 2. Incorrect program behavior (wrong result). 3. Errors at runtime Name errors (undefined variables). Type errors (operation not supported by type). Numeric Errors (division by 0). IO errors (file not found, cannot write to file...).... Daniel Bauer CS3101-1 Python - 05 - Exceptions / 4/35

Programs are Error Prone 1. Syntax errors. Detected by interpreter 2. Incorrect program behavior (wrong result). 3. Errors at runtime Name errors (undefined variables). Type errors (operation not supported by type). Numeric Errors (division by 0). IO errors (file not found, cannot write to file...).... Daniel Bauer CS3101-1 Python - 05 - Exceptions / 4/35

Programs are Error Prone 1. Syntax errors. Detected by interpreter 2. Incorrect program behavior (wrong result). Testing (later) 3. Errors at runtime Name errors (undefined variables). Type errors (operation not supported by type). Numeric Errors (division by 0). IO errors (file not found, cannot write to file...).... Daniel Bauer CS3101-1 Python - 05 - Exceptions / 4/35

Programs are Error Prone 1. Syntax errors. Detected by interpreter 2. Incorrect program behavior (wrong result). Testing (later) 3. Errors at runtime Exception Handling Name errors (undefined variables). Type errors (operation not supported by type). Numeric Errors (division by 0). IO errors (file not found, cannot write to file...).... Daniel Bauer CS3101-1 Python - 05 - Exceptions / 4/35

Exceptions Exception = Message object that indicates an error or anomalous condition. When an error is detected, Python raises an exception. Exception is propagated through the call hierarchy. Exception can be handled by the program. If the exception is not handled and arrives at the top-level: Program terminates. Error message and traceback report is printed. Daniel Bauer CS3101-1 Python - 05 - Exceptions / 5/35

Example Error and Traceback Traceback contains the path the exception took through the call hierarchy Includes module names, function names, line numbers. error test.py def foo (a): x = bar (a) print ( done. ) return (x) def bar (b): return b [0] / b [1] >>> foo (42) Traceback ( most recent call last ): File " <stdin >", line 1, in <module > File " error_test. py", line 2, in foo x = bar (a) File " error_test. py", line 7, in bar return b [0] / b [1] TypeError : int object is not subscriptable Daniel Bauer CS3101-1 Python - 05 - Exceptions / 6/35

try... except statements (1) If an error occurs in the block indented below try: Execution is interrupted at the point of error. Optional except block is executed if exception has the right type (exception captured in local variable). Execution is resumed below the try... except block. def foo (a): try : x = bar (a) except TypeError, ex: print ( caught error. ) print (ex) x = None print ( done. ) return (x) >>> foo ([4,2]) done. 2 >>> foo (42) caught error. int object is not subscriptable done. def bar (b): return b [0] / b [1] Daniel Bauer CS3101-1 Python - 05 - Exceptions / 7/35

try... except statements (2) Can use multiple except blocks for different types: try : x = bar (a) except TypeError : # Binding the exception # object is optional print ( caught Type error. ) except ZeroDivisionError, ex: print ( caught div0 error. ) Can use tuple of exception types. try : x = bar (a) except ( TypeError, ZeroDivisionError ): print ( caught either a type or a div0 error. ) No exception type: catch all exceptions (use sparingly!). try : x = bar (a) except : print ( caught some exception. ) Daniel Bauer CS3101-1 Python - 05 - Exceptions / 8/35

try... except... else Optional else block is run only if try block terminates normally. Avoids unintentionally handling exceptions that occur in the else block. try : x = bar (a) except ZeroDivisionError : print ( caught a div0 error from bar. ) else : try : y = 72 / x # Can cause a different # div0 error! except ZeroDivisionError : print ( caught another div0 error. ) Daniel Bauer CS3101-1 Python - 05 - Exceptions / 9/35

try... except... finally finally block is executed no matter what! When the try block terminates normally. When an exception is caught. Even if break, return, continue is called or another exception is raised. def foo (x): try : y = x [0] return y except IndexError : return 0 finally : print (" Done.") >>> foo ([]) Done 0 >>> foo ([42]) Done. 42 >>> foo (42) Done.... TypeError : int object is not subscriptable Daniel Bauer CS3101-1 Python - 05 - Exceptions / 10/35

Raising Exceptions Exceptions can be raised if internal errors occur. Exceptions can be initiated explicitly with raise. First expression: Exception class Second expression: passed to exception class init. x = raw_ input () if x == " yes ": foo () elif x == "no": bar () else : raise ValueError, \ " Expected either yes or no." Daniel Bauer CS3101-1 Python - 05 - Exceptions / 11/35

Passing on Exceptions Can pass on Exceptions through the call hierarchy after partially handling them. def foo (x): try : y = x [0] return y except IndexError : print (" Foo : index 0 did not exist.") print (" Let someone else deal with it.") raise # Re - raise exception Daniel Bauer CS3101-1 Python - 05 - Exceptions / 12/35

Exceptions in the Iterator Protocol (review) iter (self) method that returns itself. next(self) method that returns the next element. if no element is left, calling next(self) raises a StopIteration exception. Python 3: next (self) class ReverseIterLst ( list ): def iter ( self ): self. index = len ( self ) return self def next ( self ): if self. index == 0: raise StopIteration else : self. index -= 1 return \ self [ self. index ] >>> l = \ ReverseIterLst ([1,2,3]) >>> for x in l:... print x... 3 2 1 Daniel Bauer CS3101-1 Python - 05 - Exceptions / 13/35

Built-in and Custom Exceptions List of built-in exceptions: http://docs.python.org/library/exceptions.html Can write our own exceptions (exceptions are classes): Can subclass any of the defined Exceptions (try to be as specific as possible). class EmptyListException ( IndexException ): """ An Exception that indicates that we found an empty list. """ def foo (x): try : y = x [0] return y except EmptyListException, ex: sys. stderr. write ( " Argument list cannot be empty.\ n") return None Daniel Bauer CS3101-1 Python - 05 - Exceptions / 14/35

Using Exceptions Properly Write exception handlers only if you know how to handle the exception (i.e. it s easy to back-off or the exception is normal behavior). Except specific exception classes, rather than Exception or StandardError (can mask unexpected errors). Raise informative exceptions rather then just terminating the program. Some recommend to use exceptions for control flow (I don t!): Easier to Ask for Forgiveness than for Permission (EAFP). x = { a :1, b :2, c :1} y = {} for a in x: try : y[x[a ]]. append (a) except KeyError : y[x[a]] = [a] Daniel Bauer CS3101-1 Python - 05 - Exceptions / 15/35

with Statement (1) Need to handle resources (e.g. files): acquire, use, free. Consider the problem of closing a file after using it, no matter what: # Acquire resource f = open ( trash. txt, w ) try : # Do something that can fail f. write ( Spam.\n ) finally : # Clean - up and free resource f. close () Can instead use: with open ( spam.txt, w ) as f: f. write ( Spam.\n ) file object provides functionality to set up and do clean-up. Daniel Bauer CS3101-1 Python - 05 - Exceptions / 16/35

with Statement (2) with works with any object implementing the context manager protocol. enter (self) is invoked when with statement is entered. exit (self) replaces finally block and is executed after the with block terminates (normally or with an exception). This with expression as name : statement Translates roughly to _tmp = expression name = _tmp. enter () try : statement finally : temporary. exit () Daniel Bauer CS3101-1 Python - 05 - Exceptions / 17/35

Exceptions Daniel Bauer CS3101-1 Python - 05 - Exceptions / 18/35

So far: structure of the programming language itself. Python comes with a batteries included philosophy. A lot of built-in functionality. Large standard library of modules. Will only cover some important / representative modules. See docs for more: http://docs.python.org/library/index.html Daniel Bauer CS3101-1 Python - 05 - Exceptions / 19/35

Some Important Modules (1) General Purpose: sys - Access runtime environment. collections More Container Datatypes itertools - Fancy iterators. functools - Functional programming tools. math - Mathematical functions. pickle - Save data to files. subprocess - Spawn child processes. Strings: re - Regular Expressions. codecs - Encoding/Decoding strings. File I/O: os - interact with the operating system. os.path - pathname operations / browse fs. gzip, bz2, zipfile, tarfile - compressed archives. csv - read/write comma separated value file. xml - XML utils Daniel Bauer CS3101-1 Python - 05 - Exceptions / 20/35

Some Important Modules (2) Internet / Networking: socket - low-level networking. urllib - Open resources by URL. cgi CGI scripts. smtplib / email - send e-mail. json - use JSON encoded data. GUI: TKinter - built-in GUI Debugging / Profiling: logger - built-in logging pdb - Python debugger trace - Trace statement execution. hotshot - Profiler Daniel Bauer CS3101-1 Python - 05 - Exceptions / 21/35

sys - System-specific parameters and functions sys System (i.e. interpreter)-specific parameters and functions. Daniel Bauer CS3101-1 Python - 05 - Exceptions / 22/35

sys - System-specific parameters and functions sys Module - IO Stream File Objects sys.stdin is the terminal input. sys.stdout is the terminal output. sys.stderr is the error stream. By default stderr is printed to terminal as well. In UNIX/Linux/Mac: can pipe different streams to files $ python error_test.py > stdout. out 2> stderr. log $ Daniel Bauer CS3101-1 Python - 05 - Exceptions / 23/35

sys - System-specific parameters and functions sys Module - path sys.path - a list of strings that determine where python searches for modules. Environment variable PYTHONPATH is appended to default path. >$ export PYTHONPATH =" $PYTHONPATH :/ Users / daniel / project /" >$ python Python 2.7.2 ( default, Jan 21 2012, 18: 42: 05) [ GCC 4.2.1] on darwin Type " help ", " copyright ", " credits " or " license " for more information. >>> import sys >>> sys. path [, / Library / Python /2.7/ site - packages, / Users / daniel / project / ] Daniel Bauer CS3101-1 Python - 05 - Exceptions / 24/35

sys - System-specific parameters and functions sys Module - Terminating Interpreter sys.exit([arg]) - terminate the interpreter immediately. arg is the error code: 0 - successful termination. 1 - termination with error. >>> import sys >>> sys. exit (0) daniel :$ _ Daniel Bauer CS3101-1 Python - 05 - Exceptions / 25/35

sys - System-specific parameters and functions sys Module - Command Line Arguments sys.argv is a list containing command line arguments. sys.argv[0] is the name of the script all other elements are arguments passed to the script. test args.py import sys print sys. argv daniel : $ python test_ args. py [ test_args.py, foo, bar ] Daniel Bauer CS3101-1 Python - 05 - Exceptions / 26/35

collections High-Performance Container Datatypes Collections High-Performance Container Datatypes Daniel Bauer CS3101-1 Python - 05 - Exceptions / 27/35

collections High-Performance Container Datatypes collections Module - defaultdict A dictionary class that automatically supplies default values for missing keys. Is initialized with a factory object, that creates the default values. Can be a function or a class object (calling a class instantiates it). Can be a basic type (list, set, dict, int initializes to 0). >>> from collections import defaultdict >>> x = defaultdict ( list ) >>> x[ yellow ]. append ( banana ) >>> x defaultdict (< type list >, { yellow : [ banana ]}) >>> x[ pink ] [] Daniel Bauer CS3101-1 Python - 05 - Exceptions / 28/35

collections High-Performance Container Datatypes collections Module - Counter Easy interface to count hashable objects in collections (often strings). Once created, they are dictionaries mapping each object to its count. Support method most common([n]) Can be updated with other counters or dictionaries. >>> from collections import Counter >>> x = Counter ( banana ) >>> x Counter ({ a : 3, n : 2, b : 1}) >>> x. most_ common (2) [( a, 3), ( n, 2)] >>> x. update ({ b :1}) >>> x[ b ] 2 Daniel Bauer CS3101-1 Python - 05 - Exceptions / 29/35

collections High-Performance Container Datatypes os Interacting with the operating system. os.path Filename Manipulation Daniel Bauer CS3101-1 Python - 05 - Exceptions / 30/35

os.path - Filename Manipulation os.path Module - manipulate pathnames os.path.abspath(path) - Returns the absolute pathname for a relative path. >>> os. path. abspath (" test ") / Users / daniel / cs3101 / lecture -3/ test os.path.join(path1, path2,... pathnames (system specific). ) - Concatenates >>> os. path. join (" Users "," daniel "," cs3101 ") Users / daniel / cs3101 os.path.basename(path) - Returns the basename of a path ( for dirs). >>> os. path. basename ("/ Users / daniel / test. txt ") test. txt os.path.isfile(path) - returns True if the path points to a file. os.path.isdir(path) - returns True if the path points to a directory. Daniel Bauer CS3101-1 Python - 05 - Exceptions / 31/35

os.path - Filename Manipulation os Module - list content of a directory os is a powerful module for interacting with the operating system. For homework: os.listdir(path) lists files in a directory. >>> os. listdir ("/ Users / daniel / listtest /") [ test1. txt, test2. txt ] Daniel Bauer CS3101-1 Python - 05 - Exceptions / 32/35

os.path - Filename Manipulation Pickle Object serialization / Data persistence Daniel Bauer CS3101-1 Python - 05 - Exceptions / 33/35

os.path - Filename Manipulation Pickle Module - Object serialization Provides a convenient way to store Python objects in files and reload them. Alows saving/reloading program data or transferring them over a network. Can pickle almost everything: All standard data types User defined functions, classes and instances. Works on complete object hierarchies. Classes and functions need to be defined when un-pickling. with open ( pickled_foo. pickle, w ) as f: pickle. dump (foo, f) with open ( pickled_foo. pickle, r ) as f: foo = pickle. load (f) Daniel Bauer CS3101-1 Python - 05 - Exceptions / 34/35

os.path - Filename Manipulation Pickle Module - Protocols and cpickle Normally pickle uses a plaintext ASCII protocol. Newer protocols available. 0 - ASCII protocol 1 - old binary format (backward compatible) 2 - new protocol ( python 2.3, more efficient) More efficient reimplementation of Pickle in C. Always use this for large object hierarchies (up to 1000x faster). import cpickle with open ( pickled_foo. pickle, wb ) as f: cpickle. dump (foo, f, protocol = 2) Daniel Bauer CS3101-1 Python - 05 - Exceptions / 35/35