refactor: 抽出 core/(clock store text numeric),小学版与初中版共用归一化与数值解析
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
# core —— 两个产品共用的纯函数
|
||||
|
||||
这里只放**与业务无关**的纯函数:给定输入就有确定输出,不读数据库、不知道「错题」「毕业」「家长」这些概念。
|
||||
|
||||
一旦某个函数需要知道业务(比如「连续对两次该怎么改状态」),它就**不该**放在这里——留在各自的 `backend/`(小学版)或 `junior/`(初中版)。
|
||||
|
||||
## 有什么
|
||||
|
||||
| 文件 | 内容 | 谁在用 |
|
||||
|---|---|---|
|
||||
| `clock.py` | `now()` / `today()` / `add_days()` / `new_id()`,统一 +08:00 | 两个版本的 db 与 services |
|
||||
| `store.py` | `open_db()`(`isolation_level=None`)、`row_to_dict()` / `rows_to_list()`、`ensure_columns()`(老库补列) | 两个版本的 db |
|
||||
| `text.py` | `halfwidth()` 全角转半角、`squeeze()` 去空白、`strip_punct()` / `strip_units()` / `strip_tail()`、`has_chinese()` | 两个版本的 judging |
|
||||
| `numeric.py` | `eval_exact()` 用 `Fraction` 精确算四则、`to_qty()` 认 `3/4` `1又1/2` `0.75`、`fmt_qty()` 输出 `3/10` | 两个版本的 judging |
|
||||
|
||||
## 为什么只有这几样
|
||||
|
||||
`backend/judging.py` 与 `junior/judging.py` 之前是复制粘贴:同一段 `normalize`、同一个 `safe_eval`、同一套分数解析,改一处要改两处,一不留神就漂。所以真正共享的只有**归一化与数值**这两层。
|
||||
|
||||
**有意不抽的**:
|
||||
|
||||
- `normalize()` 本身。小学版连标点一起去掉(`83。` 与 `83` 视为同一个答案);初中版保留标点(语文默写要逐字比)。策略不同,只共享零件。
|
||||
- 单位表。小学是元/角/分/米/厘米;初中是 m/s、kg/m³、Ω、℃。放一起会互相污染。
|
||||
- `log_event()`。两个表的列名不同(`child_id` vs `student_id`),而且各自要记的事件也不同。
|
||||
- 调度器(`scheduler.py`)。小学版是「连对两次 + 讲清」,初中版是「连对两次 + 变式 + 讲清」,状态机不同。
|
||||
- `services.py`。业务本来就该分开。
|
||||
|
||||
## 加东西的判断标准
|
||||
|
||||
问三个问题,都是「是」才放进来:
|
||||
|
||||
1. 两个产品都会用、且用法完全一样?
|
||||
2. 不依赖任何业务概念(不许出现 mistake / guardian / graduated)?
|
||||
3. 单独写测试就能验证(不需要建库、不需要起服务)?
|
||||
|
||||
改完记得跑两边的测试:
|
||||
|
||||
```powershell
|
||||
python -m unittest tests.test_backend tests.test_ocr tests.test_choice tests.test_scan tests.test_kp tests.test_ocr_http tests.test_hardening tests.test_scan_flow tests.test_frac tests.test_junior
|
||||
python tests/smoke_http.py; python tests/smoke_junior.py
|
||||
```
|
||||
Reference in New Issue
Block a user