1 / 12

Python’s Standard Library Part I

Explore Python's standard library modules for operating system interaction, file handling, internet access, and more. Learn to manipulate files, work with dates, compress data, and measure performance efficiently.

gloriajones
Download Presentation

Python’s Standard Library Part I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python’s Standard LibraryPart I Joe Houpert CS265

  2. Operating System Interface • The 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

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

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

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

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

  7. Internet Access • There 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: >>> importurllib2 >>> 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 Time print line <BR>Nov. 25, 09:43:32 PM EST

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

  9. Data Compression • Common data archiving and compression formats are directly supported by modules including: zlib, gzip, bz2, zipfile and tarfile. • >>> importzlib • >>> 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'

  10. 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. >>> fromtimeitimport 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

  11. Quality Control • The doctest module provides a tool for scanning a module and validating tests embedded in a program’s docstrings deffactorial(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

  12. References • http://docs.python.org/tutorial/stdlib.html • http://docs.python.org/library/doctest.html#module-doctest

More Related