Python Standard Library Overview
The Python Standard Library is vast, offering a wide range of components. By using the standard library, you can effortlessly accomplish various tasks.
Here are some of the modules in the Python 3 Standard Library:
1. os Module
The os module provides numerous functions for interacting with the operating system, such as creating, moving, and deleting files and directories, and accessing environment variables.
2. sys Module
The sys module offers functions related to the Python interpreter and system, including information about the interpreter's version and path, as well as handling stdin, stdout, and stderr.
3. time Module
The time module includes functions for working with time, such as getting the current time, formatting dates and times, and measuring intervals.
4. datetime Module
The datetime module offers more advanced time and date handling, such as working with time zones, calculating time differences, and manipulating dates.
5. random Module
The random module provides functions for generating random numbers, such as random integers, floats, and sequences.
6. math Module
The math module contains mathematical functions such as trigonometric functions, logarithms, exponentials, and constants.
7. re Module
The re module supplies regular expression handling, which can be used for searching, replacing, and splitting text.
8. json Module
The json module offers functions for encoding and decoding JSON. It allows you to convert Python objects to JSON format and parse JSON back into Python objects.
9. urllib Module
The urllib module provides functions for accessing web pages and handling URLs, including downloading files, sending POST requests, and handling cookies.
Operating System Interface
The os module provides various functions related to operating system interactions, such as file and directory operations.
Example:
import os
# Get the current working directory
current_dir = os.getcwd()
print("Current working directory:", current_dir)
# List the files in the directory
files = os.listdir(current_dir)
print("Files in the directory:", files)It’s recommended to use import os rather than from os import *, as this avoids overriding built-in functions like open() with platform-specific functions like os.open().
When working with large modules like os, built-in functions such as dir() and help() are very useful:
import os dir(os) # Returns a list of all module functions help(os) # Returns an extensive manual page from the module's docstrings
For higher-level file and directory management tasks, the shutil module offers an easy-to-use interface:
import shutil
shutil.copyfile('data.db', 'archive.db')
shutil.move('/build/executables', 'installdir')File Wildcards
The glob module provides a function to generate file lists from directory wildcard searches.
Example:
import glob
glob.glob('*.py')
# Output: ['primes.py', 'random.py', 'quote.py']Command-Line Arguments
Scripts often call for command-line arguments, which are stored in the sys module’s argv variable as a list. For example, running the command python demo.py one two three would yield the following output:
Example:
import sys print(sys.argv) # Output: ['demo.py', 'one', 'two', 'three']
Error Output Redirection and Program Termination
The sys module also provides access to stdin, stdout, and stderr. The stderr stream can be used to display warnings and error messages, even when stdout is redirected.
Example:
sys.stderr.write('Warning, log file not found, starting a new one\n')
# Output: Warning, log file not found, starting a new oneFor most scripts, program termination is handled using sys.exit().
String Regular Expressions
The re module provides powerful tools for advanced string processing using regular expressions. For complex matching and manipulation, regular expressions offer a concise and optimized solution.
Example:
import re re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest') # Output: ['foot', 'fell', 'fastest'] re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat') # Output: 'cat in the hat'
If you only need simple functionality, it’s better to use string methods, as they are straightforward and easier to read and debug.
Example:
'tea for too'.replace('too', 'two')
# Output: 'tea for two'Mathematics in Python
The math module provides access to the underlying C library functions for floating-point arithmetic.
Example:
import math print(math.cos(math.pi / 4)) # Output: 0.70710678118654757 print(math.log(1024, 2)) # Output: 10.0
Random Number Generation
The random module offers tools for generating random numbers.
Example:
import random print(random.choice(['apple', 'pear', 'banana'])) # Output: 'apple' print(random.sample(range(100), 10)) # Sampling without replacement # Output: [30, 83, 16, 4, 8, 81, 41, 50, 18, 33] print(random.random()) # Random float # Output: 0.17970987693706186 print(random.randrange(6)) # Random integer chosen from range(6) # Output: 4
Accessing the Internet
Python offers several modules to access the internet and handle network protocols. Two of the simplest modules are urllib.request for receiving data from URLs and smtplib for sending emails.
Example:
from urllib.request import urlopen
for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
line = line.decode('utf-8') # Decode binary data to text
if 'EST' in line or 'EDT' in line: # Look for Eastern Time
print(line)
# Output: <BR>Nov. 25, 09:43:32 PM ESTTo send an email:
import smtplib
server = smtplib.SMTP('localhost')
server.sendmail(
'soothsayer@example.org', 'jcaesar@example.org',
"""To: jcaesar@example.org
From: soothsayer@example.org
Beware the Ides of March.
""")
server.quit()Note: The second example requires a local mail server.
Dates and Times
The datetime module provides simple and complex ways to handle date and time. It focuses on efficient processing and formatting of output.
Example:
import datetime
# Get current date and time
current_datetime = datetime.datetime.now()
print(current_datetime)
# Get current date
current_date = datetime.date.today()
print(current_date)
# Format date and time
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_datetime) # Output: 2023-07-17 15:30:45The module also supports time zone handling:
from datetime import date
now = date.today() # Current date
print(now) # Output: datetime.date(2023, 7, 17)
# Formatting
print(now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B."))
# Output: '07-17-23. 17 Jul 2023 is a Monday on the 17 day of July.'
# Calculate time difference
birthday = date(1964, 7, 31)
age = now - birthday
print(age.days) # Output: 21535Data Compression
Python directly supports common data compression formats with modules like zlib, gzip, bz2, zipfile, and tarfile.
Example:
import zlib s = b'witch which has which witches wrist watch' print(len(s)) # Output: 41 t = zlib.compress(s) print(len(t)) # Output: 37 print(zlib.decompress(t)) # Output: b'witch which has which witches wrist watch' print(zlib.crc32(s)) # Output: 226805979
Performance Measurement
Some users are interested in measuring the performance of different methods to solve the same problem. Python offers the timeit module to provide direct answers to performance-related questions.
Example:
from timeit import Timer
# Measure time for tuple swapping
print(Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()) # Output: 0.57535828626024577
# Measure time for modern swapping method
print(Timer('a,b = b,a', 'a=1; b=2').timeit()) # Output: 0.54962537085770791For more in-depth performance analysis of larger code blocks, profile and pstats modules provide a more detailed measurement tool.
Testing Modules
One approach to develop high-quality software is to create test cases for each function and run tests frequently during development.
The doctest module scans the module and runs tests based on the embedded docstrings.
Example:
def average(values): """Computes the arithmetic mean of a list of numbers. >>> print(average([20, 30, 70])) 40.0 """ return sum(values) / len(values) import doctest doctest.testmod() # Automatically validates embedded tests
The unittest module provides a more comprehensive test suite, though it's not as easy to use as doctest.
Example:
import unittest class TestStatisticalFunctions(unittest.TestCase): def test_average(self): self.assertEqual(average([20, 30, 70]), 40.0) self.assertEqual(round(average([1, 5, 7]), 1), 4.3) self.assertRaises(ZeroDivisionError, average, []) self.assertRaises(TypeError, average, 20, 30, 70) unittest.main() # Calling from the command line invokes all tests
This is just a glimpse of the Python 3 Standard Library. You can explore more modules in the official documentation: Python Standard Library Documentation.