darwin-cleaner 0.4.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ Metadata-Version: 2.4
2
+ Name: darwin-cleaner
3
+ Version: 0.4.0.dev0
4
+ Summary: 契约与规则驱动的单文件清洗引擎:读取 parquet → 分叉 → 按 order 跑 middleware → 返回数据与 meta
5
+ License: MIT
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: pandas>=2.0
10
+ Requires-Dist: pyarrow>=12
11
+ Provides-Extra: test
12
+ Requires-Dist: pytest>=8; extra == "test"
13
+ Dynamic: license-file
14
+
15
+ # darwin-cleaner
16
+
17
+ 契约与规则驱动的**单文件清洗引擎**:读取一个 parquet(或内存 DataFrame)→ 行分叉 / 列分叉 → 按 order 跑 middleware → 在内存中返回数据与 meta。
18
+
19
+ ## 边界
20
+
21
+ 本库只做数据变换,不做上下游事务:
22
+
23
+ | 库内 | 库外(上层应用) |
24
+ |---|---|
25
+ | 读单个 parquet / 接收 DataFrame | 多文件合并、文件发现、新鲜度判定 |
26
+ | 行分叉 / 列分叉 | 加载 `cleaner.json` / `clean.d/*.json` / adapter 配置 |
27
+ | 12 个内置 middleware(含默认关闭的 `dedupe`) | 写 parquet / csv / meta / 断点 |
28
+ | 契约与规则校验、统计与报告生成 | CLI、调度、退出码、业务标识 |
29
+
30
+ 详细设计见 [docs/design.md](docs/design.md),现有代码梳理见 [docs/clean阶段代码逻辑梳理.md](docs/clean阶段代码逻辑梳理.md)。
31
+
32
+ ## 安装
33
+
34
+ ```bash
35
+ pip install -e ".[test]"
36
+ ```
37
+
38
+ 依赖:`pandas>=2.0`、`pyarrow>=12`;Python >= 3.10。
39
+
40
+ ## 快速开始
41
+
42
+ ```python
43
+ from pathlib import Path
44
+ from darwin_cleaner import (
45
+ CleaningEngine, CleanRequest, CleanOptions, Contract, FieldSpec,
46
+ MiddlewareOverride, MiddlewareRegistryImpl, Rules, TypeSpec,
47
+ )
48
+
49
+ registry = MiddlewareRegistryImpl()
50
+ registry.builtins() # 12 个内置步骤,dedupe 默认关闭
51
+
52
+ contract = Contract(
53
+ name="stk_ohlcv",
54
+ fields={
55
+ "symbol": FieldSpec(name="symbol", type="str"),
56
+ "trade_date": FieldSpec(name="trade_date", type="str"),
57
+ "close": FieldSpec(name="close", type="float"),
58
+ },
59
+ types={"float": TypeSpec(name="float", converter="numeric", min=0)},
60
+ output=("symbol", "trade_date", "close"),
61
+ keys=("symbol", "trade_date"), # 参与 keys 的列自动 required
62
+ )
63
+
64
+ engine = CleaningEngine(registry)
65
+ result = engine.clean(CleanRequest(
66
+ input=Path("raw.parquet"),
67
+ contracts={"stk_ohlcv": contract},
68
+ rules=Rules(),
69
+ options=CleanOptions(
70
+ middleware_overrides={
71
+ "dedupe": MiddlewareOverride(
72
+ enabled=True,
73
+ params={"scope": "keys", "strategy": "last"},
74
+ ),
75
+ },
76
+ ),
77
+ ))
78
+
79
+ for out in result.outputs:
80
+ if out.status == "success":
81
+ df = out.data # pandas.DataFrame
82
+ meta = out.meta # 内容 hash + 规则指纹 + 统计
83
+ report = out.report # 过程审计
84
+ # 写盘由上层负责
85
+ ```
86
+
87
+ ## 核心概念
88
+
89
+ - **Contract**:字段、类型、输出列、唯一行键、排序。由上层解析配置后注入,引擎不读配置文件。
90
+ - **FieldSpec.required**:三态。`True` 缺失即报错;`False` 显式允许缺失(补空列);`None` 未声明时由引擎推导——参与 `sort` / `keys` 的列自动为 `True`,其余为 `False`。
91
+ - **Rules**:`mapping` / `value_map` / `transform` / `filters` / `outputs` 等,替代 middleware 内的全局配置读取。
92
+ - **Middleware**:统一签名 `(df, ctx, params) -> df | None`。`order` 决定执行顺序,`params` 是声明式参数(计入规则指纹),`enabled` 是开关;`MiddlewareOverride` 可按请求覆盖。
93
+ - **order 分段**:`10-19` 预处理、`20-29` dtype、`30-39` 填充、`40-49` 公式、`50-69` 校验/去重、`90-99` 输出。
94
+
95
+ ## 内置 middleware
96
+
97
+ | order | 名称 | 作用 | 默认 |
98
+ |---|---|---|---|
99
+ | 10 | `field_mapping` | 字段改名 | 开 |
100
+ | 12 | `value_mapping` | 值替换 | 开 |
101
+ | 15 | `field_transform` | 常量 / 系数 / 动作 | 开 |
102
+ | 20 | `type_conversion` | dtype 归一 | 开 |
103
+ | 30 | `fill` | 分组 ffill | 开 |
104
+ | 40 | `formula_compute` | 全空列按公式计算 | 开 |
105
+ | 45 | `formula_validate` | 双容差公式校验 | 开 |
106
+ | 50 | `consistency` | 字段间一致性 | 开 |
107
+ | 60 | `type_validate` | inf / min / max 越界置空 | 开 |
108
+ | 70 | `dedupe` | 按 keys 去重 | **关** |
109
+ | 90 | `output` | 按契约选列 | 开 |
110
+ | 95 | `sort` | 按契约排序 | 开 |
111
+
112
+ ## 测试
113
+
114
+ ```bash
115
+ pytest
116
+ ```
117
+
118
+ ## 许可证
119
+
120
+ MIT
@@ -0,0 +1,15 @@
1
+ darwin_cleaner/__init__.py,sha256=1NogeLZZfcBEiIFawMAQiWRuGgwJyz9-WZGQ6lNVtLI,2456
2
+ darwin_cleaner/context.py,sha256=C9S5X0KHJS4c5CM7VPPg-rR3PiRa8rgwXZ8jcaa2O-Q,1239
3
+ darwin_cleaner/contract.py,sha256=3Wy3-09gsNB8H6R38ciJsMvIKML6GHSP7cQaml6pGOI,3932
4
+ darwin_cleaner/engine.py,sha256=Nw8uz9RztffEphF8mUdpD7ltBZHrd1wfugIJKzPf79c,14803
5
+ darwin_cleaner/exceptions.py,sha256=cCDx6tAUrWVkBV8MSsm5ZZJNdeFxmSmTNNCzGkAZkVU,1328
6
+ darwin_cleaner/middleware.py,sha256=aHioXdw_BYROABicS96lm17Wm21PXGDQNPlALdNKA1E,10765
7
+ darwin_cleaner/request.py,sha256=8ixVzx7MnDbw7LL-94InKUDJdf8UAG_416SfTafHhrw,2134
8
+ darwin_cleaner/results.py,sha256=pXt8K-QypqZvZXQKGxOXdyfoLnQsc9qlfUoNmXI2-gY,7400
9
+ darwin_cleaner/rules.py,sha256=sgTTIFnGvfXjhGNSyaMc_5Ra4NV5CjyySL-cl4Waebo,2910
10
+ darwin_cleaner/steps.py,sha256=bG2dgK-jzKZIbfZ3-9r4qQwDNlggByLZJmm_FXd3p4c,16875
11
+ darwin_cleaner-0.4.0.dev0.dist-info/licenses/LICENSE,sha256=Jv2rYe7HuP4ueMkCFHFFWd9J4Cp6zKPZAi9VQWomW74,1068
12
+ darwin_cleaner-0.4.0.dev0.dist-info/METADATA,sha256=jItcXUz54RXuVG14ZX6jgLdo1SZwnrz9o5145V6G72U,4468
13
+ darwin_cleaner-0.4.0.dev0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
14
+ darwin_cleaner-0.4.0.dev0.dist-info/top_level.txt,sha256=81uF1TQwFp__8e8Y7tL908n5Tlp4PH14f86zx5SeIVw,15
15
+ darwin_cleaner-0.4.0.dev0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 darwinalpha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ darwin_cleaner