Python Basics: Dictionaries, Sets, File Operations, Regular Expressions, Logging, Unit Testing, Profiling, and Third‑Party Libraries
This article introduces core Python concepts—including dictionaries and sets as key‑value and unique collections, file and directory manipulation with the os module, regular expression handling, logging setup, unit testing with unittest, performance profiling via cProfile, and using popular third‑party libraries such as requests and BeautifulSoup.
Dictionary (dict)
A dictionary stores key‑value pairs, making it ideal for data storage and retrieval.
# 创建字典:
person = {"name": "Alice", "age": 25, "city": "New York"}
print(person["name"]) # 输出: Alice
print(person.get("name")) # 输出: Alice
person["email"] = "[email protected]" # 添加新元素
person["age"] = 26 # 修改现有元素
del person["city"] # 删除元素
for key, value in person.items():
print(f"{key}: {value}")Set (set)
A set is an unordered collection of unique items, useful for deduplication and set operations.
# 创建集合:
numbers = {1, 2, 3, 4}
numbers.add(5) # 添加元素
numbers.remove(3) # 删除元素
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union = set1 | set2 # 并集
intersection = set1 & set2 # 交集
difference = set1 - set2 # 差集
symmetric_difference = set1 ^ set2 # 对称差集File and Directory Operations
Python’s os module provides functions for working with files and directories.
# 导入 os 模块:
import os
current_dir = os.getcwd()
print(current_dir)
os.chdir("/path/to/new/directory")
os.mkdir("/path/to/new/directory")
os.rmdir("/path/to/directory")
files = os.listdir("/path/to/directory")
print(files)
# 路径拼接
path = os.path.join("/home/user", "data", "file.txt")
print(path)
if os.path.exists("/path/to/file.txt"):
print("File exists")
else:
print("File does not exist")
size = os.path.getsize("/path/to/file.txt")
print(size)Regular Expressions
Regular expressions enable powerful pattern matching and text manipulation.
# 导入 re 模块
import re
pattern = r"\d+" # 匹配一个或多个数字
text = "The price is 100 dollars."
match = re.search(pattern, text)
if match:
print(match.group()) # 输出: 100
new_text = re.sub(r"\d+", "XXX", text)
print(new_text) # 输出: The price is XXX dollars.
matches = re.findall(r"\d+", text)
print(matches) # 输出: ['100']Logging
Logging helps track program execution and errors.
# 导入 logging 模块
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug("This is a debug message")
logging.info("This is an info message")
logging.warning("This is a warning message")
logging.error("This is an error message")
logging.critical("This is a critical message")
# 输出到文件
logging.basicConfig(filename='app.log', level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')Unit Testing
Python’s unittest module simplifies writing and running tests.
# 导入 unittest 模块
import unittest
class TestAddition(unittest.TestCase):
def test_add(self):
self.assertEqual(add(1, 2), 3)
def test_add_negative(self):
self.assertEqual(add(-1, -2), -3)
if __name__ == '__main__':
unittest.main()
def add(a, b):
return a + bPerformance Profiling
Profiling identifies bottlenecks; cProfile is the standard tool.
# 导入 cProfile 模块
import cProfile
def example_function():
# Your code here
pass
cProfile.run('example_function()')Third‑Party Libraries
Python’s ecosystem offers many libraries to extend functionality.
# 使用 pip 安装库:
pip install requests
pip install beautifulsoup4
# 使用 requests 发送 HTTP 请求
import requests
response = requests.get("https://api.example.com/data")
print(response.status_code)
print(response.json())
# 使用 beautifulsoup4 解析 HTML
from bs4 import BeautifulSoup
import requests
response = requests.get("https://www.example.com")
soup = BeautifulSoup(response.text, 'html.parser')
print(soup.title.string)Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.