style: 统一代码风格并进行现代化改进
对整个代码库进行了一次全面的风格统一和现代化改进。主要变更包括:
- 将 `hasattr` 等内置函数中的字符串参数从单引号 `'` 统一为双引号 `"`。
- 采用现代类型注解,例如将 `Optional[T]` 替换为 `T | None`,`List[T]` 替换为 `list[T]` 等。
- 移除不再需要的 Python 2 兼容性声明 `# -*- coding: utf-8 -*-`。
- 清理了多余的空行、注释和未使用的导入。
- 统一了文件末尾的换行符。
- 优化了部分日志输出和字符串格式化 (`f"{e!s}"`)。
这些改动旨在提升代码的可读性、一致性和可维护性,使其更符合现代 Python 编码规范。
This commit is contained in:
committed by
Windpicker-owo
parent
1fb2ab6450
commit
cd84373828
@@ -313,11 +313,11 @@ class ChatStream:
|
||||
except Exception as e:
|
||||
logger.error(f"计算消息兴趣值失败: {e}", exc_info=True)
|
||||
# 异常情况下使用默认值
|
||||
if hasattr(db_message, 'interest_value'):
|
||||
if hasattr(db_message, "interest_value"):
|
||||
db_message.interest_value = 0.3
|
||||
if hasattr(db_message, 'should_reply'):
|
||||
if hasattr(db_message, "should_reply"):
|
||||
db_message.should_reply = False
|
||||
if hasattr(db_message, 'should_act'):
|
||||
if hasattr(db_message, "should_act"):
|
||||
db_message.should_act = False
|
||||
|
||||
def _extract_reply_from_segment(self, segment) -> str | None:
|
||||
@@ -894,10 +894,10 @@ def _convert_to_original_stream(self, optimized_stream) -> "ChatStream":
|
||||
original_stream.saved = optimized_stream.saved
|
||||
|
||||
# 复制上下文信息(如果存在)
|
||||
if hasattr(optimized_stream, '_stream_context') and optimized_stream._stream_context:
|
||||
if hasattr(optimized_stream, "_stream_context") and optimized_stream._stream_context:
|
||||
original_stream.stream_context = optimized_stream._stream_context
|
||||
|
||||
if hasattr(optimized_stream, '_context_manager') and optimized_stream._context_manager:
|
||||
if hasattr(optimized_stream, "_context_manager") and optimized_stream._context_manager:
|
||||
original_stream.context_manager = optimized_stream._context_manager
|
||||
|
||||
return original_stream
|
||||
|
||||
@@ -3,17 +3,12 @@
|
||||
避免不必要的深拷贝开销,提升多流并发性能
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import copy
|
||||
import hashlib
|
||||
import time
|
||||
from typing import TYPE_CHECKING, Any, Dict, Optional
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from maim_message import GroupInfo, UserInfo
|
||||
from rich.traceback import install
|
||||
|
||||
from src.common.database.sqlalchemy_database_api import get_db_session
|
||||
from src.common.database.sqlalchemy_models import ChatStreams
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
|
||||
@@ -28,7 +23,7 @@ logger = get_logger("optimized_chat_stream")
|
||||
class SharedContext:
|
||||
"""共享上下文数据 - 只读数据结构"""
|
||||
|
||||
def __init__(self, stream_id: str, platform: str, user_info: UserInfo, group_info: Optional[GroupInfo] = None):
|
||||
def __init__(self, stream_id: str, platform: str, user_info: UserInfo, group_info: GroupInfo | None = None):
|
||||
self.stream_id = stream_id
|
||||
self.platform = platform
|
||||
self.user_info = user_info
|
||||
@@ -37,7 +32,7 @@ class SharedContext:
|
||||
self._frozen = True
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if hasattr(self, '_frozen') and self._frozen and name not in ['_frozen']:
|
||||
if hasattr(self, "_frozen") and self._frozen and name not in ["_frozen"]:
|
||||
raise AttributeError(f"SharedContext is frozen, cannot modify {name}")
|
||||
super().__setattr__(name, value)
|
||||
|
||||
@@ -46,7 +41,7 @@ class LocalChanges:
|
||||
"""本地修改跟踪器"""
|
||||
|
||||
def __init__(self):
|
||||
self._changes: Dict[str, Any] = {}
|
||||
self._changes: dict[str, Any] = {}
|
||||
self._dirty = False
|
||||
|
||||
def set_change(self, key: str, value: Any):
|
||||
@@ -62,7 +57,7 @@ class LocalChanges:
|
||||
"""是否有修改"""
|
||||
return self._dirty
|
||||
|
||||
def get_changes(self) -> Dict[str, Any]:
|
||||
def get_changes(self) -> dict[str, Any]:
|
||||
"""获取所有修改"""
|
||||
return self._changes.copy()
|
||||
|
||||
@@ -80,8 +75,8 @@ class OptimizedChatStream:
|
||||
stream_id: str,
|
||||
platform: str,
|
||||
user_info: UserInfo,
|
||||
group_info: Optional[GroupInfo] = None,
|
||||
data: Optional[Dict] = None,
|
||||
group_info: GroupInfo | None = None,
|
||||
data: dict | None = None,
|
||||
):
|
||||
# 共享的只读数据
|
||||
self._shared_context = SharedContext(
|
||||
@@ -129,42 +124,42 @@ class OptimizedChatStream:
|
||||
"""修改用户信息时触发写时复制"""
|
||||
self._ensure_copy_on_write()
|
||||
# 由于SharedContext是frozen的,我们需要在本地修改中记录
|
||||
self._local_changes.set_change('user_info', value)
|
||||
self._local_changes.set_change("user_info", value)
|
||||
|
||||
@property
|
||||
def group_info(self) -> Optional[GroupInfo]:
|
||||
if self._local_changes.has_changes() and 'group_info' in self._local_changes._changes:
|
||||
return self._local_changes.get_change('group_info')
|
||||
def group_info(self) -> GroupInfo | None:
|
||||
if self._local_changes.has_changes() and "group_info" in self._local_changes._changes:
|
||||
return self._local_changes.get_change("group_info")
|
||||
return self._shared_context.group_info
|
||||
|
||||
@group_info.setter
|
||||
def group_info(self, value: Optional[GroupInfo]):
|
||||
def group_info(self, value: GroupInfo | None):
|
||||
"""修改群组信息时触发写时复制"""
|
||||
self._ensure_copy_on_write()
|
||||
self._local_changes.set_change('group_info', value)
|
||||
self._local_changes.set_change("group_info", value)
|
||||
|
||||
@property
|
||||
def create_time(self) -> float:
|
||||
if self._local_changes.has_changes() and 'create_time' in self._local_changes._changes:
|
||||
return self._local_changes.get_change('create_time')
|
||||
if self._local_changes.has_changes() and "create_time" in self._local_changes._changes:
|
||||
return self._local_changes.get_change("create_time")
|
||||
return self._shared_context.create_time
|
||||
|
||||
@property
|
||||
def last_active_time(self) -> float:
|
||||
return self._local_changes.get_change('last_active_time', self.create_time)
|
||||
return self._local_changes.get_change("last_active_time", self.create_time)
|
||||
|
||||
@last_active_time.setter
|
||||
def last_active_time(self, value: float):
|
||||
self._local_changes.set_change('last_active_time', value)
|
||||
self._local_changes.set_change("last_active_time", value)
|
||||
self.saved = False
|
||||
|
||||
@property
|
||||
def sleep_pressure(self) -> float:
|
||||
return self._local_changes.get_change('sleep_pressure', 0.0)
|
||||
return self._local_changes.get_change("sleep_pressure", 0.0)
|
||||
|
||||
@sleep_pressure.setter
|
||||
def sleep_pressure(self, value: float):
|
||||
self._local_changes.set_change('sleep_pressure', value)
|
||||
self._local_changes.set_change("sleep_pressure", value)
|
||||
self.saved = False
|
||||
|
||||
def _ensure_copy_on_write(self):
|
||||
@@ -176,14 +171,14 @@ class OptimizedChatStream:
|
||||
|
||||
def _get_effective_user_info(self) -> UserInfo:
|
||||
"""获取有效的用户信息"""
|
||||
if self._local_changes.has_changes() and 'user_info' in self._local_changes._changes:
|
||||
return self._local_changes.get_change('user_info')
|
||||
if self._local_changes.has_changes() and "user_info" in self._local_changes._changes:
|
||||
return self._local_changes.get_change("user_info")
|
||||
return self._shared_context.user_info
|
||||
|
||||
def _get_effective_group_info(self) -> Optional[GroupInfo]:
|
||||
def _get_effective_group_info(self) -> GroupInfo | None:
|
||||
"""获取有效的群组信息"""
|
||||
if self._local_changes.has_changes() and 'group_info' in self._local_changes._changes:
|
||||
return self._local_changes.get_change('group_info')
|
||||
if self._local_changes.has_changes() and "group_info" in self._local_changes._changes:
|
||||
return self._local_changes.get_change("group_info")
|
||||
return self._shared_context.group_info
|
||||
|
||||
def update_active_time(self):
|
||||
@@ -199,6 +194,7 @@ class OptimizedChatStream:
|
||||
|
||||
# 将MessageRecv转换为DatabaseMessages并设置到stream_context
|
||||
import json
|
||||
|
||||
from src.common.data_models.database_data_model import DatabaseMessages
|
||||
|
||||
message_info = getattr(message, "message_info", {})
|
||||
@@ -298,7 +294,7 @@ class OptimizedChatStream:
|
||||
self._create_stream_context()
|
||||
return self._context_manager
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
"""转换为字典格式 - 考虑本地修改"""
|
||||
user_info = self._get_effective_user_info()
|
||||
group_info = self._get_effective_group_info()
|
||||
@@ -319,7 +315,7 @@ class OptimizedChatStream:
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict) -> "OptimizedChatStream":
|
||||
def from_dict(cls, data: dict) -> "OptimizedChatStream":
|
||||
"""从字典创建实例"""
|
||||
user_info = UserInfo.from_dict(data.get("user_info", {})) if data.get("user_info") else None
|
||||
group_info = GroupInfo.from_dict(data.get("group_info", {})) if data.get("group_info") else None
|
||||
@@ -481,8 +477,8 @@ def create_optimized_chat_stream(
|
||||
stream_id: str,
|
||||
platform: str,
|
||||
user_info: UserInfo,
|
||||
group_info: Optional[GroupInfo] = None,
|
||||
data: Optional[Dict] = None,
|
||||
group_info: GroupInfo | None = None,
|
||||
data: dict | None = None,
|
||||
) -> OptimizedChatStream:
|
||||
"""创建优化版聊天流实例"""
|
||||
return OptimizedChatStream(
|
||||
@@ -491,4 +487,4 @@ def create_optimized_chat_stream(
|
||||
user_info=user_info,
|
||||
group_info=group_info,
|
||||
data=data
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user