refactor(cleanup): 移除剩余死代码模块
- 删除 douyin/utils/douyin_text_utils.py 及其测试 - 移除 performance_tracker.py 中未使用的 track_function 方法 - 移除 param_usage_tracer.py 中未使用的 stop_tracing / is_enabled - 清理 bin/run_cli.py 和 tests/test_imports.py 中的残留引用 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c4544489ac
commit
96fdc96f24
7 files changed
+3
-288
No files matched your search
@@ -76,7 +76,6 @@ def _load_local_shim(mod_name: str, rel_path: str) -> None:
|
||||
|
||||
|
||||
# 1) 先加载本地 shim 模块,确保后续 import 优先使用这些实现
|
||||
_load_local_shim("douyin_text_utils", "douyin/utils/douyin_text_utils.py")
|
||||
_load_local_shim("douyin_swipe_ops", "douyin/handlers/douyin_swipe_ops.py")
|
||||
|
||||
# 2) 再把完整逻辑所在目录追加到 sys.path,供 `douyin_fudai` 等模块导入
|
||||
|
||||
@@ -33,17 +33,6 @@ class PerformanceTracker:
|
||||
return 0.0
|
||||
return time.perf_counter() - start
|
||||
|
||||
def track_function(self, func: Callable) -> Callable:
|
||||
def wrapper(*args, **kwargs):
|
||||
name = func.__qualname__
|
||||
timer = self.start_timer(name)
|
||||
try:
|
||||
result = func(*args, **kwargs)
|
||||
return result
|
||||
finally:
|
||||
self.end_timer(timer)
|
||||
return wrapper
|
||||
|
||||
def track_step(self, name: str) -> Callable:
|
||||
def decorator(func: Callable) -> Callable:
|
||||
def inner(*args, **kwargs):
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
"""福袋相关文本解析辅助函数。
|
||||
|
||||
本模块提供若干轻量级的解析函数,主要用于:
|
||||
- 从 OCR 文本中抽取福袋内容/倒计时/价值/参与人数
|
||||
- 方便在单元测试中独立验证文本处理逻辑
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
import re
|
||||
|
||||
from douyin.utils.douyin_utils import normalize_countdown_text, extract_prize_value_yuan
|
||||
|
||||
|
||||
def _safe_str(text: Optional[str]) -> str:
|
||||
"""将输入安全地转换为字符串,None -> 空字符串。"""
|
||||
if text is None:
|
||||
return ""
|
||||
return str(text)
|
||||
|
||||
|
||||
def extract_fudai_content(text: Optional[str]) -> str:
|
||||
"""从 OCR 文本中提取“福袋内容”相关描述。
|
||||
|
||||
当前实现比较宽松:
|
||||
- 优先截取 “福袋内容:xxx” 或 “内容:xxx” 之后的部分
|
||||
- 否则返回原始文本(去掉首尾空白)
|
||||
"""
|
||||
t = _safe_str(text).strip()
|
||||
if not t:
|
||||
return ""
|
||||
|
||||
# 常见前缀:福袋内容:xxx / 奖品内容:xxx / 内容:xxx
|
||||
m = re.search(r"(福袋内容|奖品内容|内容)[::]\s*(.+)", t)
|
||||
if m:
|
||||
return m.group(2).strip()
|
||||
|
||||
# 如果包含“福袋”关键字,简单返回整句,后续需要时可再细化
|
||||
if "福袋" in t:
|
||||
return t
|
||||
|
||||
return t
|
||||
|
||||
|
||||
def extract_fudai_countdown(text: Optional[str]) -> str:
|
||||
"""从 OCR 文本中提取倒计时字符串。
|
||||
|
||||
返回值示例:
|
||||
- ``"05:30"`` / ``"05:30后开奖"``
|
||||
- 若无法解析则返回空字符串。
|
||||
"""
|
||||
t = _safe_str(text)
|
||||
if not t:
|
||||
return ""
|
||||
|
||||
norm = normalize_countdown_text(t)
|
||||
return norm or ""
|
||||
|
||||
|
||||
def extract_fudai_value(text: Optional[str]) -> float:
|
||||
"""从 OCR 文本中提取福袋价值金额(元)。
|
||||
|
||||
直接复用 ``douyin_utils.extract_prize_value_yuan``。
|
||||
"""
|
||||
return float(extract_prize_value_yuan(_safe_str(text)))
|
||||
|
||||
|
||||
def extract_participants_count(text: Optional[str]) -> int:
|
||||
"""从 OCR 文本中提取“参与人数”。
|
||||
|
||||
支持的常见格式示例:
|
||||
- ``"共1份1173人已参与"``
|
||||
- ``"已有1000人参与"``
|
||||
- ``"1173人已参与"``
|
||||
- 若无法解析则返回 0。
|
||||
"""
|
||||
t = _safe_str(text)
|
||||
if not t:
|
||||
return 0
|
||||
|
||||
# 优先匹配“xxxx人已参与 / xxxx人参与 / xxxx人”
|
||||
m = re.search(r"(\d+)\s*人(?:已参与|参与)?", t)
|
||||
if m:
|
||||
try:
|
||||
return int(m.group(1))
|
||||
except ValueError:
|
||||
return 0
|
||||
|
||||
# 退化:查找所有数字,取中等大小的一个,避免把“份数”等误当成人数
|
||||
nums = [int(n) for n in re.findall(r"\d+", t) if n.isdigit()]
|
||||
if not nums:
|
||||
return 0
|
||||
# 简化策略:返回最大值(通常人数是文本里最大的数字)
|
||||
return max(nums)
|
||||
@@ -121,15 +121,4 @@ def start_tracing() -> None:
|
||||
_state["enabled"] = False
|
||||
|
||||
|
||||
def stop_tracing() -> None:
|
||||
try:
|
||||
sys.settrace(None)
|
||||
finally:
|
||||
_state["enabled"] = False
|
||||
|
||||
|
||||
def is_enabled() -> bool:
|
||||
return bool(_state.get("enabled"))
|
||||
|
||||
|
||||
__all__ = ["start_tracing", "stop_tracing", "is_enabled"]
|
||||
__all__ = ["start_tracing"]
|
||||
@@ -1,131 +0,0 @@
|
||||
"""douyin/utils/douyin_text_utils 的 pytest 单元测试。
|
||||
|
||||
覆盖福袋内容/倒计时/价值/参与人数 4 个 OCR 文本解析函数。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from douyin.utils.douyin_text_utils import (
|
||||
_safe_str,
|
||||
extract_fudai_content,
|
||||
extract_fudai_countdown,
|
||||
extract_fudai_value,
|
||||
extract_participants_count,
|
||||
)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# _safe_str
|
||||
# ============================================================
|
||||
|
||||
class TestSafeStr:
|
||||
@pytest.mark.parametrize(
|
||||
"value,expected",
|
||||
[
|
||||
(None, ""),
|
||||
("", ""),
|
||||
("abc", "abc"),
|
||||
(123, "123"),
|
||||
(0, "0"),
|
||||
],
|
||||
)
|
||||
def test_safe_str(self, value, expected):
|
||||
assert _safe_str(value) == expected
|
||||
|
||||
|
||||
# ============================================================
|
||||
# extract_fudai_content
|
||||
# ============================================================
|
||||
|
||||
class TestExtractFudaiContent:
|
||||
@pytest.mark.parametrize(
|
||||
"text,expected_substr",
|
||||
[
|
||||
("福袋内容:现金红包 100 元", "现金红包 100 元"),
|
||||
("奖品内容:iPhone 15", "iPhone 15"),
|
||||
("内容:手机一台", "手机一台"),
|
||||
# 全/半角冒号都支持
|
||||
("福袋内容: 现金", "现金"),
|
||||
],
|
||||
)
|
||||
def test_with_prefix(self, text, expected_substr):
|
||||
result = extract_fudai_content(text)
|
||||
assert expected_substr in result
|
||||
|
||||
def test_no_prefix_returns_original(self):
|
||||
text = "随便一段没有前缀的描述"
|
||||
# 没有前缀时返回原文(去首尾空白),不抛异常
|
||||
assert extract_fudai_content(text) == text.strip()
|
||||
|
||||
@pytest.mark.parametrize("value", ["", None])
|
||||
def test_empty(self, value):
|
||||
assert extract_fudai_content(value) == ""
|
||||
|
||||
|
||||
# ============================================================
|
||||
# extract_fudai_countdown
|
||||
# ============================================================
|
||||
|
||||
class TestExtractFudaiCountdown:
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
["", None],
|
||||
)
|
||||
def test_empty_returns_empty_string(self, value):
|
||||
assert extract_fudai_countdown(value) == ""
|
||||
|
||||
def test_returns_string_type(self):
|
||||
# 复杂文本走 normalize_countdown_text,结果应总是 str
|
||||
result = extract_fudai_countdown("剩余 05:30 开奖")
|
||||
assert isinstance(result, str)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# extract_fudai_value
|
||||
# ============================================================
|
||||
|
||||
class TestExtractFudaiValue:
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
["", None],
|
||||
)
|
||||
def test_empty_returns_zero(self, value):
|
||||
assert extract_fudai_value(value) == 0.0
|
||||
|
||||
def test_returns_float(self):
|
||||
# 不论输入是什么,返回值必须是 float
|
||||
result = extract_fudai_value("价值 100 元")
|
||||
assert isinstance(result, float)
|
||||
# 至少应该解析出一个非负数
|
||||
assert result >= 0.0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# extract_participants_count
|
||||
# ============================================================
|
||||
|
||||
class TestExtractParticipantsCount:
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
["", None],
|
||||
)
|
||||
def test_empty_returns_zero(self, value):
|
||||
assert extract_participants_count(value) == 0
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"text,expected",
|
||||
[
|
||||
("已1173人已参与", 1173),
|
||||
("已经500人参与", 500),
|
||||
("1173人已参与", 1173),
|
||||
("1000人参与", 1000),
|
||||
],
|
||||
)
|
||||
def test_extract_pattern(self, text, expected):
|
||||
assert extract_participants_count(text) == expected
|
||||
|
||||
def test_returns_int(self):
|
||||
result = extract_participants_count("100 人")
|
||||
assert isinstance(result, int)
|
||||
assert result >= 0
|
||||
@@ -74,7 +74,6 @@ CORE_MODULES = [
|
||||
"douyin.utils.douyin_config",
|
||||
"douyin.utils.douyin_helpers",
|
||||
"douyin.utils.douyin_utils",
|
||||
"douyin.utils.douyin_text_utils",
|
||||
"douyin.utils.ocr_preprocess",
|
||||
"douyin.utils.time_utils",
|
||||
"douyin.utils.param_usage_tracer",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""douyin/logging/performance_tracker.PerformanceTracker 的 pytest 单元测试。
|
||||
|
||||
覆盖:start_timer / end_timer / get_elapsed / track_function / track_step /
|
||||
get_statistics 共 6 个方法。
|
||||
覆盖:start_timer / end_timer / get_elapsed / track_step /
|
||||
get_statistics 共 5 个方法。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -67,41 +67,6 @@ class TestGetElapsed:
|
||||
assert tracker.get_elapsed(t) == 0.0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# track_function 装饰器
|
||||
# ============================================================
|
||||
|
||||
class TestTrackFunction:
|
||||
def test_wraps_and_returns_value(self, tracker):
|
||||
@tracker.track_function
|
||||
def add(a, b):
|
||||
return a + b
|
||||
|
||||
assert add(2, 3) == 5
|
||||
|
||||
def test_records_duration(self, tracker):
|
||||
@tracker.track_function
|
||||
def slow():
|
||||
time.sleep(0.01)
|
||||
|
||||
slow()
|
||||
stats = tracker.get_statistics()
|
||||
# 函数名 = "TestTrackFunction.test_records_duration.<locals>.slow"
|
||||
# 记录在以 qualname 为前缀的 key 上
|
||||
assert any("slow" in k for k in stats), f"未记录 slow 的统计:{stats}"
|
||||
|
||||
def test_exception_still_records(self, tracker):
|
||||
@tracker.track_function
|
||||
def raises():
|
||||
raise ValueError("boom")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
raises()
|
||||
stats = tracker.get_statistics()
|
||||
# 即使函数抛异常,时长也应被记录
|
||||
assert any("raises" in k for k in stats)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# track_step 装饰器
|
||||
# ============================================================
|
||||
|
||||
Reference in new issue
Block a user