1. Python常用模块深度解析
作为一名Python开发者,日常工作中我们经常需要处理各种系统操作、时间管理、随机数生成等任务。Python标准库提供了丰富的模块来简化这些操作,今天我将详细介绍几个最常用的模块及其核心功能。
1.1 时间处理模块:time与datetime
time模块是Python处理时间的底层模块,提供了各种时间相关的函数。最常用的功能包括:
import time # 获取当前时间戳(1970年1月1日以来的秒数) timestamp = time.time() # 将时间戳转换为本地时间结构体 local_time = time.localtime(timestamp) # 格式化时间输出 formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time)datetime模块在time模块基础上提供了更高级的日期时间处理功能:
from datetime import datetime, timedelta # 获取当前日期时间 now = datetime.now() # 时间加减操作 three_days_later = now + timedelta(days=3) two_hours_earlier = now - timedelta(hours=2) # 日期时间格式化 formatted = now.strftime("%Y年%m月%d日 %H时%M分")实际开发中,datetime模块通常比time模块更常用,因为它提供了更直观的日期时间操作接口。但time模块在处理时间戳和性能敏感场景时仍有其优势。
1.2 随机数生成:random模块
random模块提供了各种随机数生成功能:
import random # 生成0-1之间的随机浮点数 rand_float = random.random() # 生成指定范围内的随机整数 rand_int = random.randint(1, 100) # 从序列中随机选择元素 items = ['apple', 'banana', 'orange'] choice = random.choice(items) # 打乱序列顺序 random.shuffle(items)随机数在游戏开发、模拟测试、抽样等场景中非常有用。需要注意的是,random模块生成的随机数实际上是伪随机数,不适合用于加密等安全敏感场景。
1.3 系统交互:os和sys模块
os模块提供了丰富的操作系统交互功能:
import os # 获取当前工作目录 current_dir = os.getcwd() # 列出目录内容 files = os.listdir('.') # 创建/删除目录 os.makedirs('new_dir', exist_ok=True) os.rmdir('empty_dir') # 执行系统命令 os.system('ls -l')sys模块则主要用于与Python解释器交互:
import sys # 获取命令行参数 args = sys.argv # 修改Python模块搜索路径 sys.path.append('/custom/module/path') # 标准输入输出 sys.stdout.write('Hello\n') user_input = sys.stdin.readline()1.4 文件操作:shutil模块
shutil模块提供了高级文件操作功能:
import shutil # 复制文件 shutil.copy('source.txt', 'dest.txt') # 递归复制目录 shutil.copytree('src_dir', 'dst_dir') # 移动/重命名文件 shutil.move('old_name', 'new_name') # 删除目录树 shutil.rmtree('directory_to_remove')相比os模块的文件操作,shutil提供了更高级的接口,特别适合批量文件处理任务。
1.5 数据序列化:json和pickle模块
json模块用于JSON格式的序列化和反序列化:
import json data = {'name': 'Alice', 'age': 25, 'scores': [90, 85, 95]} # 序列化为JSON字符串 json_str = json.dumps(data) # 写入JSON文件 with open('data.json', 'w') as f: json.dump(data, f) # 从JSON字符串加载 loaded_data = json.loads(json_str)pickle模块则用于Python对象的序列化:
import pickle # 序列化对象 pickle_data = pickle.dumps(data) # 保存到文件 with open('data.pkl', 'wb') as f: pickle.dump(data, f) # 反序列化 loaded = pickle.loads(pickle_data)JSON更适合不同语言间的数据交换,而pickle可以保存更复杂的Python对象,但仅限于Python环境使用。
1.6 配置处理:configparser模块
configparser用于处理INI格式的配置文件:
import configparser config = configparser.ConfigParser() config.read('config.ini') # 读取配置 db_host = config.get('database', 'host') db_port = config.getint('database', 'port') # 修改配置 config.set('database', 'port', '3306') # 保存配置 with open('config.ini', 'w') as f: config.write(f)1.7 安全哈希:hashlib模块
hashlib提供了常见的哈希算法:
import hashlib # MD5哈希 md5 = hashlib.md5() md5.update(b'Hello World') print(md5.hexdigest()) # SHA256哈希 sha256 = hashlib.sha256() sha256.update(b'Hello World') print(sha256.hexdigest())哈希算法常用于密码存储、数据完整性校验等场景。
1.8 子进程管理:subprocess模块
subprocess模块用于创建和管理子进程:
import subprocess # 执行简单命令 subprocess.run(['ls', '-l']) # 获取命令输出 result = subprocess.run(['python', '--version'], capture_output=True, text=True) print(result.stdout) # 管道操作 p1 = subprocess.Popen(['cat', 'file.txt'], stdout=subprocess.PIPE) p2 = subprocess.Popen(['grep', 'keyword'], stdin=p1.stdout, stdout=subprocess.PIPE) output = p2.communicate()[0]1.9 日志记录:logging模块
logging模块提供了灵活的日志记录功能:
import logging # 基础配置 logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', filename='app.log' ) # 记录日志 logging.info('Application started') try: 1 / 0 except ZeroDivisionError: logging.error('Division by zero', exc_info=True)更高级的日志配置可以创建多个logger,添加不同的handler(文件、控制台等),设置日志轮转等。
1.10 正则表达式:re模块
re模块提供了正则表达式功能:
import re # 匹配模式 pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' text = "Contact us at info@example.com or support@company.org" # 查找所有匹配 emails = re.findall(pattern, text) # 替换文本 anonymized = re.sub(pattern, '[EMAIL]', text) # 分组提取 date_pattern = r'(\d{4})-(\d{2})-(\d{2})' match = re.search(date_pattern, 'Event on 2023-05-15') if match: year, month, day = match.groups()正则表达式是文本处理的强大工具,但复杂的正则表达式可能难以维护,需要权衡使用。
2. 模块使用中的常见问题与解决方案
2.1 时间处理中的时区问题
处理跨时区应用时,建议使用pytz库或Python 3.9+的zoneinfo模块:
from datetime import datetime import pytz utc = pytz.utc local_tz = pytz.timezone('Asia/Shanghai') utc_time = datetime.now(utc) local_time = utc_time.astimezone(local_tz)2.2 文件路径的跨平台兼容性
使用os.path或pathlib处理路径可确保跨平台兼容:
from pathlib import Path # 推荐方式 config_path = Path('config') / 'settings.ini' # 传统方式 import os.path config_path = os.path.join('config', 'settings.ini')2.3 大文件处理的内存优化
处理大文件时,应避免一次性读取全部内容:
# 不好的做法 with open('large_file.txt') as f: content = f.read() # 可能耗尽内存 # 推荐做法 with open('large_file.txt') as f: for line in f: # 逐行处理 process(line)2.4 子进程调用的安全性
调用外部命令时,应避免直接使用用户输入构造命令:
# 不安全 user_input = 'some; malicious; command' subprocess.run(f'ls {user_input}', shell=True) # 安全做法 subprocess.run(['ls', user_input]) # 作为单独参数传递2.5 日志配置的最佳实践
生产环境中推荐使用字典配置或文件配置logging:
import logging.config LOGGING_CONFIG = { 'version': 1, 'formatters': { 'detailed': { 'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s' } }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'level': 'INFO', }, 'file': { 'class': 'logging.FileHandler', 'filename': 'app.log', 'formatter': 'detailed', }, }, 'root': { 'level': 'DEBUG', 'handlers': ['console', 'file'] }, } logging.config.dictConfig(LOGGING_CONFIG)3. 模块组合使用的实际案例
3.1 自动化备份脚本
结合os、shutil、datetime等模块创建备份工具:
import os import shutil from datetime import datetime def backup_files(src_dir, dst_dir): if not os.path.exists(dst_dir): os.makedirs(dst_dir) timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') backup_name = f'backup_{timestamp}' backup_path = os.path.join(dst_dir, backup_name) try: shutil.copytree(src_dir, backup_path) print(f'Backup created at {backup_path}') return True except Exception as e: print(f'Backup failed: {e}') return False3.2 配置文件管理工具
使用configparser和json管理应用配置:
import configparser import json from pathlib import Path class ConfigManager: def __init__(self, ini_path, json_path): self.ini_path = Path(ini_path) self.json_path = Path(json_path) def load_ini(self): config = configparser.ConfigParser() config.read(self.ini_path) return config def save_ini(self, config): with open(self.ini_path, 'w') as f: config.write(f) def load_json(self): with open(self.json_path) as f: return json.load(f) def save_json(self, data): with open(self.json_path, 'w') as f: json.dump(data, f, indent=4)3.3 日志分析工具
结合re和collections分析日志文件:
import re from collections import Counter def analyze_logs(log_file): ip_pattern = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' error_pattern = r'ERROR|WARNING|CRITICAL' ip_counter = Counter() error_counter = Counter() with open(log_file) as f: for line in f: # 统计IP出现次数 ips = re.findall(ip_pattern, line) ip_counter.update(ips) # 统计错误类型 errors = re.findall(error_pattern, line) error_counter.update(errors) return { 'top_ips': ip_counter.most_common(5), 'error_types': dict(error_counter) }4. 性能优化与高级技巧
4.1 正则表达式预编译
频繁使用的正则表达式应该预编译:
import re # 预编译正则表达式 email_re = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b') # 复用编译后的模式 def extract_emails(text): return email_re.findall(text)4.2 使用生成器处理大数据
结合os.walk和生成器处理大量文件:
import os def find_files(directory, pattern=None): for root, dirs, files in os.walk(directory): for file in files: if pattern is None or file.endswith(pattern): yield os.path.join(root, file) # 使用示例 for py_file in find_files('/projects', '.py'): print(py_file)4.3 使用functools.lru_cache缓存函数结果
适合缓存计算密集型函数:
import functools import time @functools.lru_cache(maxsize=128) def expensive_computation(n): time.sleep(2) # 模拟耗时计算 return n * n # 第一次调用会耗时 print(expensive_computation(5)) # 约2秒 # 相同参数的后续调用会使用缓存 print(expensive_computation(5)) # 立即返回4.4 使用concurrent.futures并行处理
利用多核CPU加速处理:
import concurrent.futures import os def process_file(file_path): # 模拟文件处理 size = os.path.getsize(file_path) return file_path, size def batch_process_files(file_list): with concurrent.futures.ThreadPoolExecutor() as executor: results = executor.map(process_file, file_list) return list(results)5. 模块选择与替代方案
5.1 何时选择第三方库
虽然Python标准库很强大,但某些场景下第三方库可能更合适:
- 更复杂的日期时间处理:arrow或pendulum
- 高级文件操作:pathlib(Python 3.4+已内置)
- 更强大的配置管理:PyYAML(处理YAML)
- 更安全的密码哈希:passlib
5.2 标准库与第三方库的性能比较
某些标准库模块在性能上可能不如专门的第三方库:
- json:对于超大型JSON,orjson或ujson可能更快
- pickle:对于科学计算数据,numpy的save/load可能更高效
- re:对于复杂模式匹配,regex库提供更多功能
5.3 异步编程考虑
对于I/O密集型应用,考虑异步替代方案:
- subprocess → asyncio.create_subprocess_exec
- threading → asyncio
- 文件操作 → aiofiles
6. 测试与调试技巧
6.1 模拟时间进行测试
使用unittest.mock测试时间相关代码:
from datetime import datetime from unittest.mock import patch def is_weekend(): return datetime.today().weekday() >= 5 # 测试代码 with patch('datetime.datetime') as mock_datetime: mock_datetime.today.return_value = datetime(2023, 5, 6) # 星期六 assert is_weekend() is True mock_datetime.today.return_value = datetime(2023, 5, 8) # 星期一 assert is_weekend() is False6.2 测试随机行为
固定随机种子确保可重复的测试:
import random import unittest class TestRandom(unittest.TestCase): def setUp(self): random.seed(42) # 固定随机种子 def test_random_behavior(self): first = [random.randint(1, 100) for _ in range(5)] random.seed(42) # 重置相同种子 second = [random.randint(1, 100) for _ in range(5)] self.assertEqual(first, second)6.3 日志测试
检查代码是否生成了正确的日志:
import logging from io import StringIO import unittest class TestLogging(unittest.TestCase): def setUp(self): self.log_stream = StringIO() logging.basicConfig(stream=self.log_stream, level=logging.INFO) def test_error_logging(self): logger = logging.getLogger(__name__) logger.error('Test error') self.assertIn('Test error', self.log_stream.getvalue())7. 安全注意事项
7.1 pickle的安全风险
避免从不可信来源加载pickle数据:
import pickle # 不安全 - 可能执行任意代码 malicious_data = b"cos\nsystem\n(S'rm -rf /'\ntR." pickle.loads(malicious_data) # 危险!7.2 安全地处理文件路径
防止路径遍历攻击:
import os def safe_join(base_dir, user_input): # 规范化路径并检查是否仍在基础目录下 full_path = os.path.abspath(os.path.join(base_dir, user_input)) if not full_path.startswith(os.path.abspath(base_dir)): raise ValueError("Invalid path") return full_path7.3 密码哈希的最佳实践
使用适当的参数进行密码哈希:
import hashlib import os def hash_password(password): salt = os.urandom(32) # 随机盐值 key = hashlib.pbkdf2_hmac( 'sha256', password.encode('utf-8'), salt, 100000 # 迭代次数 ) return salt + key def verify_password(stored, password): salt = stored[:32] key = stored[32:] new_key = hashlib.pbkdf2_hmac( 'sha256', password.encode('utf-8'), salt, 100000 ) return new_key == key8. 模块的进阶用法
8.1 创建自定义日志过滤器
import logging class CriticalFilter(logging.Filter): def filter(self, record): return record.levelno == logging.CRITICAL logger = logging.getLogger(__name__) logger.addFilter(CriticalFilter())8.2 使用shelve持久化对象
import shelve with shelve.open('data.db') as db: db['key1'] = {'a': 1, 'b': 2} # 存储 value = db.get('key1') # 读取8.3 处理XML数据
import xml.etree.ElementTree as ET # 解析XML tree = ET.parse('data.xml') root = tree.getroot() # 查找元素 for child in root.findall('item'): print(child.get('id'), child.text) # 创建XML new_item = ET.Element('item', {'id': '4'}) new_item.text = 'New content' root.append(new_item) tree.write('updated.xml')8.4 处理YAML配置
import yaml # 读取YAML with open('config.yml') as f: config = yaml.safe_load(f) # 写入YAML config['new_setting'] = 'value' with open('config.yml', 'w') as f: yaml.dump(config, f)9. 跨平台开发注意事项
9.1 路径分隔符处理
import os # 不好的做法 - 硬编码路径分隔符 path = 'dir\\subdir\\file.txt' # Windows风格 # 好的做法 - 使用os.path.join path = os.path.join('dir', 'subdir', 'file.txt')9.2 行结束符处理
# 通用换行模式读取文本文件 with open('file.txt', 'r', newline='') as f: content = f.read() # 自动处理不同平台的换行符9.3 平台特定代码
import sys if sys.platform == 'win32': # Windows特定代码 temp_dir = os.environ['TEMP'] elif sys.platform == 'darwin': # MacOS特定代码 temp_dir = '/tmp' else: # Linux/Unix temp_dir = '/tmp'10. 模块的替代与扩展
10.1 更现代的路径处理:pathlib
from pathlib import Path # 创建路径对象 config_path = Path('config') / 'settings.ini' # 检查文件存在 if config_path.exists(): content = config_path.read_text() # 递归查找文件 for py_file in Path('src').rglob('*.py'): print(py_file)10.2 更强大的日期时间处理
from datetime import datetime, timezone from zoneinfo import ZoneInfo # Python 3.9+ # 时区感知的datetime dt = datetime.now(ZoneInfo('Asia/Shanghai')) utc_dt = dt.astimezone(timezone.utc)10.3 高级日志配置
import logging.config logging.config.dictConfig({ 'version': 1, 'formatters': { 'detailed': { 'format': '%(asctime)s %(name)-15s %(levelname)-8s %(message)s' } }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'level': 'INFO', 'formatter': 'detailed' } }, 'root': { 'level': 'DEBUG', 'handlers': ['console'] } })11. 实际项目中的模块组合应用
11.1 自动化测试框架
结合os、sys、unittest、logging等模块:
import os import sys import unittest import logging from datetime import datetime class TestRunner: def __init__(self, test_dir='tests'): self.test_dir = test_dir self.setup_logging() def setup_logging(self): log_file = f"test_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log" logging.basicConfig( filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s' ) def discover_and_run(self): loader = unittest.TestLoader() suite = loader.discover(self.test_dir) runner = unittest.TextTestRunner(verbosity=2) result = runner.run(suite) logging.info(f"Tests run: {result.testsRun}") logging.info(f"Failures: {len(result.failures)}") logging.info(f"Errors: {len(result.errors)}") return result.wasSuccessful() if __name__ == '__main__': runner = TestRunner() success = runner.discover_and_run() sys.exit(0 if success else 1)11.2 配置文件管理系统
结合configparser、json和argparse:
import configparser import json import argparse from pathlib import Path class ConfigManager: def __init__(self): self.parser = self.setup_arg_parser() self.args = self.parser.parse_args() self.config = self.load_config() def setup_arg_parser(self): parser = argparse.ArgumentParser() parser.add_argument('--env', choices=['dev', 'prod'], default='dev') return parser def load_config(self): base_dir = Path(__file__).parent config_path = base_dir / f"config_{self.args.env}.ini" config = configparser.ConfigParser() config.read(config_path) # 加载额外的JSON配置 json_path = base_dir / 'settings.json' if json_path.exists(): with open(json_path) as f: json_config = json.load(f) config['json'] = json_config return config def get(self, section, key, default=None): try: return self.config.get(section, key) except (configparser.NoSectionError, configparser.NoOptionError): return default if __name__ == '__main__': manager = ConfigManager() db_host = manager.get('database', 'host', 'localhost') print(f"Database host: {db_host}")11.3 日志分析工具
结合re、collections和datetime:
import re from collections import defaultdict, Counter from datetime import datetime class LogAnalyzer: def __init__(self, log_file): self.log_file = log_file self.ip_pattern = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') self.date_pattern = re.compile(r'\[(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2})') def analyze(self): ip_counter = Counter() error_counter = Counter() hourly_stats = defaultdict(int) with open(self.log_file) as f: for line in f: # IP统计 ips = self.ip_pattern.findall(line) ip_counter.update(ips) # 错误统计 if 'ERROR' in line: error_counter['ERROR'] += 1 elif 'WARNING' in line: error_counter['WARNING'] += 1 # 时间分析 date_match = self.date_pattern.search(line) if date_match: dt = datetime.strptime(date_match.group(1), '%d/%b/%Y:%H:%M:%S') hourly_stats[dt.hour] += 1 return { 'top_ips': ip_counter.most_common(5), 'errors': dict(error_counter), 'hourly_traffic': dict(hourly_stats) } if __name__ == '__main__': analyzer = LogAnalyzer('app.log') results = analyzer.analyze() print("Top IPs:", results['top_ips']) print("Errors:", results['errors']) print("Hourly Traffic:", results['hourly_traffic'])12. 性能监控与优化
12.1 使用timeit测量代码执行时间
import timeit code_to_test = """ def factorial(n): return 1 if n <= 1 else n * factorial(n-1) factorial(20) """ execution_time = timeit.timeit(code_to_test, number=1000) print(f"Average execution time: {execution_time/1000:.6f} seconds")12.2 使用cProfile分析性能瓶颈
import cProfile def process_data(): # 模拟数据处理 data = [i**2 for i in range(10000)] return sum(data) / len(data) profiler = cProfile.Profile() profiler.runcall(process_data) profiler.print_stats(sort='cumulative')12.3 内存使用分析
import tracemalloc def process_large_data(): tracemalloc.start() # 内存密集型操作 data = [bytearray(1024*1024) for _ in range(10)] # 10MB x 10 snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno') print("[ Top 10 memory usage ]") for stat in top_stats[:10]: print(stat) tracemalloc.stop() process_large_data()13. 错误处理与调试技巧
13.1 优雅地处理文件操作错误
import os import sys def safe_file_operation(file_path): try: with open(file_path) as f: content = f.read() return content except FileNotFoundError: print(f"Error: File {file_path} not found", file=sys.stderr) return None except PermissionError: print(f"Error: No permission to read {file_path}", file=sys.stderr) return None except Exception as e: print(f"Unexpected error: {e}", file=sys.stderr) return None13.2 调试子进程调用
import subprocess def run_command_safely(cmd): try: result = subprocess.run( cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) return result.stdout except subprocess.CalledProcessError as e: print(f"Command failed with exit code {e.returncode}") print(f"Error output:\n{e.stderr}") return None13.3 日志记录异常堆栈
import logging logging.basicConfig( level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s' ) def risky_operation(): try: 1 / 0 except Exception as e: logging.error("Operation failed", exc_info=True) raise try: risky_operation() except: pass # 异常已记录14. 模块的线程安全考虑
14.1 随机数生成的线程安全
import random import threading # 每个线程使用自己的Random实例 def threaded_random_operation(): local_random = random.Random() print(local_random.randint(1, 100)) threads = [] for _ in range(5): t = threading.Thread(target=threaded_random_operation) threads.append(t) t.start() for t in threads: t.join()14.2 日志记录的线程安全
import logging import threading logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(threadName)s - %(levelname)s - %(message)s' ) def worker(): logging.info("Thread started") # 工作代码 logging.info("Thread finished") threads = [] for i in range(3): t = threading.Thread(target=worker, name=f"Worker-{i}") threads.append(t) t.start() for t in threads: t.join()14.3 文件操作的线程安全
import threading from filelock import FileLock # 需要安装filelock库 lock = threading.Lock() def safe_write(file_path, content): with lock: with open(file_path, 'a') as f: f.write(content + '\n') # 或者使用文件锁 def safe_write_with_filelock(file_path, content): lock_path = file_path + '.lock' with FileLock(lock_path): with open(file_path, 'a') as f: f.write(content + '\n')15. 模块的未来发展与替代
15.1 time和datetime的未来
Python 3.9+引入了zoneinfo作为时区处理的标准方式:
from datetime import datetime from zoneinfo import ZoneInfo # 创建时区感知的datetime dt = datetime(2023, 5, 15, 12, 0, tzinfo=ZoneInfo("Asia/Shanghai")) print(dt) # 2023-05-15 12:00:00+08:00 # 转换为其他时区 utc_dt = dt.astimezone(ZoneInfo("UTC")) print(utc_dt) # 2023-05-15 04:00:00+00:0015.2 pathlib的普及
pathlib在Python 3.4+中成为标准库的一部分,提供了更面向对象的路径操作方式:
from pathlib import Path # 创建目录 config_dir = Path('config') config_dir.mkdir(exist_ok=True) # 配置文件路径 config_file = config_dir / 'settings.ini' # 写入配置 config_file.write_text('[DEFAULT]\nkey=value\n') # 递归查找文件 for py_file in Path('src').rglob('*.py'): print(py_file)15.3 结构化日志记录
新的日志记录趋势是结构化日志,可以使用python-json-logger等库:
import logging from pythonjsonlogger import jsonlogger # 设置JSON格式的日志 logger = logging.getLogger() handler = logging.StreamHandler() formatter = jsonlogger.JsonFormatter( '%(asctime)s %(levelname)s %(name)s %(message)s' ) handler.setFormatter(formatter) logger.addHandler(handler) # 记录结构化日志 logger.info("User logged in", extra={ 'user': 'alice', 'ip': '192.168.1.1', 'tags': ['auth', 'login'] })16. 模块的替代实现与扩展
16.1 更快的JSON处理
对于性能敏感的应用,可以考虑orjson(Rust实现):
import orjson # 需要安装orjson data = {'name': 'Alice', 'age': 25, 'tags': ['python', 'data']} # 序列化 json_bytes = orjson.dumps(data) # 反序列化 loaded = orjson.loads(json_bytes)16.2 高级配置文件格式
除了INI和JSON,还可以使用TOML(Python 3.11+内置):
import tomllib # Python 3.11+ # 或 import toml # 需要安装toml库 # 读取TOML with open('config.toml', 'rb') as f: config = tomllib.load(f) # 写入TOML (需要toml库) import toml with open('config.toml', 'w') as f: toml.dump(config, f)16.3 更强大的正则表达式
regex库提供了比标准re模块更多的功能:
import regex # 需要安装regex # 支持更复杂的Unicode属性匹配 pattern = regex.compile(r'\p{Script=Han}+') # 匹配中文字符 text = "中文Chinese混合文本" print(pattern.findall(text)) # ['中文']17. 模块在Web开发中的应用
17.1 日志记录中间件
import logging from datetime import datetime from time import time logger = logging.getLogger('web') logger.setLevel(logging.INFO) class LoggingMiddleware: def __init__(self, app): self.app = app def __call__(self, environ, start_response): start_time = time() def custom_start_response(status, headers): duration = int((time() - start_time) * 1000) # 毫秒 client_ip = environ.get('REMOTE_ADDR', 'unknown') method = environ['REQUEST_METHOD'] path = environ['PATH_INFO'] logger.info( f"{client_ip} - {method} {path} - {status} -