Python架构知识点大全(三)

简介: 教程来源 http://unbgv.cn/category/shengxiaocaiyun.html 本文系统讲解Python架构核心:异常处理(体系、自定义、上下文管理)、23种设计模式的Python实现(单例、工厂、观察者等)、C扩展/Cython/ctypes集成、分层/微服务/事件驱动三大架构实践,助开发者深入理解Python底层机制与工程最佳实践。

七、异常处理架构

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

相关文章
|
17天前
|
人工智能 自然语言处理 API
零基础必看:阿里云轻量服务器部署OpenClaw(Clawdbot)完整教程+百炼Coding Plan API配置避坑指南
在AI智能体技术深度落地的2026年,OpenClaw(原Clawdbot,曾用名Moltbot)凭借大模型+技能插件的组合模式,打破了传统AI仅能语言交互的局限,成为个人办公提效、企业轻量协作的核心工具。这款开源AI智能体框架的核心价值的在于“连接大模型大脑与设备执行能力”,不仅能理解自然语言指令,更能直接在云服务器上执行文件管理、日程安排、跨平台自动化等实际任务,真正实现了从“被动问答”到“主动执行”的跨越。其隐私优先的核心理念,让所有数据在用户自己的服务器上处理,永不上传第三方平台,既保证了数据安全,又实现了自主可控,深受对数据敏感的个人和轻量团队青睐。
544 8
|
30天前
|
机器学习/深度学习 人工智能 语音技术
2026最新阿里云GPU服务器租赁价目表:AI 推理 / 训练算力费用与场景汇总
阿里云AI服务器提供GPU(A10/V100/T4/P4/P100等)、FPGA等多种加速方案,单实例最高5PFLOPS算力,适用于AI训练、推理、科学计算等场景。本文汇总2026年最新月付/年付/按小时优惠价格及规格配置,助您高效选型。(239字)
1035 15
|
22天前
|
存储 人工智能 关系型数据库
OpenClaw怎么可能没痛点?用RDS插件来释放OpenClaw全部潜力
OpenClaw插件是深度介入Agent生命周期的扩展机制,提供24个钩子,支持自动注入知识、持久化记忆等被动式干预。相比Skill/Tool,插件可主动在关键节点(如对话开始/结束)执行逻辑,适用于RAG增强、云化记忆等高级场景。
765 56
OpenClaw怎么可能没痛点?用RDS插件来释放OpenClaw全部潜力
|
6天前
|
人工智能 安全 IDE
多模型自由切换,研发效率再升级实战干货
不是简单的AI补全,而是完整的「研发全流程支撑体系」,把「环境、协作、AI、安全」四大核心模块打包整合,形成可直接落地的实操体系。很多之前要手动折腾半天的事,现在能交给系统自动完成。本篇结合真实使用场景,从认知、核心能力、实战案例到使用心得,把实操细节、踩过的坑和效率提升点一次性说透。
|
15天前
|
关系型数据库 分布式数据库 PolarDB
PolarDB开源新作:DuckDB-paimon,助力企业构建面向AI的多模数据底座
DuckDB-paimon 是 PolarDB 团队开发的 DuckDB 插件,支持直接查询 Apache Paimon 数据湖表(本地/OSS),无需 Flink/Spark 集群。基于 paimon-cpp 原生 C++ 实现,具备列裁剪、多线程扫描、Secret 安全凭证等特性,实现秒级轻量分析。
254 9
|
21天前
|
安全 Go API
Go 1.26 go fix 实战:一键现代化你的Go代码
2026年Go 1.26重磅升级`go fix`:从静态补丁工具跃升为智能重构引擎!支持全项目扫描、自动适配`errors.AsType`/`io.ReadAll`等新特性,提升性能与类型安全。本文带你三步上手、避坑实战,轻松实现代码现代化。(239字)
|
1月前
|
机器学习/深度学习 人工智能 算法
Bug定级不准?让AI学习历史数据,自动标记P0/P1
本文揭秘AI如何解决Bug定级难题:基于历史数据(标题、模块、评论等特征),用随机森林或LSTM-Attention模型自动预测优先级,嵌入JIRA/飞书流程。实践后争议降60%,P0/P1响应提速30%,让团队专注解Bug而非扯皮。
|
22天前
|
SQL 人工智能 分布式计算
OpenClaw + ClaudeCode 在企业数据仓库开发中的实践
本文介绍团队如何用OpenClaw+ClaudeCode构建数仓标签开发自动化工作流:基于ODS-CDM-ADM三层架构,提炼“六步法”(需求分析→数据源调研→方案设计→代码生成→AI审查→测试上线),配合脚本工具与人机协作规范,在保障数据准确前提下,将单个标签开发耗时从6-8小时压缩至1.5–2小时。
776 5
OpenClaw + ClaudeCode 在企业数据仓库开发中的实践
|
15天前
|
人工智能 IDE 测试技术
接口文档一丢,AI自动生成测试用例和自动化脚本?
AI IDE + MCP 正重塑软件测试:需求文档→AI自动生成测试用例与自动化脚本→CI自动执行。相比传统人工编写,它大幅提升效率;区别于知识库方案,AI IDE可操作文件、调用API、构建工程。核心前提:需求需结构化、清晰。
|
21天前
|
人工智能 数据可视化 机器人
Qclaw:你的本地龙虾管家,一键管理 OpenClaw
Qclaw是OpenClaw的图形化管理工具,专为新手设计:一键安装、拳击龙虾钳引导界面、分步可视化配置(模型/API/飞书钉钉/群策略/Skills等),支持定时任务、费用看板、记忆管理与人设加载,让小白轻松上手AI机器人。
1227 2
Qclaw:你的本地龙虾管家,一键管理 OpenClaw