Python’s Standard Library Part I Joe Houpert CS265.

Slides:



Advertisements
Similar presentations
Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Advertisements

PYTHON FRUITFUL FUNCTIONS CHAPTER 6 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Intro to Python Welcome to the Wonderful world of GIS programing!
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
10/1/2014BCHB Edwards Python Modules and Basic File Parsing BCHB Lecture 10.
Guide To UNIX Using Linux Third Edition
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 35 – Python Outline 35.1 Introduction First Python Program Python Keywords 35.2 Basic.
Selecting and Combining Tools F. Duveau 02/03/12 F. Duveau 02/03/12 Chapter 14.
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
By Zeng Sheng Liu. os - provides dozens of functions for interacting with the operating system >>> import os >>> os.system('time 0:02') 0 >>> os.getcwd()
 Name Space ◦ modname.funcname ◦ Main 의 module name: ‘__main__’ if __name__ == ‘__main__’:  Scopes.
Introduction to Python By Neil Cook Twitter: njcuk Slides/Notes:
By: Joshua O’Donoghue. Operating System Interface In order to interact with the operating system in python you will want to become familiar with the OS.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
By Ryan Smith The Standard Library In Python. Python’s “Batteries Included” Philosophy Python’s standard library was designed to be able to handle as.
Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects.
Python’s Standard Library - Part I Josh Lawrence.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
H3D API Training  Part 3.1: Python – Quick overview.
Introduction to Python September 26, /10/ Bioinformatics Languages Low-level, compiled languages: C, C++, Java… Pros: performance Cons:
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
Intro Python: Variables, Indexing, Numbers, Strings.
Libraries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Introduction to Information Security Python. Python motivation Python is to a Hacker what Matlab is to an engineer Lots of built-in modules Lots of 3.
1 CS 177 Week 11 Recitation Slides Writing out programs, Reading from the Internet and Using Modules.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Announcements Course evaluation Your opinion matters! Attendance grades Will be posted prior to the final Project 5 grades Will be posted prior to the.
Ganga 4 Basics - Tutorial Jakub T. Moscicki ARDA/LHCb Ganga Tutorial, November 2005.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Ganga 4 Basics - Tutorial Jakub T. Moscicki ARDA/LHCb Ganga Tutorial, September 2006.
3. COMPUTING WITH NUMBERS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
PC204 Lecture 5 Conrad Huang Genentech Hall, N453A
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
CS2021 Python Programming Week 3 Systems Programming PP-Part II.
Lecture 4 Python Basics Part 3.
CIT 590 Intro to Programming Files etc. Agenda Files Try catch except A module to read html off a remote website (only works sometimes)
Productive Laziness with Python Programmability in SPSS Albert-Jan Roskam, PhD. Statistics Netherlands (CBS) ASSESS – York UK - October.
By: Aradhya Malhotra.  To interact with the OS in python you will want to become familiar with the OS module  The command “import os” is used for this.
Python’s Standard Library Part II Dennis Tran. Output Formatting The repr module provides a version of repr() customized for abbreviated displays of large.
Introduction to Python for System Administrators Toshio Kuratomi May 2011.
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
Lesson 06: Functions Class Participation: Class Chat:
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012
CIRC Winter Boot Camp 2017 Baowei Liu
CS 330 Class 7 Comments on Exam Programming plan for today:
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
Python Modules and Basic File Parsing
Python Modules and Basic File Parsing
CSC1018F: Functional Programming
Python Useful Functions and Methods
LING 408/508: Computational Techniques for Linguists
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
Python’s Standard library part I
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Python Modules and Basic File Parsing
Main() { int fact; fact = Factorial(4); } main fact.
Class code for pythonroom.com cchsp2cs
More Basics of Python Common types of data we will work with
Using Modules.
Slides Courtesy: Prof. Paul Fodor, SBU
Presentation transcript:

Python’s Standard Library Part I Joe Houpert CS265

Operating System Interface The os module provides dozens of functions for interacting with the operating systemos Example: >>> import os >>> os.getcwd() # Return the current working directory 'C:\\Python26' >>> os.chdir('/server/accesslogs') # Change current working directory >>> os.system('mkdir today') # Run the command mkdir in the system shell

File Wildcards The glob module provides a function for making file lists from directory wildcard searchesglob Example: >>> import glob >>> glob.glob('*.py') ['primes.py', 'random.py', 'quote.py']

Command Line Arguments Stored in sys module’s argv attribute as a list Example: python demo.py one two three >>> import sys >>> print sys.argv ['demo.py', 'one', 'two', 'three']

String Pattern Matching The re module provides regular expression tools for advanced string processingre Example: >>> import re >>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest') ['foot', 'fell', 'fastest']

Mathematics The math module gives access to the underlying C library functions for floating point mathmath Example: >>> import math >>> math.cos(math.pi / 4.0) >>> math.log(1024, 2) 10.0 Random module.

Internet Access There are a number of modules for accessing the internet and processing internet protocols. Urllib2: for retrieving data from a url. Urllib2 Smtplib: used for sending mail. Smtplib Example: >>> import urllib2 >>> for line in urllib2.urlopen(' bin/timer.pl') if 'EST' in line or 'EDT' in line: # look for Eastern Time print line Nov. 25, 09:43:32 PM EST

Dates and Times The datetime module supplies classes for manipulating dates and times in both simple and complex waysdatetime Date and time arithmetic Output formatting and manipulation >>> from datetime import date # dates support calendar arithmetic >>> birthday = date(1964, 7, 31) >>> age = now - birthday >>> age.days 14368

Data Compression Common data archiving and compression formats are directly supported by modules including: zlib, gzip, bz2, zipfile and tarfile.zlibgzipbz2zipfiletarfile >>> import zlib >>> s = 'witch which has which witches wrist watch' >>> len(s) 41 >>> t = zlib.compress(s) >>> len(t) 37 >>> zlib.decompress(t) 'witch which has which witches wrist watch'

Performance Measurement Python provides a measurement tool that measures the relative performance of different approaches to the same problem For example, tuple packing versus traditional swap. >>> from timeit import Timer >>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit() >>> Timer('a,b = b,a', 'a=1; b=2').timeit()

Quality Control The doctest module provides a tool for scanning a module and validating tests embedded in a program’s docstringsdoctest def factorial(n): """Return the factorial of n, an exact integer >= 0. If the result is small enough to fit in an int, return an int. Else return a long. >>> [factorial(n) for n in range(6)] [1, 1, 2, 6, 24, 120] Example: $ python example.py -v Trying: factorial(5) Expecting: 120 ok

References l#module-doctest l#module-doctest