七、异常处理架构
7.1 异常体系
# Python异常层次结构
"""
BaseException
├── SystemExit
├── KeyboardInterrupt
├── GeneratorExit
└── Exception
├── StopIteration
├── StopAsyncIteration
├── ArithmeticError
├── AssertionError
├── AttributeError
├── BufferError
├── EOFError
├── ImportError
├── LookupError
├── MemoryError
├── NameError
├── OSError
├── ReferenceError
├── RuntimeError
├── SyntaxError
├── SystemError
├── TypeError
└── ValueError
"""
# 自定义异常
class ValidationError(Exception):
"""验证错误"""
pass
class BusinessError(Exception):
"""业务错误"""
def __init__(self, code, message):
self.code = code
self.message = message
super().__init__(f"[{code}] {message}")
class DatabaseError(Exception):
"""数据库错误"""
pass
# 使用自定义异常
def validate_user_age(age):
if age < 0:
raise ValidationError("年龄不能为负数")
if age > 150:
raise ValidationError("年龄不能超过150")
def process_payment(amount, balance):
if amount > balance:
raise BusinessError(1001, "余额不足")
return balance - amount
# 异常链
def read_config():
try:
with open('config.json', 'r') as f:
return f.read()
except FileNotFoundError as e:
raise RuntimeError("配置文件加载失败") from e
# 上下文管理器中的异常处理
class Resource:
def __enter__(self):
print("获取资源")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("释放资源")
if exc_type:
print(f"处理异常: {exc_val}")
return False # 不抑制异常
def use_resource():
with Resource() as res:
raise ValueError("发生错误")
7.2 上下文管理器
from contextlib import contextmanager
import time
# 使用类实现上下文管理器
class Timer:
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
print(f"耗时: {self.end - self.start:.4f}秒")
# 使用装饰器实现上下文管理器
@contextmanager
def timer_context():
start = time.time()
try:
yield
finally:
end = time.time()
print(f"耗时: {end - start:.4f}秒")
# 支持异常的上下文管理器
class Transaction:
def __init__(self, connection):
self.conn = connection
def __enter__(self):
self.conn.begin()
return self.conn
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type:
self.conn.rollback()
print("事务回滚")
else:
self.conn.commit()
print("事务提交")
# 多个上下文管理器
def multiple_contexts():
with open('file1.txt', 'w') as f1, open('file2.txt', 'w') as f2:
f1.write("Hello")
f2.write("World")
# 上下文管理器用于资源清理
@contextmanager
def managed_resource(*args, **kwargs):
resource = acquire_resource(*args, **kwargs)
try:
yield resource
finally:
release_resource(resource)
# 示例:数据库连接上下文
class DatabaseConnection:
def __init__(self, connection_string):
self.conn_string = connection_string
self.connection = None
def __enter__(self):
print(f"连接数据库: {self.conn_string}")
self.connection = f"Connection_{id(self)}"
return self.connection
def __exit__(self, exc_type, exc_val, exc_tb):
print(f"关闭数据库连接: {self.connection}")
self.connection = None
# 使用示例
with DatabaseConnection("mysql://localhost:3306/mydb") as conn:
print(f"使用连接: {conn}")
# 执行数据库操作
八、设计模式在Python中的实现
8.1 创建型模式
# 1. 单例模式
class Singleton:
_instance = None
_lock = threading.Lock()
def __new__(cls):
if cls._instance is None:
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
self.value = 0
# 使用装饰器实现单例
def singleton(cls):
instances = {}
def get_instance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return get_instance
@singleton
class Database:
def __init__(self):
self.connected = False
def connect(self):
self.connected = True
# 2. 工厂模式
class Product:
def operation(self):
pass
class ConcreteProductA(Product):
def operation(self):
return "产品A的操作"
class ConcreteProductB(Product):
def operation(self):
return "产品B的操作"
class Factory:
def create_product(self, product_type):
if product_type == "A":
return ConcreteProductA()
elif product_type == "B":
return ConcreteProductB()
raise ValueError(f"未知产品类型: {product_type}")
# 3. 建造者模式
class Computer:
def __init__(self):
self.cpu = None
self.memory = None
self.storage = None
def __str__(self):
return f"Computer(cpu={self.cpu}, memory={self.memory}, storage={self.storage})"
class ComputerBuilder:
def __init__(self):
self.computer = Computer()
def set_cpu(self, cpu):
self.computer.cpu = cpu
return self
def set_memory(self, memory):
self.computer.memory = memory
return self
def set_storage(self, storage):
self.computer.storage = storage
return self
def build(self):
return self.computer
# 使用建造者
computer = ComputerBuilder()\
.set_cpu("Intel i7")\
.set_memory("16GB")\
.set_storage("512GB SSD")\
.build()
8.2 结构型模式
# 1. 适配器模式
class EuropeanSocket:
def voltage(self):
return 220
def plug_type(self):
return "欧洲标准"
class USSocket:
def voltage(self):
return 110
def plug_type(self):
return "美国标准"
class SocketAdapter:
def __init__(self, socket):
self.socket = socket
def voltage(self):
if self.socket.voltage() == 220:
return 110 # 转换电压
return self.socket.voltage()
def plug_type(self):
if self.socket.plug_type() == "欧洲标准":
return "美国标准"
return self.socket.plug_type()
# 2. 装饰器模式(Python内置支持)
def log_decorator(func):
def wrapper(*args, **kwargs):
print(f"调用函数: {func.__name__}")
return func(*args, **kwargs)
return wrapper
class DataSource:
def read_data(self):
return "原始数据"
class DataSourceDecorator(DataSource):
def __init__(self, source):
self.source = source
def read_data(self):
return self.source.read_data()
class EncryptionDecorator(DataSourceDecorator):
def read_data(self):
data = self.source.read_data()
return f"加密({data})"
class CompressionDecorator(DataSourceDecorator):
def read_data(self):
data = self.source.read_data()
return f"压缩({data})"
# 3. 代理模式
class RealImage:
def __init__(self, filename):
self.filename = filename
self.load_image()
def load_image(self):
print(f"加载图片: {self.filename}")
def display(self):
print(f"显示图片: {self.filename}")
class ImageProxy:
def __init__(self, filename):
self.filename = filename
self.real_image = None
def display(self):
if self.real_image is None:
self.real_image = RealImage(self.filename)
self.real_image.display()
8.3 行为型模式
# 1. 观察者模式
class Subject:
def __init__(self):
self._observers = []
def attach(self, observer):
self._observers.append(observer)
def detach(self, observer):
self._observers.remove(observer)
def notify(self, data):
for observer in self._observers:
observer.update(data)
class Observer:
def update(self, data):
pass
class ConcreteObserver(Observer):
def __init__(self, name):
self.name = name
def update(self, data):
print(f"{self.name} 收到更新: {data}")
# 2. 策略模式
class SortStrategy:
def sort(self, data):
pass
class BubbleSort(SortStrategy):
def sort(self, data):
data = data.copy()
n = len(data)
for i in range(n):
for j in range(0, n-i-1):
if data[j] > data[j+1]:
data[j], data[j+1] = data[j+1], data[j]
return data
class QuickSort(SortStrategy):
def sort(self, data):
if len(data) <= 1:
return data
pivot = data[0]
left = [x for x in data[1:] if x <= pivot]
right = [x for x in data[1:] if x > pivot]
return self.sort(left) + [pivot] + self.sort(right)
class Sorter:
def __init__(self, strategy):
self.strategy = strategy
def set_strategy(self, strategy):
self.strategy = strategy
def sort(self, data):
return self.strategy.sort(data)
# 3. 模板方法模式
class DataProcessor:
def process(self):
self.load_data()
self.process_data()
self.save_data()
def load_data(self):
raise NotImplementedError
def process_data(self):
raise NotImplementedError
def save_data(self):
raise NotImplementedError
class CSVProcessor(DataProcessor):
def load_data(self):
print("加载CSV文件")
def process_data(self):
print("处理CSV数据")
def save_data(self):
print("保存为CSV格式")
class JSONProcessor(DataProcessor):
def load_data(self):
print("加载JSON文件")
def process_data(self):
print("处理JSON数据")
def save_data(self):
print("保存为JSON格式")
九、扩展与嵌入
9.1 C扩展开发
c
// Python C扩展示例
// example_module.c
/*
#include <Python.h>
// 函数实现
static PyObject* example_add(PyObject* self, PyObject* args) {
int a, b;
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
return NULL;
}
return PyLong_FromLong(a + b);
}
// 方法定义
static PyMethodDef ExampleMethods[] = {
{"add", example_add, METH_VARARGS, "Add two numbers"},
{NULL, NULL, 0, NULL}
};
// 模块定义
static struct PyModuleDef examplemodule = {
PyModuleDef_HEAD_INIT,
"example",
NULL,
-1,
ExampleMethods
};
// 模块初始化
PyMODINIT_FUNC PyInit_example(void) {
return PyModule_Create(&examplemodule);
}
*/
# 编译命令
# python setup.py build_ext --inplace
9.2 Cython使用
# cython_example.pyx
"""
def fibonacci(int n):
cdef int i
cdef int a = 0, b = 1, c
for i in range(n):
c = a + b
a = b
b = c
return a
"""
# setup.py
"""
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("cython_example.pyx")
)
"""
# 使用Cython加速Python代码
def pure_python_fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
# 编译后导入
# from cython_example import fibonacci as cython_fibonacci
9.3 ctypes与CFFI
import ctypes
from ctypes import c_int, c_double, POINTER
# 加载C库
libc = ctypes.CDLL("libc.so.6") # Linux
# libc = ctypes.CDLL("msvcrt.dll") # Windows
# 调用C函数
print(f"strlen('hello'): {libc.strlen(b'hello')}")
# 定义C结构体
class Point(ctypes.Structure):
_fields_ = [
("x", c_int),
("y", c_int)
]
# 调用自定义C库
# lib = ctypes.CDLL("./mylib.so")
# lib.add.argtypes = (c_int, c_int)
# lib.add.restype = c_int
# CFFI示例
"""
from cffi import FFI
ffi = FFI()
ffi.cdef("""
int add(int a, int b);
int multiply(int a, int b);
""")
lib = ffi.dlopen("./mylib.so")
result = lib.add(10, 20)
"""
十、Python架构最佳实践
10.1 分层架构
# 典型的三层架构
# 表现层(Presentation Layer)
class UserController:
def __init__(self, user_service):
self.user_service = user_service
def get_user(self, user_id):
try:
user = self.user_service.get_user(user_id)
return {"code": 200, "data": user}
except Exception as e:
return {"code": 500, "message": str(e)}
# 业务逻辑层(Business Layer)
class UserService:
def __init__(self, user_repository):
self.user_repository = user_repository
def get_user(self, user_id):
# 业务逻辑
if user_id <= 0:
raise ValueError("无效的用户ID")
user = self.user_repository.find_by_id(user_id)
if not user:
raise ValueError("用户不存在")
# 处理敏感信息
user.pop('password', None)
return user
# 数据访问层(Data Access Layer)
class UserRepository:
def __init__(self, db_connection):
self.db = db_connection
def find_by_id(self, user_id):
# 数据访问逻辑
cursor = self.db.cursor()
cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
result = cursor.fetchone()
return dict(result) if result else None
10.2 微服务架构
# 服务发现与注册
import consul
import requests
class ServiceRegistry:
def __init__(self, consul_host='localhost', consul_port=8500):
self.consul = consul.Consul(host=consul_host, port=consul_port)
def register(self, name, service_id, address, port):
self.consul.agent.service.register(
name=name,
service_id=service_id,
address=address,
port=port,
check=consul.Check.http(f"http://{address}:{port}/health", interval="10s")
)
def discover(self, name):
_, services = self.consul.health.service(name, passing=True)
return [f"{s['Service']['Address']}:{s['Service']['Port']}" for s in services]
# API网关
class APIGateway:
def __init__(self, service_registry):
self.registry = service_registry
def route(self, service_name, path, method='GET', data=None):
services = self.registry.discover(service_name)
if not services:
raise Exception(f"服务 {service_name} 不可用")
# 简单的负载均衡
service_url = f"http://{services[0]}{path}"
response = requests.request(method, service_url, json=data)
return response.json()
10.3 事件驱动架构
import asyncio
from typing import Dict, List, Callable
from dataclasses import dataclass
from datetime import datetime
@dataclass
class Event:
name: str
data: Dict
timestamp: datetime = None
def __post_init__(self):
if self.timestamp is None:
self.timestamp = datetime.now()
class EventBus:
def __init__(self):
self._handlers: Dict[str, List[Callable]] = {}
def subscribe(self, event_name: str, handler: Callable):
if event_name not in self._handlers:
self._handlers[event_name] = []
self._handlers[event_name].append(handler)
def publish(self, event: Event):
if event.name in self._handlers:
for handler in self._handlers[event.name]:
asyncio.create_task(handler(event))
async def publish_async(self, event: Event):
if event.name in self._handlers:
tasks = [handler(event) for handler in self._handlers[event.name]]
await asyncio.gather(*tasks)
# 事件处理器示例
class UserEventHandlers:
async def on_user_created(self, event: Event):
print(f"用户创建: {event.data}")
# 发送欢迎邮件
# 记录日志
# 触发其他业务
async def on_user_deleted(self, event: Event):
print(f"用户删除: {event.data}")
# 清理用户数据
# 发送通知
# 使用示例
event_bus = EventBus()
handlers = UserEventHandlers()
event_bus.subscribe("user.created", handlers.on_user_created)
event_bus.subscribe("user.deleted", handlers.on_user_deleted)
# 发布事件
event_bus.publish(Event("user.created", {"id": 1, "name": "张三"}))
Python架构设计体现了语言设计者的智慧,从解释器实现到内存管理,从并发模型到扩展机制,每个层面都经过精心设计。本文系统性地梳理了Python架构的核心知识点,从对象模型到性能优化,从设计模式到微服务,帮助开发者深入理解Python的内部工作机制。
来源:
http://unbgv.cn/category/shengxiaoshiye.html