Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 By Ryan Smith The Standard Library In Python

2 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.

3 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

4 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’]

5 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

6 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()

7 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 fo fum, 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’

8 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

9 Accessing the Internet We can retrieve data and send emails using python. This requires the urllib.request module and the smtplib module respectively

10 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

11 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

12 Performance Measurement The timeit module 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

13 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.

14 Sources Python Course resources #2, Part 10: https://docs.python.org/3/tutorial/stdlib.html


Download ppt "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."

Similar presentations


Ads by Google