Files
Mofox-Core/src/__init__.py
明天好像没什么 5638076328 refactor: 统一类型注解风格并优化代码结构
- 将裸 except 改为显式 Exception 捕获
- 用列表推导式替换冗余 for 循环
- 为类属性添加 ClassVar 注解
- 统一 Union/Optional 写法为 |
- 移除未使用的导入
- 修复 SQLAlchemy 空值比较语法
- 优化字符串拼接与字典更新逻辑
- 补充缺失的 noqa 注释与异常链

BREAKING CHANGE: 所有插件基类的类级字段现要求显式 ClassVar 注解,自定义插件需同步更新
2025-11-19 23:30:26 +08:00

68 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import random
from collections.abc import Sequence
from colorama import Fore, init
from src.common.logger import get_logger
egg = get_logger("小彩蛋")
def weighted_choice(data: Sequence[str], weights: list[float] | None = None) -> str:
"""
从 data 中按权重随机返回一条。
若 weights 为 None则所有元素权重默认为 1。
"""
if weights is None:
weights = [1.0] * len(data)
if len(data) != len(weights):
raise ValueError("data 和 weights 长度必须相等")
# 计算累计权重区间
total = 0.0
acc = []
for w in weights:
total += w
acc.append(total)
if total <= 0:
raise ValueError("总权重必须大于 0")
# 随机落点
r = random.random() * total
# 二分查找落点所在的区间
left, right = 0, len(acc) - 1
while left < right:
mid = (left + right) // 2
if r < acc[mid]:
right = mid
else:
left = mid + 1
return data[left]
class BaseMain:
"""基础主程序类"""
def __init__(self):
"""初始化基础主程序"""
self.easter_egg()
@staticmethod
def easter_egg():
# 彩蛋
init()
items = [
"多年以后面对AI行刑队张三将会回想起他2023年在会议上讨论人工智能的那个下午",
"你知道吗诺狐的耳朵很软很好rua",
"喵喵~你的麦麦被猫娘入侵了喵~",
]
w = [10, 5, 2]
text = weighted_choice(items, w)
rainbow_colors = [Fore.RED, Fore.YELLOW, Fore.GREEN, Fore.CYAN, Fore.BLUE, Fore.MAGENTA]
rainbow_text = ""
for i, char in enumerate(text):
rainbow_text += rainbow_colors[i % len(rainbow_colors)] + char
egg.info(rainbow_text)