Python - Standard Libraries
urllib
module for working with URLs.The urllib
module in Python provides a collection of modules for working with URLs. It allows you to make HTTP requests, handle URL encoding and decoding, parse URLs, and perform other tasks related to web-based interactions. The urllib
module is an essential tool for accessing web resources, interacting with APIs, and working with URLs in a Python script.
Let's explore a simple example using the urllib.request
module to make an HTTP GET request and retrieve the content of a web page:
import urllib.request
# URL of the web page to retrieve
url = "https://www.example.com"
# Make an HTTP GET request and retrieve the content
response = urllib.request.urlopen(url)
web_page_content = response.read()
# Display the content of the web page
print(web_page_content.decode('utf-8'))
Output:
<!doctype html> <html> <head> </head> <body> </body> </html>
In this example, we use the urllib.request.urlopen()
function to make an HTTP GET request to the specified URL ("https://www.example.com"
). The response object contains the content of the web page, which we retrieve using the read()
method. Finally, we print the decoded content using the decode()
method with the 'utf-8' encoding.
The urllib
module also includes modules like urllib.parse
for URL parsing, urllib.error
for handling errors, and urllib.robotparser
for parsing robots.txt files. It is a versatile module for handling various aspects of web interactions in a Python script.
argparse
module?The argparse
module in Python is a powerful and flexible tool for parsing command-line arguments. It simplifies the process of creating user-friendly command-line interfaces for your scripts and allows you to define and validate various types of arguments, including flags, options, and positional arguments.
Let's explore a simple example using the argparse
module to create a script that accepts a filename as a command-line argument and prints its content:
import argparse
# Create an ArgumentParser object
parser = argparse.ArgumentParser(description='Read and print the content of a file.')
# Add a positional argument for the filename
parser.add_argument('filename', type=str, help='Name of the file to read')
# Parse the command-line arguments
args = parser.parse_args()
# Read and print the content of the specified file
try:
with open(args.filename, 'r') as file:
file_content = file.read()
print(f"Content of {args.filename}:\n{file_content}")
except FileNotFoundError:
print(f"Error: File '{args.filename}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
Command-line usage: python script.py filename.txt
Output (if filename.txt contains "Hello, World!"): Content of filename.txt: Hello, World!
In this example, we use the argparse.ArgumentParser
class to create a parser object. We then add a positional argument 'filename'
with a type of string and a help message. The parse_args()
method is called to parse the command-line arguments, and the specified filename is used to read and print the content of the file.
The argparse
module provides a comprehensive solution for handling command-line arguments, including support for default values, custom argument types, and more. It is a standard library module that significantly enhances the usability of command-line interfaces for Python scripts.
logging
module for creating log messages.The logging
module in Python is a built-in module that provides flexible logging of messages within a Python program. It allows developers to log messages at different levels of severity, such as DEBUG, INFO, WARNING, ERROR, and CRITICAL. The logging
module is essential for understanding the behavior of a program during its execution and diagnosing issues.
Let's explore a simple example using the logging
module to create log messages in a Python script:
import logging
# Configure the logging settings
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# Create a logger
logger = logging.getLogger(__name__)
def example_function():
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
# Call the example function
example_function()
Output:
2022-02-04 12:34:56,789 - DEBUG - This is a debug message 2022-02-04 12:34:56,789 - INFO - This is an info message 2022-02-04 12:34:56,789 - WARNING - This is a warning message 2022-02-04 12:34:56,789 - ERROR - This is an error message 2022-02-04 12:34:56,789 - CRITICAL - This is a critical message
In this example, we use the logging.basicConfig()
function to configure the logging settings. We set the logging level to DEBUG
, which includes messages of all severity levels. The format
parameter specifies the format of the log messages.
We then create a logger using logging.getLogger(__name__)
, where __name__
is typically set to the name of the current module. The example_function()
demonstrates logging messages at different levels using the logger.
The logging
module is highly customizable, allowing developers to redirect log messages to different outputs, filter messages based on severity, and configure logging for various modules within a larger application.
re
module for regular expressions in Python?The re
module in Python is a built-in module that provides support for regular expressions, allowing developers to perform pattern-matching operations on strings. Regular expressions are powerful tools for text manipulation, searching, and validation. The re
module facilitates the use of regular expressions in Python scripts.
Let's explore a simple example using the re
module to search for email addresses in a given text:
import re
# Sample text containing email addresses
text = "Contact us at support@example.com or info@company.com"
# Define a regular expression pattern for email addresses
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
# Search for email addresses in the text
matches = re.findall(email_pattern, text)
# Display the matched email addresses
for match in matches:
print(f"Found email address: {match}")
Output:
Found email address: support@example.com Found email address: info@company.com
In this example, we use the re.findall()
function to search for all occurrences of the specified email pattern in the given text. The email pattern is defined using a regular expression that matches typical email formats.
Regular expressions allow for complex pattern matching, including character classes, quantifiers, anchors, and more. The re
module provides functions such as re.search()
, re.match()
, and re.sub()
for various regex operations.
The re
module is widely used for tasks such as text parsing, data validation, and search operations. It is a powerful tool that enhances the capabilities of Python for working with strings and text data.