"""同一账号同时学习的课程数。默认 1,最多 8。""" from __future__ import annotations DEFAULT = 1 MAX = 8 class CourseWorkersError(ValueError): """同时学习课程数不符合要求。""" def validate(value: object) -> int: if not isinstance(value, int) or isinstance(value, bool) or not DEFAULT <= value <= MAX: raise CourseWorkersError(f"同时学习课程数须为 {DEFAULT} 到 {MAX} 的整数") return value def apply(account, value: object) -> None: account.course_workers = validate(value) def of(account) -> int: raw = int(getattr(account, "course_workers", DEFAULT) or DEFAULT) return max(DEFAULT, min(MAX, raw)) def view_of(account) -> dict: workers = of(account) return {"workers": workers, "max": MAX, "recommended": workers == DEFAULT}