python’s standard library part i joe houpert cs265

12
Python’s Standard Python’s Standard Library Library Part I Part I Joe Houpert CS265

Upload: sheena-andrews

Post on 08-Jan-2018

239 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Python’s Standard Library Part I Joe Houpert CS265

Python’s Standard LibraryPython’s Standard LibraryPart IPart IJoe HoupertCS265

Page 2: Python’s Standard Library Part I Joe Houpert CS265

Operating System InterfaceOperating System InterfaceThe os module provides dozens of

functions for interacting with the operating system

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

Page 3: Python’s Standard Library Part I Joe Houpert CS265

File WildcardsFile WildcardsThe glob module provides a

function for making file lists from directory wildcard searches

Example: >>> import glob >>> glob.glob('*.py') ['primes.py', 'random.py',

'quote.py']

Page 4: Python’s Standard Library Part I Joe Houpert CS265

Command Line ArgumentsCommand Line ArgumentsStored in sys module’s argv

attribute as a listExample: python demo.py one two three >>> import sys >>> print sys.argv ['demo.py', 'one', 'two',

'three']

Page 5: Python’s Standard Library Part I Joe Houpert CS265

String Pattern MatchingString Pattern MatchingThe re module provides regular

expression tools for advanced string processing

Example: >>> import re >>> re.findall(r'\bf[a-z]*', 'which foot or hand

fell fastest') ['foot', 'fell', 'fastest']

Page 6: Python’s Standard Library Part I Joe Houpert CS265

MathematicsMathematics

The math module gives access to the underlying C library functions for floating point math

Example: >>> import math >>> math.cos(math.pi / 4.0) 0.70710678118654757 >>> math.log(1024, 2) 10.0

Random module.

Page 7: Python’s Standard Library Part I Joe Houpert CS265

Internet AccessInternet AccessThere are a number of modules for

accessing the internet and processing internet protocols.

Urllib2: for retrieving data from a url.Smtplib: used for sending mail.Example: >>> import urllib2 >>> for line in

urllib2.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl')

if 'EST' in line or 'EDT' in line: # look for Eastern Timeprint line

<BR>Nov. 25, 09:43:32 PM EST

Page 8: Python’s Standard Library Part I Joe Houpert CS265

Dates and TimesDates and TimesThe datetime module supplies classes

for manipulating dates and times in both simple and complex ways

Date and time arithmeticOutput formatting and manipulation >>> from datetime import date # dates support calendar

arithmetic >>> birthday = date(1964, 7, 31) >>> age = now - birthday >>> age.days 14368

Page 9: Python’s Standard Library Part I Joe Houpert CS265

Data CompressionData CompressionCommon data archiving and

compression formats are directly supported by modules including: zlib, gzip, bz2, zipfile and tarfile.

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

Page 10: Python’s Standard Library Part I Joe Houpert CS265

Performance Performance MeasurementMeasurementPython 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()

0.57535828626024577

>>> Timer('a,b = b,a', 'a=1; b=2').timeit() 0.54962537085770791

Page 11: Python’s Standard Library Part I Joe Houpert CS265

Quality ControlQuality ControlThe doctest module provides a tool for scanning

a module and validating tests embedded in a program’s docstrings

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

Page 12: Python’s Standard Library Part I Joe Houpert CS265

ReferencesReferenceshttp://docs.python.org/tutorial/

stdlib.htmlhttp://docs.python.org/library/

doctest.html#module-doctest