refactor: --id 改为真实 ID 列,新增 --sn 对应总排号
此前接口 --id 实际按总排号列查询,与数据库真实 ID 字段语义混淆。
现明确区分两种键类型:
- --id -> 数据库真实 ID 列(config id_field)
- --sn -> 总排号列(config id_column,即原先 --id 的语义)
- --ids-file 视为总排号(键类型 sn)
db.py: fetch_params_by_ids 改为接受 (标识符, 键类型) 列表,返回
{标识符: {param, sn}};id 键回取对应总排号;新增 id_field 配置读取
classifier.py: classify_batch/classify_single 透传键类型,输出 zong_pai_hao
一律为回查到的总排号,not_found 时回退输入标识符便于追溯
main.py / write_attachments.py: 新增 --sn,--id 改指真实 ID,二者可混用;
全表扫描回退仍按总排号(sn)查询
README.md: 对齐 --id/--sn 语义并补充 id_field 配置说明
注:config.yaml 含密钥被 .gitignore 忽略,id_field 仅存于本地配置;
db.py 在未配置 id_field 时优雅降级为按总排号查询并告警。
Co-Authored-By: WorkBuddy <workbuddy@tencent.com>
This commit is contained in:
@@ -13,11 +13,19 @@
|
||||
python write_attachments.py # 全量写入
|
||||
python write_attachments.py --limit 10 # 仅前 10 个总排号(测试)
|
||||
python write_attachments.py --ids-file ids.txt # 指定总排号(文件,每行一个)
|
||||
python write_attachments.py --id 26B742 # 单个总排号写入
|
||||
python write_attachments.py --id 26B742,26B743 # 多个总排号(逗号分隔)写入
|
||||
python write_attachments.py --sn 26B742 # 单个总排号写入(键类型 sn)
|
||||
python write_attachments.py --sn 26B742,26B743 # 多个总排号(逗号分隔)写入
|
||||
python write_attachments.py --id 802 # 单个数据库真实 ID 写入(键类型 id)
|
||||
python write_attachments.py --id 802,803 # 多个真实 ID(逗号分隔)写入
|
||||
python write_attachments.py --mode coarse # 粗分类写入
|
||||
python write_attachments.py --dry-run # 只打印将写入的行,不落库
|
||||
python write_attachments.py --config other.yaml
|
||||
|
||||
指定方式(--id / --sn / --ids-file 可混用):
|
||||
--id 数据库真实 ID 列(id_field),如 802
|
||||
--sn 总排号列(id_column),如 26B742(即原先 --id 的语义)
|
||||
--ids-file 每行一个总排号(键类型 sn)
|
||||
无论用哪种方式,结果均按回查到的总排号写入 Common.Attachment.NS。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -75,23 +83,28 @@ def convert_results_to_rows(results: list[dict], mode: str) -> tuple[list[tuple[
|
||||
return rows, skipped
|
||||
|
||||
|
||||
def _parse_ids(args: argparse.Namespace) -> list[str]:
|
||||
"""从 --id(逗号分隔,单个或多个)或 --ids-file 解析出总排号列表,去除空白项。
|
||||
def _parse_ids(args: argparse.Namespace) -> list[tuple[str, str]]:
|
||||
"""从 --id / --sn / --ids-file 解析出 (标识符, 键类型) 列表。
|
||||
|
||||
两个来源可同时提供,合并后返回(顺序:先 --id,后 --ids-file)。
|
||||
--id -> 键类型 "id"(数据库真实 ID 列)
|
||||
--sn -> 键类型 "sn"(总排号列,即原先 --id 的语义)
|
||||
--ids-file -> 每行一个总排号,键类型 "sn"
|
||||
三者可同时提供、合并后返回(顺序:--id, --sn, --ids-file)。
|
||||
--ids-file 文件不存在时直接报错退出。
|
||||
"""
|
||||
ids: list[str] = []
|
||||
items: list[tuple[str, str]] = []
|
||||
if args.id:
|
||||
ids.extend(s.strip() for s in args.id.split(",") if s.strip())
|
||||
items.extend((s.strip(), "id") for s in args.id.split(",") if s.strip())
|
||||
if args.sn:
|
||||
items.extend((s.strip(), "sn") for s in args.sn.split(",") if s.strip())
|
||||
if args.ids_file:
|
||||
p = Path(args.ids_file)
|
||||
if not p.exists():
|
||||
print(f"[错误] 总排号文件不存在: {p.resolve()}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
with p.open("r", encoding="utf-8") as f:
|
||||
ids.extend(line.strip() for line in f if line.strip())
|
||||
return ids
|
||||
items.extend((line.strip(), "sn") for line in f if line.strip())
|
||||
return items
|
||||
|
||||
|
||||
def main() -> None:
|
||||
@@ -103,16 +116,21 @@ def main() -> None:
|
||||
parser.add_argument("--config", type=str, default="config.yaml")
|
||||
parser.add_argument(
|
||||
"--id", type=str, default=None,
|
||||
help="总排号,单个或逗号分隔的多个,例如: 26B742 或 26B742,26B743;"
|
||||
"提供后直接按指定总排号写入,不再全表扫描(--limit 此时无效)",
|
||||
help="数据库真实 ID(如 802),单个或逗号分隔的多个;提供后按真实 ID 列查询,"
|
||||
"不再全表扫描(--limit 此时无效)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--sn", type=str, default=None,
|
||||
help="总排号(如 26B742),单个或逗号分隔的多个(即原先 --id 的语义);"
|
||||
"提供后直接按总排号写入,不再全表扫描(--limit 此时无效)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--limit", type=int, default=None,
|
||||
help="仅处理前 N 个总排号(按总排号排序),测试用;与 --id/--ids-file 互斥",
|
||||
help="仅处理前 N 个总排号(按总排号排序),测试用;与 --id/--sn/--ids-file 互斥",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--ids-file", type=str, default=None,
|
||||
help="包含总排号的文本文件路径,每行一个总排号;可单独使用或与 --id 合并",
|
||||
help="包含总排号的文本文件路径,每行一个总排号(键类型 sn);可单独使用或与 --id/--sn 合并",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--mode", type=str, default=None, choices=["coarse", "fine"],
|
||||
@@ -147,14 +165,18 @@ def main() -> None:
|
||||
)
|
||||
|
||||
# 1) 取总排号列表
|
||||
if args.id or args.ids_file:
|
||||
if args.id or args.sn or args.ids_file:
|
||||
ids = _parse_ids(args)
|
||||
if not ids:
|
||||
print("[错误] --id / --ids-file 未解析到任何有效的总排号", file=sys.stderr)
|
||||
print("[错误] --id / --sn / --ids-file 未解析到任何有效的标识符", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
logger.info("从 --id / --ids-file 读取 %d 个总排号", len(ids))
|
||||
n_id = sum(1 for _, k in ids if k == "id")
|
||||
n_sn = sum(1 for _, k in ids if k == "sn")
|
||||
logger.info("从 --id/--sn/--ids-file 读取 %d 个标识符(id=%d, sn=%d)", len(ids), n_id, n_sn)
|
||||
else:
|
||||
ids = fetch_all_ids(cfg["database"], limit=args.limit)
|
||||
sn_list = fetch_all_ids(cfg["database"], limit=args.limit)
|
||||
# 全表扫描得到的是总排号,键类型统一为 sn
|
||||
ids = [(s, "sn") for s in sn_list]
|
||||
logger.info(
|
||||
"源表读取 %d 个总排号%s", len(ids),
|
||||
f"(limit={args.limit})" if args.limit else "",
|
||||
@@ -169,7 +191,7 @@ def main() -> None:
|
||||
results = classify_batch(
|
||||
db_cfg=cfg["database"],
|
||||
llm_cfg=cfg["llm"],
|
||||
zong_pai_hao_list=ids,
|
||||
id_list=ids,
|
||||
max_workers=cfg["business"]["max_workers"],
|
||||
mode=mode,
|
||||
log_dir=log_dir,
|
||||
|
||||
Reference in New Issue
Block a user