by ryan smith the standard library in python. python’s “batteries included” philosophy...

Click here to load reader

Upload: maryann-summers

Post on 29-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

The Standard Library In Python

By Ryan SmithThe Standard Library In Python1Pythons Batteries Included PhilosophyPythons standard library was designed to be able to handle as many situations as possible.Has a module for just about anythingCan do email, access the internet, work with csv and xml packages, and more.Chances are if you want to do it, Python has an easier way to get it done.The OS ModulePython contains a number of functions that can interact with the operating systemThe os module allows us to access themSome examples of os commands:os.getcwd() Returns the current working directoryos.chdir(/cs265/Lab1) Change current directoryos.system(mkdir Lab2) Runs from the command lineMust use import os to gain access to the commandsWildcardsThe glob module allows us to make file lists from wildcard searchesTo use the command, we must use import globExample code:import globglob.glob(*.py)Returns: [hello.py, world.py, duh.py]

Getting Command Line ArgumentsAccessing command line arguments can be done using the sys module.The argv attribute allows us to manipulate the arguments.For example, take the file tootsie.py:import sysprint(sys.argv)Running python tootsie.py one two three from the command line would produce:[tootsie.py, one, two, three]We can do more sophisticated processing with argparseError Output & RedirectionThe sys module contains the stderr attribute.We can use it to give warnings and error messages when stdout will not work.For example:sys.stderr.write(Dat not gonna work)To terminate a script, we can use sys.exit()Regular ExpressionsIn Python, the re module allows us to use regular expressionsFor example, the findall command:import rere.findall(r\bf[a-z]*, fee fi fo fum, you smell)This would return: [fee, fi, fo, fum]Command found all words that started with fCan also use string methods:tea for too.replace(too, two)Returns tea for twoThe math ModulePythons math module allows us to access a number of complicated operationsmath.cos(), math.log()Also, the random module allows us to create random numbers.random.choice() Chooses 1 value from an inputted listrandom.sample(range(100), 10) Picks a set of numbers from a specified rangerandom.random() Creates a random floatrandom.randrange(6) Chooses a random number from the inputted rangeAccessing the InternetWe can retrieve data and send emails using python.This requires the urllib.request module and the smtplib module respectively

Dates and TimesThe datetime module allows the manipulation of dates and times.For example:from datetime import datenow = date.today()nowThis would return the current dateWe can also format now with strftimenow.strftime(%m-%d-%y)Returns 5-30-2014Data CompressionWe can compress data in python using a multitude of modules: zlib, gzip, bz2, lzma, zipfile, and tarfileFor instance:import zlibs = bwitch which has which witches wrist watchlen(s) 41t = zlib.compress(s)len(t)37Performance MeasurementThe timeit module allows us to track the performance of a programFor instance:from timeit import TimerTimer(t=a; a=b; b=t, a=1; b=2).timeit()Returns .57535828626024577

Quality ControlThe doctest module allows us to test other modulesFor instance, doctest.testmod() allows us to automatically validate any embedded tests.Using the unittest module allows us to create a set of tests that we can keep in a separate file.SourcesPython Course resources #2, Part 10:https://docs.python.org/3/tutorial/stdlib.html