版本 0.2.2 最低支持核心版本: >=0.5.0
须与
module_info.yaml一致。
本文档是本模块公开调用面的唯一人读 API 文档。
core 仍为0.x:公开入口状态最高beta(禁止stable)。
所列门面入口须有__test__/test_api.py覆盖。
快速开始见 QUICKSTART.md。术语见 glossary.yaml。架构见 ARCHITECTURE.md。
公开约定: 包根仅导出 Utils;周期常量可从 contracts.py 导入。实现位于 core/,禁止 deep-import。
Utils
描述: 通用无业务工具门面 — date / types / io / math / markdown / locale
date
Utils.date 绑定内部 DateUtils 类(全部 @staticmethod)。
常用:
| 方法 | 说明 |
today() | 今日 |
normalize_str | 解析与格式化 |
add_days | 日级运算 |
is_before | 比较 |
to_period_str | 周期运算 |
get_query_date_range_min | 无界查询日期范围 |
PERIOD_DAY | 周期常量 |
业务默认开始日请用
ProjectContext.config.get_default_start_date(),不在本门面提供。
- 状态:beta
- 举例:
from core.infra.utils import Utils
Utils.date.normalize_str("2024-01-15") # "20240115"
Utils.date.diff_days("20240101", "20240116")
types
Utils.types 绑定内部 TypeUtils:类型判断、deep_merge / deep_diff、DataFrame 薄封装。
- 状态:beta
io
| 方法 | 说明 |
write_dicts_to_csv | List[dict] ↔ CSV |
dicts_to_csv_bytes | 内存 CSV |
write_archive | zip / tar.gz / csv |
- 状态:beta
math
Utils.math.deterministic_unit_float(*key_parts) -> float
- 状态:beta
- 描述: SHA-256 派生的确定性
[0,1)伪随机
markdown
Utils.markdown 绑定 MarkdownMgr:MD 模版 {{:token}} 填充。
| 方法 | 说明 |
load_template(path) | 从文件加载模版,扫描 token |
from_text(text) | 从字符串加载模版 |
fill(token, content) | 填值(同名后写覆盖;非 str → |
save(path) | 未填 token 报错;写出 MD |
clear() | 清空已填值 |
- 状态:beta
- 举例:
from core.infra.utils import Utils
mgr = Utils.markdown.load_template("REPORT_TEMPLATE.md")
mgr.fill("wall_clock_seconds", "3s")
mgr.save("out/REPORT.md")
locale
Utils.locale.is_china() -> bool(同 Utils.is_china())
- 类型:static
- 状态:beta
- 引入版本:
0.2.1 - 描述: 本机是否在中国大陆。只认大陆时区地名(
Asia/Shanghai等)或zh_CN,不用 UTC+8(Perth / Singapore 为假)。不含网络探测。 - 举例:
from core.infra.utils import Utils
if Utils.locale.is_china():
...
# 或 Utils.is_china()
pkg
Utils.pkg.use_china_mirror() -> bool
Utils.pkg.pip_args() -> list[str]
Utils.pkg.run_pip(pip_argv, *, cwd=None, env=None, python=None) -> int
Utils.pkg.npm_env(base: Mapping[str, str] | None = None) -> dict
Utils.pkg.announce() -> None
- 类型:static
- 状态:beta
- 引入版本:
0.2.2 - 描述: pip 与 npm 拉外部依赖的唯一网络策略。优先级:
USE_CHINA_MIRROR=1/0→Utils.is_china()→ pypi.org 两秒探不通则镜像。国内:pip 默认中科大(run_pip失败会换清华 / 阿里云 / 官方)、npm npmmirror。pip_args含超时;npm_env写入npm_config_registry与 fetch 超时。 - 举例:
from core.infra.utils import Utils
Utils.pkg.announce()
code = Utils.pkg.run_pip(["install", "-r", "requirements.txt"])
npm_env = Utils.pkg.npm_env()
contracts
| 符号 | 说明 |
PERIOD_DAY | 周期类型字符串常量 |
PeriodType | Literal 别名 |