360 likes | 1.04k Views
The Standard Library In Python . By Ryan Smith. Python’s “Batteries Included” Philosophy. Python’s standard library was designed to be able to handle as many situations as possible. Has a module for just about anything
E N D
The Standard Library In Python By Ryan Smith
Python’s “Batteries Included” Philosophy • Python’s standard library was designed to be able to handle as many situations as possible. • Has a module for just about anything • Can 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 Module • Python contains a number of functions that can interact with the operating system • The os module allows us to access them • Some examples of os commands: • os.getcwd() – Returns the current working directory • os.chdir(‘/cs265/Lab1’)– Change current directory • os.system(‘mkdir Lab2’) – Runs from the command line • Must use import os to gain access to the commands
Wildcards • The glob module allows us to make file lists from wildcard searches • To use the command, we must use import glob • Example code: • import glob • glob.glob(‘*.py’) • Returns: [‘hello.py’, ‘world.py’, ‘duh.py’]
Getting Command Line Arguments • Accessing 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 sys • print(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 argparse
Error Output & Redirection • The 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 Expressions • In Python, the re module allows us to use regular expressions • For example, the findall command: • import re • re.findall(r’\bf[a-z]*’, ‘fee fi fofum, you smell’) • This would return: [‘fee’, ‘fi’, ‘fo’, ‘fum’] • Command found all words that started with f • Can also use string methods: • ‘tea for too’.replace(‘too’, ‘two’) • Returns ‘tea for two’
The math Module • Python’s math module allows us to access a number of complicated operations • math.cos(), math.log() • Also, the random module allows us to create random numbers. • random.choice() – Chooses 1 value from an inputted list • random.sample(range(100), 10) – Picks a set of numbers from a specified range • random.random() – Creates a random float • random.randrange(6) – Chooses a random number from the inputted range
Accessing the Internet • We can retrieve data and send emails using python. • This requires the urllib.request module and the smtplib module respectively
Dates and Times • The datetime module allows the manipulation of dates and times. • For example: • from datetime import date • now = date.today() • now • This would return the current date • We can also format now with strftime • now.strftime(“%m-%d-%y) • Returns 5-30-2014
Data Compression • We can compress data in python using a multitude of modules: zlib, gzip, bz2, lzma, zipfile, and tarfile • For instance: • import zlib • s = b’witch which has which witches wrist watch’ • len(s) • 41 • t = zlib.compress(s) • len(t) • 37
Performance Measurement • The timeitmodule allows us to track the performance of a program • For instance: • from timeit import Timer • Timer(‘t=a; a=b; b=t’, ‘a=1; b=2’).timeit() • Returns .57535828626024577
Quality Control • The doctest module allows us to test other modules • For 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.
Sources • Python Course resources #2, Part 10: • https://docs.python.org/3/tutorial/stdlib.html