Python工程与模块命名规范:构建可维护的大型项目架构

目录

Python工程与模块命名规范:构建可维护的大型项目架构

引言:命名的重要性

在软件开发中,命名可能是最容易被忽视但却是最重要的实践之一。根据2023年对Python开源项目的分析,超过35%的维护问题与糟糕的命名约定直接相关。一个好的命名系统不仅提高代码可读性,还能显著降低团队协作成本。

“计算机科学中只有两件难事:缓存失效和命名事物。” —— Phil Karlton

本文将深入探讨Python工程和模块命名的艺术与科学,提供一套完整的、可实践的命名体系。

一、命名基础理论与原则

1.1 命名的认知心理学基础

优秀的命名遵循人类认知模式,需要考虑:

· 识别性:名称应快速传达含义
· 区分性:相似概念应有明显区别
· 一致性:相同概念使用相同命名模式

优秀命名
认知负荷降低
维护成本减少
协作效率提升
快速理解
减少误解
易于修改
bug减少
新成员上手快
沟通更顺畅

1.2 命名核心原则

1.2.1 清晰性 over 简洁性

# 糟糕的命名
def proc(d): return d * 1.1# 良好的命名  
def apply_tax(amount):return amount * TAX_RATE

1.2.2 一致性原则

在整个项目中保持相同的命名模式:

# 不一致的命名
customer_id = 123
userName = "john"
USER_EMAIL = "john@example.com"# 一致的命名
customer_id = 123
customer_name = "john" 
customer_email = "john@example.com"

1.2.3 避免歧义

# 有歧义的命名
list = []  # 覆盖内置类型
str = "hello"  # 覆盖内置函数# 明确的命名
items = []
greeting = "hello"

二、Python工程结构设计

2.1 标准项目结构

project-root/
├── src/                    # 源代码目录
│   └── package_name/       # 主包
│       ├── __init__.py
│       ├── core/           # 核心模块
│       │   ├── __init__.py
│       │   ├── models.py
│       │   └── utils.py
│       ├── api/            # API模块
│       │   ├── __init__.py
│       │   ├── routes.py
│       │   └── schemas.py
│       └── services/       # 服务层
│           ├── __init__.py
│           ├── user_service.py
│           └── payment_service.py
├── tests/                  # 测试目录
│   ├── __init__.py
│   ├── conftest.py
│   ├── unit/
│   │   ├── test_models.py
│   │   └── test_services.py
│   └── integration/
│       ├── test_api.py
│       └── test_database.py
├── docs/                   # 文档
├── scripts/                # 脚本文件
├── data/                   # 数据文件
├── requirements.txt        # 依赖文件
├── pyproject.toml          # 项目配置
└── README.md               # 项目说明

2.2 工程命名规范

2.2.1 项目根目录命名

# 好的项目名
financial-analysis-tool  # 使用连字符
data_processing_pipeline  # 使用下划线
imageClassifier  # 驼峰式(较少用)# 避免的项目名
myproject  # 太泛
project123  # 无意义数字
test-project  # 可能冲突

2.2.2 包和模块命名

# 包名(目录)
models/          # 复数形式
utils/           # 工具函数
services/        # 服务层
adapters/        # 适配器# 模块名(文件)
user_model.py    # 明确单一职责
database_connection.py
config_loader.py

三、详细命名约定与模式

3.1 变量命名规范

3.1.1 基础变量命名

# 基本数据类型
count = 10                  # 整数
price = 29.99               # 浮点数
name = "Alice"              # 字符串
is_active = True            # 布尔值
items = []                  # 列表
user_map = {}               # 字典# 集合类型
user_list = []              # 列表
user_dict = {}              # 字典  
user_set = set()            # 集合
user_tuple = ()             # 元组

3.1.2 复合变量命名

# 使用有意义的复合名称
max_retry_count = 3         # 最大重试次数
default_timeout_seconds = 30  # 默认超时秒数
is_user_authenticated = True  # 用户是否认证# 避免的命名
temp = get_data()           # 无意义
var1 = calculate()          # 编号命名
flag = check_status()       # 过于泛化

3.2 函数与方法命名

3.2.1 动作-对象命名模式

# 好的函数名
def calculate_total_price(items): ...
def validate_user_input(input_data): ...
def send_email_notification(recipient, message): ...
def create_user_profile(user_data): ...# 动词选择指南
# 获取数据: get_, fetch_, retrieve_, load_
# 修改数据: update_, modify_, set_, change_
# 创建数据: create_, add_, insert_, make_
# 删除数据: delete_, remove_, drop_, erase_
# 检查状态: is_, has_, can_, should_, check_

3.2.2 布尔函数命名

# 返回布尔值的函数
def is_valid_email(email): ...
def has_permission(user, resource): ...
def should_retry_request(response): ...
def can_user_access(user, feature): ...# 使用正面的布尔命名
def is_available(): ...     # 而不是 is_not_available
def has_content(): ...      # 而不是 lacks_content

3.3 类命名规范

3.3.1 类与对象命名

# 类名使用驼峰式
class UserAccount: def __init__(self, username):self.username = username  # 实例变量使用下划线self._private_data = {}   # 私有变量前加下划线class DatabaseConnection:def connect(self):passclass HTTPRequestHandler:def handle_request(self):pass# 避免的类名
class myClass: ...          # 应使用驼峰
class User_Account: ...     # 不应使用下划线

3.3.2 特殊方法命名

class Vector:def __init__(self, x, y):self.x = xself.y = y# 运算符重载def __add__(self, other):return Vector(self.x + other.x, self.y + other.y)# 字符串表示def __str__(self):return f"Vector({self.x}, {self.y})"# 容器协议def __len__(self):return 2# 上下文管理器def __enter__(self):return selfdef __exit__(self, exc_type, exc_val, exc_tb):pass

3.4 常量与配置命名

# 常量使用全大写+下划线
MAX_CONNECTIONS = 100
DEFAULT_TIMEOUT = 30
API_BASE_URL = "https://api.example.com"
SUPPORTED_FILE_FORMATS = ['.json', '.csv', '.xml']# 配置类常量
class Config:DATABASE_URL = "postgresql://user:pass@localhost/db"LOG_LEVEL = "INFO"CACHE_TIMEOUT = 3600

四、模块化设计与命名策略

4.1 模块职责划分

graph TBA[项目架构] --> B[核心模块]A --> C[业务模块]A --> D[工具模块]A --> E[接口模块]B --> B1[models.py]B --> B2[exceptions.py]B --> B3[constants.py]C --> C1[user_service.py]C --> C2[order_service.py]C --> C3[payment_service.py]D --> D1[database.py]D --> D2[logger.py]D --> D3[validators.py]E --> E1[api/E1 --> E11[routes.py]E1 --> E12[schemas.py]E1 --> E13[controllers.py]

4.2 模块命名模式

4.2.1 按功能划分模块

# 数据模型模块
# models.py
class User:passclass Product:passclass Order:pass# 服务模块
# user_service.py
class UserService:def create_user(self, user_data): ...def get_user(self, user_id): ...# 工具模块
# validation_utils.py
def validate_email(email): ...
def validate_phone(phone): ...

4.2.2 按层级划分模块

# 数据访问层
# repositories/
# user_repository.py
class UserRepository:def save(self, user): ...def find_by_id(self, user_id): ...# 业务逻辑层  
# services/
# user_service.py
class UserService:def __init__(self, user_repo):self.user_repo = user_repodef register_user(self, user_data): ...# 表示层
# web/
# controllers.py
class UserController:def post_user(self, request): ...

五、高级命名模式与技巧

5.1 设计模式相关命名

# 工厂模式
class ConnectionFactory:def create_connection(self, config): ...# 单例模式
class DatabaseManager:_instance = Nonedef __new__(cls):if cls._instance is None:cls._instance = super().__new__(cls)return cls._instance# 观察者模式
class EventPublisher:def __init__(self):self._subscribers = []def subscribe(self, subscriber): ...def notify_subscribers(self, event): ...class EventSubscriber:def on_event(self, event): ...# 策略模式
class CompressionStrategy:def compress(self, data): ...class ZipCompression(CompressionStrategy):def compress(self, data): ...class GzipCompression(CompressionStrategy):def compress(self, data): ...

5.2 领域驱动设计(DDD)命名

# 实体
class Customer:def __init__(self, customer_id, name):self.customer_id = customer_id  # 唯一标识self.name = name# 值对象
class Money:def __init__(self, amount, currency):self.amount = amountself.currency = currency# 聚合根
class Order:def __init__(self, order_id, order_items):self.order_id = order_idself.order_items = order_itemsdef add_item(self, product, quantity): ...def calculate_total(self): ...# 领域服务
class OrderProcessingService:def process_order(self, order): ...# 仓储接口
class OrderRepository:def save(self, order): ...def find_by_id(self, order_id): ...

5.3 异步编程命名

import asyncioclass AsyncDataProcessor:# 异步方法使用async前缀或相关动词async def fetch_data(self, url): ...async def process_data(self, data): ...async def save_results(self, results): ...# 回调函数命名def on_data_received(self, data): ...def on_processing_complete(self, result): ...def on_error(self, error): ...# 异步上下文管理器
class AsyncDatabaseConnection:async def __aenter__(self):await self.connect()return selfasync def __aexit__(self, exc_type, exc_val, exc_tb):await self.close()async def connect(self): ...async def close(self): ...

六、完整项目示例

6.1 电子商务项目结构

ecommerce-platform/
├── src/
│   └── ecommerce/
│       ├── __init__.py
│       ├── core/
│       │   ├── __init__.py
│       │   ├── models.py
│       │   ├── exceptions.py
│       │   └── types.py
│       ├── domain/
│       │   ├── __init__.py
│       │   ├── product.py
│       │   ├── order.py
│       │   ├── user.py
│       │   └── payment.py
│       ├── application/
│       │   ├── __init__.py
│       │   ├── product_service.py
│       │   ├── order_service.py
│       │   ├── user_service.py
│       │   └── payment_service.py
│       ├── infrastructure/
│       │   ├── __init__.py
│       │   ├── database/
│       │   │   ├── __init__.py
│       │   │   ├── connection.py
│       │   │   ├── repositories.py
│       │   │   └── migrations/
│       │   ├── cache/
│       │   │   ├── __init__.py
│       │   │   └── redis_client.py
│       │   └── external/
│       │       ├── __init__.py
│       │       ├── email_service.py
│       │       └── payment_gateway.py
│       ├── api/
│       │   ├── __init__.py
│       │   ├── routes/
│       │   │   ├── __init__.py
│       │   │   ├── product_routes.py
│       │   │   ├── order_routes.py
│       │   │   └── user_routes.py
│       │   ├── schemas/
│       │   │   ├── __init__.py
│       │   │   ├── product_schemas.py
│       │   │   ├── order_schemas.py
│       │   │   └── user_schemas.py
│       │   └── middleware/
│       │       ├── __init__.py
│       │       ├── authentication.py
│       │       ├── logging.py
│       │       └── error_handling.py
│       └── utils/
│           ├── __init__.py
│           ├── validation.py
│           ├── logging.py
│           ├── security.py
│           └── helpers.py
├── tests/
│   ├── __init__.py
│   ├── unit/
│   │   ├── __init__.py
│   │   ├── test_models.py
│   │   ├── test_services.py
│   │   └── test_utils.py
│   ├── integration/
│   │   ├── __init__.py
│   │   ├── test_api.py
│   │   ├── test_database.py
│   │   └── test_external_services.py
│   └── conftest.py
├── scripts/
│   ├── setup_database.py
│   ├── run_migrations.py
│   └── start_server.py
├── docs/
│   ├── api.md
│   ├── architecture.md
│   └── development.md
├── requirements/
│   ├── base.txt
│   ├── development.txt
│   └── production.txt
├── config/
│   ├── development.yaml
│   ├── production.yaml
│   └── test.yaml
├── .pre-commit-config.yaml
├── pyproject.toml
├── Dockerfile
└── README.md

6.2 核心模块代码示例

# src/ecommerce/core/models.py
"""数据模型定义"""from datetime import datetime
from typing import Optional, List
from decimal import Decimalclass BaseModel:"""所有模型的基类"""created_at: datetimeupdated_at: datetimeis_active: bool = Trueclass Product(BaseModel):"""产品实体"""def __init__(self,product_id: str,name: str,price: Decimal,description: Optional[str] = None,stock_quantity: int = 0):self.product_id = product_idself.name = nameself.price = priceself.description = descriptionself.stock_quantity = stock_quantityself.created_at = datetime.now()self.updated_at = datetime.now()def update_price(self, new_price: Decimal) -> None:"""更新产品价格"""if new_price <= 0:raise ValueError("价格必须大于0")self.price = new_priceself.updated_at = datetime.now()def reduce_stock(self, quantity: int) -> None:"""减少库存"""if quantity > self.stock_quantity:raise ValueError("库存不足")self.stock_quantity -= quantityself.updated_at = datetime.now()class Order(BaseModel):"""订单实体"""def __init__(self, order_id: str, customer_id: str):self.order_id = order_idself.customer_id = customer_idself.order_items: List[OrderItem] = []self.status = "pending"self.total_amount = Decimal('0.00')self.created_at = datetime.now()self.updated_at = datetime.now()def add_item(self, product: Product, quantity: int) -> None:"""添加订单项"""if quantity <= 0:raise ValueError("数量必须大于0")# 检查库存if quantity > product.stock_quantity:raise ValueError("产品库存不足")item_total = product.price * quantityorder_item = OrderItem(product, quantity, item_total)self.order_items.append(order_item)self.total_amount += item_totalself.updated_at = datetime.now()def calculate_total(self) -> Decimal:"""计算订单总额"""return sum(item.total_price for item in self.order_items)class OrderItem:"""订单项值对象"""def __init__(self, product: Product, quantity: int, total_price: Decimal):self.product = productself.quantity = quantityself.total_price = total_price

6.3 服务层代码示例

# src/ecommerce/application/order_service.py
"""订单服务层"""from typing import List, Optional
from decimal import Decimal
from datetime import datetime
from ..core.models import Order, Product
from ..core.exceptions import (OrderNotFoundException,ProductNotFoundException,InsufficientStockException
)class OrderService:"""订单业务服务"""def __init__(self, order_repository, product_repository):self.order_repository = order_repositoryself.product_repository = product_repositorydef create_order(self, customer_id: str, items: List[dict]) -> Order:"""创建新订单"""order = Order(order_id=self._generate_order_id(),customer_id=customer_id)for item in items:product = self.product_repository.find_by_id(item['product_id'])if not product:raise ProductNotFoundException(f"产品 {item['product_id']} 不存在")try:order.add_item(product, item['quantity'])except ValueError as e:raise InsufficientStockException(str(e))self.order_repository.save(order)return orderdef get_order(self, order_id: str) -> Order:"""获取订单详情"""order = self.order_repository.find_by_id(order_id)if not order:raise OrderNotFoundException(f"订单 {order_id} 不存在")return orderdef update_order_status(self, order_id: str, new_status: str) -> Order:"""更新订单状态"""order = self.get_order(order_id)order.status = new_statusorder.updated_at = datetime.now()self.order_repository.save(order)return orderdef calculate_order_discount(self,order: Order,discount_percentage: Decimal) -> Decimal:"""计算订单折扣"""if not 0 <= discount_percentage <= 100:raise ValueError("折扣百分比必须在0-100之间")discount_factor = discount_percentage / Decimal('100.00')return order.total_amount * discount_factordef _generate_order_id(self) -> str:"""生成订单ID"""timestamp = datetime.now().strftime("%Y%m%d%H%M%S")return f"ORD{timestamp}"

6.4 API层代码示例

# src/ecommerce/api/routes/order_routes.py
"""订单API路由"""from fastapi import APIRouter, Depends, HTTPException, status
from typing import List
from ...application.order_service import OrderService
from ...infrastructure.database.repositories import get_order_repository
from ...infrastructure.database.repositories import get_product_repository
from ..schemas.order_schemas import (OrderCreateRequest,OrderResponse,OrderStatusUpdate
)router = APIRouter(prefix="/orders", tags=["orders"])@router.post("/", response_model=OrderResponse, status_code=status.HTTP_201_CREATED)
async def create_order(request: OrderCreateRequest,order_service: OrderService = Depends(get_order_service)
) -> OrderResponse:"""创建新订单"""try:order = order_service.create_order(customer_id=request.customer_id,items=request.items)return OrderResponse.from_domain(order)except Exception as e:raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,detail=str(e))@router.get("/{order_id}", response_model=OrderResponse)
async def get_order(order_id: str,order_service: OrderService = Depends(get_order_service)
) -> OrderResponse:"""获取订单详情"""try:order = order_service.get_order(order_id)return OrderResponse.from_domain(order)except Exception as e:raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,detail=str(e))@router.patch("/{order_id}/status", response_model=OrderResponse)
async def update_order_status(order_id: str,status_update: OrderStatusUpdate,order_service: OrderService = Depends(get_order_service)
) -> OrderResponse:"""更新订单状态"""try:order = order_service.update_order_status(order_id=order_id,new_status=status_update.status)return OrderResponse.from_domain(order)except Exception as e:raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,detail=str(e))def get_order_service():"""获取订单服务依赖"""order_repo = get_order_repository()product_repo = get_product_repository()return OrderService(order_repo, product_repo)

七、自动化检查与工具

7.1 使用pre-commit进行命名检查

# .pre-commit-config.yaml
repos:- repo: https://github.com/pre-commit/pre-commit-hooksrev: v4.4.0hooks:- id: trailing-whitespace- id: end-of-file-fixer- id: check-yaml- id: check-added-large-files- repo: https://github.com/PyCQA/flake8rev: 6.0.0hooks:- id: flake8additional_dependencies: [flake8-naming]- repo: https://github.com/psf/blackrev: 23.3.0hooks:- id: black- repo: https://github.com/pycqa/isortrev: 5.12.0hooks:- id: isort- repo: https://github.com/pre-commit/mirrors-mypyrev: v1.3.0hooks:- id: mypy

7.2 自定义命名检查规则

# scripts/check_naming.py
"""自定义命名检查脚本"""import ast
import re
from pathlib import Path
from typing import List, Dict, Anyclass NamingChecker(ast.NodeVisitor):"""AST节点访问器,检查命名规范"""def __init__(self):self.violations: List[Dict[str, Any]] = []self.class_names = set()self.function_names = set()def visit_ClassDef(self, node: ast.ClassDef) -> None:"""检查类命名"""if not re.match(r'^[A-Z][a-zA-Z0-9]*$', node.name):self.violations.append({'type': 'class','name': node.name,'line': node.lineno,'message': '类名应使用驼峰命名法'})self.class_names.add(node.name)self.generic_visit(node)def visit_FunctionDef(self, node: ast.FunctionDef) -> None:"""检查函数命名"""if not re.match(r'^[a-z_][a-z0-9_]*$', node.name):self.violations.append({'type': 'function','name': node.name,'line': node.lineno,'message': '函数名应使用蛇形命名法'})# 检查布尔函数命名if node.name.startswith('is_') or node.name.startswith('has_'):# 应该返回布尔值passself.function_names.add(node.name)self.generic_visit(node)def visit_Name(self, node: ast.Name) -> None:"""检查变量命名"""if isinstance(node.ctx, ast.Store):if not re.match(r'^[a-z_][a-z0-9_]*$', node.id):self.violations.append({'type': 'variable','name': node.id,'line': node.lineno,'message': '变量名应使用蛇形命名法'})self.generic_visit(node)def visit_Constant(self, node: ast.Constant) -> None:"""检查常量命名(如果被赋值)"""if hasattr(node, 'parent') and isinstance(node.parent, ast.Assign):for target in node.parent.targets:if isinstance(target, ast.Name) and target.id.isupper():if not re.match(r'^[A-Z_][A-Z0-9_]*$', target.id):self.violations.append({'type': 'constant','name': target.id,'line': node.lineno,'message': '常量名应使用全大写蛇形命名法'})def check_file_naming(file_path: Path) -> List[Dict[str, Any]]:"""检查单个文件的命名规范"""try:with open(file_path, 'r', encoding='utf-8') as f:content = f.read()tree = ast.parse(content)checker = NamingChecker()checker.visit(tree)return checker.violationsexcept Exception as e:return [{'type': 'error','name': str(file_path),'line': 0,'message': f'解析文件时出错: {str(e)}'}]def main():"""主检查函数"""project_root = Path(__file__).parent.parentpython_files = list(project_root.rglob('*.py'))all_violations = []for file_path in python_files:if 'venv' in str(file_path) or '.pytest_cache' in str(file_path):continueviolations = check_file_naming(file_path)for violation in violations:violation['file'] = str(file_path.relative_to(project_root))all_violations.append(violation)if all_violations:print("命名规范检查发现以下问题:")for violation in all_violations:print(f"{violation['file']}:{violation['line']} - "f"{violation['type']} '{violation['name']}': "f"{violation['message']}")return 1else:print("所有文件命名规范检查通过!")return 0if __name__ == "__main__":exit(main())

八、总结与最佳实践

8.1 核心命名原则回顾

  1. 清晰性优先:名称应准确描述其用途
  2. 一致性保持:相同概念使用相同命名模式
  3. 简洁性适当:在清晰的前提下保持简洁
  4. 避免歧义:不使用可能引起混淆的名称

8.2 项目级别建议

  1. 制定命名规范文档:团队共享的命名约定
  2. 使用自动化工具:集成到CI/CD流程中
  3. 定期代码审查:人工检查命名质量
  4. 重构改进:持续优化现有代码命名

8.3 性能与可维护性平衡

优秀的命名虽然可能稍微增加代码长度,但带来的可维护性提升是显著的。研究表明,良好的命名可以减少20-30%的代码理解时间和15-25%的bug引入几率。

通过本文提供的规范和示例,您可以构建出既符合Python社区标准又满足项目特定需求的命名体系,为构建可维护的大型Python项目奠定坚实基础。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/96226.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/96226.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Props 与 State 类型定义

下面&#xff0c;我们来系统的梳理关于 TypeScript 集成&#xff1a;Props 与 State 类型定义 的基本知识点&#xff1a;一、TypeScript 在 React 中的核心价值 TypeScript 为 React 开发提供了强大的类型安全保证&#xff0c;特别是在定义组件 Props 和 State 时&#xff1a; …

[1Prompt1Story] 注意力机制增强 IPCA | 去噪神经网络 UNet | U型架构分步去噪

第五章&#xff1a;注意力机制增强&#xff08;IPCA&#xff09; 欢迎回到1Prompt1Story&#x1f43b;‍❄️ 在第四章中&#xff0c;我们掌握了**语义向量重加权&#xff08;SVR&#xff09;**技术&#xff0c;通过语义向量调节实现核心要素强化。 但当场景从"雪地嬉戏…

【P7071 [CSP-J2020] 优秀的拆分 - 洛谷 https://www.luogu.com.cn/problem/P7071】

题目 P7071 [CSP-J2020] 优秀的拆分 - 洛谷 https://www.luogu.com.cn/problem/P7071 代码 #include <bits/stdc.h> using namespace std; const int N1e71; int d; vector<int> v; bool k[N]; bool fen(int x){if(x0)return 1;//能拆分完 for(int ix;i>x/…

从ioutil到os:Golang在线客服聊天系统文件读取的迁移实践

了解更多&#xff0c;搜索"程序员老狼"作为一名Golang开发者&#xff0c;我最近在维护一个客服系统时遇到了一个看似简单却值得深思的问题&#xff1a;如何将项目中遗留的ioutil.ReadFile调用迁移到现代的os.ReadFile。这看似只是一个简单的函数替换&#xff0c;但背…

Python UI自动化测试Web frame及多窗口切换

这篇文章主要为大家介绍了Python UI自动化测试Web frame及多窗口切换&#xff0c;有需要的朋友可以借鉴参考下&#xff0c;希望能够有所帮助&#xff0c;祝大家多多进步&#xff0c;早日升职加薪 一、什么是frame&frame切换&#xff1f; frame&#xff1a;HTML页面中的一…

工业相机基本知识解读:像元、帧率、数据接口等

工业相机&#xff08;Industrial Camera&#xff09;是一种专门为工业自动化和机器视觉应用而设计的成像设备&#xff0c;它不同于消费类相机&#xff08;如手机、单反&#xff09;&#xff0c;主要追求的是成像稳定性、长时间可靠性、实时性和精确性。它通常与镜头、光源、图像…

RTC之神奇小闹钟

&#x1f3aa; RTC 是什么&#xff1f;—— 电子设备的“迷你生物钟”想象一下&#xff1a;你晚上睡觉时&#xff0c;手机关机了。但当你第二天开机&#xff0c;它居然知道现在几点&#xff01;这就是 RTC&#xff08;Real-Time Clock&#xff0c;实时时钟&#xff09; 的功劳&…

判断IP是否属于某个网段

判断IP是否属于某个网段判断一个IP是否是否属于某个CIDR网段&#xff0c;核心是比较IP与网段的网络位是否一致&#xff0c;步骤如下&#xff1a; 一、明确CIDR网段的两个关键信息 假设要判断的IP是 IPx&#xff0c;目标网段是 CIDR 网段地址/n&#xff08;例如 192.168.1.0/24…

Python day50

浙大疏锦行 python day50. 在预训练模型&#xff08;resnet18&#xff09;中添加cbam注意力机制&#xff0c;需要修改模型的架构&#xff0c;同时应该考虑插入的cbam注意力机制模块的位置&#xff1b; import torch import torch.nn as nn from torchvision import models# 自…

VPS海外节点性能监控全攻略:从基础配置到高级优化

在全球化业务部署中&#xff0c;VPS海外节点的稳定运行直接影响用户体验。本文将深入解析如何构建高效的性能监控体系&#xff0c;涵盖网络延迟检测、资源阈值设置、告警机制优化等核心环节&#xff0c;帮助运维人员实现跨国服务器的可视化管控。 VPS海外节点性能监控全攻略&am…

C语言初学者笔记【结构体】

文章目录一、结构体的使用1. 结构体声明2. 变量创建与初始化3. 特殊声明与陷阱二、内存对齐1. 规则&#xff1a;2. 示例分析&#xff1a;3. 修改默认对齐数&#xff1a;三、结构体传参四、结构体实现位段1. 定义2. 内存分配3. 应用场景4. 跨平台问题&#xff1a;5. 注意事项&am…

基于XGBoost算法的数据回归预测 极限梯度提升算法 XGBoost

一、作品详细简介 1.1附件文件夹程序代码截图 全部完整源代码&#xff0c;请在个人首页置顶文章查看&#xff1a; 学行库小秘_CSDN博客​编辑https://blog.csdn.net/weixin_47760707?spm1000.2115.3001.5343 1.2各文件夹说明 1.2.1 main.m主函数文件 该MATLAB 代码实现了…

数据安全系列4:常用的对称算法浅析

常用的算法介绍 常用的算法JAVA实现 jce及其它开源包介绍、对比 传送门 数据安全系列1&#xff1a;开篇 数据安全系列2&#xff1a;单向散列函数概念 数据安全系列3&#xff1a;密码技术概述 时代有浪潮&#xff0c;就有退去的时候 在我的博客文章里面&#xff0c;其中…

云计算学习100天-第26天

地址重写地址重写语法——关于Nginx服务器的地址重写&#xff0c;主要用到的配置参数是rewrite 语法格式&#xff1a; rewrite regex replacement flag rewrite 旧地址 新地址 [选项]地址重写步骤&#xff1a;#修改配置文件(访问a.html重定向到b.html) cd /usr/local/ngin…

【Python办公】字符分割拼接工具(GUI工具)

目录 专栏导读 项目简介 功能特性 🔧 核心功能 1. 字符分割功能 2. 字符拼接功能 🎨 界面特性 现代化设计 用户体验优化 技术实现 开发环境 核心代码结构 关键技术点 使用指南 安装步骤 完整代码 字符分割操作 字符拼接操作 应用场景 数据处理 文本编辑 开发辅助 项目优势 …

Windows 命令行:dir 命令

专栏导航 上一篇&#xff1a;Windows 命令行&#xff1a;Exit 命令 回到目录 下一篇&#xff1a;MFC 第一章概述 本节前言 学习本节知识&#xff0c;需要你首先懂得如何打开一个命令行界面&#xff0c;也就是命令提示符界面。链接如下。 参考课节&#xff1a;Windows 命令…

软考高级--系统架构设计师--案例分析真题解析

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录前言试题一 软件架构设计一、2019年 案例分析二、2020年 案例分析三、2021年 案例分析四、2022年 案例分析试题二 软件系统设计一、2019年 案例分析二、2020年 案例分…

css中的性能优化之content-visibility: auto

content-visibility: auto的核心机制是让浏览器智能跳过屏幕外元素的渲染工作&#xff0c;包括布局和绘制&#xff0c;直到它们接近视口时才渲染。这与虚拟滚动等传统方案相比优势明显&#xff0c;只需要一行CSS就能实现近似效果。值得注意的是必须配合contain-intrinsic-size属…

通过uniapp将vite vue3项目打包为android系统的.apk包,并实现可自动升级功能

打包vue项目,注意vite.config.ts文件和路由文件设置 vite.config.ts,将base等配置改为./ import {fileURLToPath, URL } from node:urlimport {defineConfig } from vite import vue from @vitejs/plugin-vue import AutoImport from unplugin-auto-import/vite import Com…

经营帮租赁经营板块:解锁资产运营新生态,赋能企业增长新引擎

在商业浪潮奔涌向前的当下&#xff0c;企业资产运营与租赁管理的模式不断迭代&#xff0c;“经营帮” 以其租赁经营板块为支点&#xff0c;构建起涵盖多元业务场景、适配不同需求的生态体系&#xff0c;成为众多企业破局资产低效困局、挖掘增长新动能的关键助力。本文将深度拆解…