dqflow 0.1.0__tar.gz
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.
- dqflow-0.1.0/.claude/settings.local.json +10 -0
- dqflow-0.1.0/.gitignore +44 -0
- dqflow-0.1.0/LICENSE +21 -0
- dqflow-0.1.0/PKG-INFO +146 -0
- dqflow-0.1.0/README.md +114 -0
- dqflow-0.1.0/examples/orders.yaml +33 -0
- dqflow-0.1.0/pyproject.toml +84 -0
- dqflow-0.1.0/src/dqflow/__init__.py +8 -0
- dqflow-0.1.0/src/dqflow/cli.py +130 -0
- dqflow-0.1.0/src/dqflow/column.py +25 -0
- dqflow-0.1.0/src/dqflow/contract.py +102 -0
- dqflow-0.1.0/src/dqflow/engines/__init__.py +6 -0
- dqflow-0.1.0/src/dqflow/engines/base.py +19 -0
- dqflow-0.1.0/src/dqflow/engines/pandas.py +179 -0
- dqflow-0.1.0/src/dqflow/result.py +68 -0
- dqflow-0.1.0/tests/__init__.py +1 -0
- dqflow-0.1.0/tests/conftest.py +56 -0
- dqflow-0.1.0/tests/test_cli.py +141 -0
- dqflow-0.1.0/tests/test_column.py +51 -0
- dqflow-0.1.0/tests/test_contract.py +137 -0
dqflow-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# Distribution / packaging
|
|
7
|
+
build/
|
|
8
|
+
dist/
|
|
9
|
+
*.egg-info/
|
|
10
|
+
*.egg
|
|
11
|
+
.eggs/
|
|
12
|
+
|
|
13
|
+
# Virtual environments
|
|
14
|
+
venv/
|
|
15
|
+
.venv/
|
|
16
|
+
env/
|
|
17
|
+
|
|
18
|
+
# IDE
|
|
19
|
+
.idea/
|
|
20
|
+
.vscode/
|
|
21
|
+
*.swp
|
|
22
|
+
*.swo
|
|
23
|
+
|
|
24
|
+
# Testing
|
|
25
|
+
.pytest_cache/
|
|
26
|
+
.coverage
|
|
27
|
+
htmlcov/
|
|
28
|
+
.tox/
|
|
29
|
+
.nox/
|
|
30
|
+
|
|
31
|
+
# mypy
|
|
32
|
+
.mypy_cache/
|
|
33
|
+
|
|
34
|
+
# ruff
|
|
35
|
+
.ruff_cache/
|
|
36
|
+
|
|
37
|
+
# OS
|
|
38
|
+
.DS_Store
|
|
39
|
+
Thumbs.db
|
|
40
|
+
|
|
41
|
+
# Local dev
|
|
42
|
+
*.local
|
|
43
|
+
.env
|
|
44
|
+
.env.*
|
dqflow-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Quang
|
|
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.
|
dqflow-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dqflow
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lightweight, contract-first data quality engine for modern data pipelines
|
|
5
|
+
Project-URL: Homepage, https://github.com/quang/dqflow
|
|
6
|
+
Project-URL: Repository, https://github.com/quang/dqflow
|
|
7
|
+
Project-URL: Issues, https://github.com/quang/dqflow/issues
|
|
8
|
+
Author-email: Quang <nguyenvanquang247@gmail.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: data-contracts,data-quality,data-validation,etl,pandas
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Requires-Dist: click>=8.0
|
|
23
|
+
Requires-Dist: pandas>=1.5.0
|
|
24
|
+
Requires-Dist: pyyaml>=6.0
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: mypy>=1.0; extra == 'dev'
|
|
27
|
+
Requires-Dist: pre-commit>=3.0; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
30
|
+
Requires-Dist: ruff>=0.1.0; extra == 'dev'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# dqflow
|
|
34
|
+
|
|
35
|
+
**dqflow** is a lightweight, contract-first data quality engine for modern data pipelines.
|
|
36
|
+
|
|
37
|
+
Define explicit expectations for your data (schema, validity, freshness) and **fail fast** when data breaks — before bad data reaches downstream systems.
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Why dqflow?
|
|
42
|
+
|
|
43
|
+
Data quality issues are inevitable — silent failures are not.
|
|
44
|
+
|
|
45
|
+
Most teams rely on ad-hoc checks, fragile assertions, or heavyweight frameworks that are hard to maintain. dqflow takes a different approach:
|
|
46
|
+
|
|
47
|
+
* **Contracts over checks** — expectations are explicit and versionable
|
|
48
|
+
* **Pipeline-first** — designed for ETL, ELT, and streaming workflows
|
|
49
|
+
* **Lightweight & Pythonic** — minimal API, easy to embed
|
|
50
|
+
* **Fail fast** — break pipelines intentionally, not silently
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## Quick example
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from dqflow import Contract, Column
|
|
58
|
+
|
|
59
|
+
orders = Contract(
|
|
60
|
+
name="orders",
|
|
61
|
+
columns={
|
|
62
|
+
"order_id": Column(str, not_null=True),
|
|
63
|
+
"amount": Column(float, min=0),
|
|
64
|
+
"currency": Column(str, allowed=["USD", "EUR"]),
|
|
65
|
+
"created_at": Column("timestamp", freshness_minutes=60),
|
|
66
|
+
},
|
|
67
|
+
rules=[
|
|
68
|
+
"row_count > 1000",
|
|
69
|
+
"null_rate(amount) < 0.01",
|
|
70
|
+
],
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
result = orders.validate(df)
|
|
74
|
+
|
|
75
|
+
if not result.ok:
|
|
76
|
+
raise Exception(result.summary())
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Features (v0.1 scope)
|
|
82
|
+
|
|
83
|
+
* Contract-as-code (Python & YAML)
|
|
84
|
+
* Column-level checks
|
|
85
|
+
|
|
86
|
+
* type validation
|
|
87
|
+
* not null
|
|
88
|
+
* min / max
|
|
89
|
+
* allowed values
|
|
90
|
+
* Table-level checks
|
|
91
|
+
|
|
92
|
+
* row count
|
|
93
|
+
* freshness
|
|
94
|
+
* Structured validation results (JSON-friendly)
|
|
95
|
+
* Pandas engine
|
|
96
|
+
* CLI support
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## CLI usage
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
dq validate contracts/orders.yaml data/orders.parquet
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## Supported engines
|
|
109
|
+
|
|
110
|
+
* ✅ Pandas
|
|
111
|
+
* 🚧 PySpark (planned)
|
|
112
|
+
* 🚧 SQL tables (planned)
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## Philosophy
|
|
117
|
+
|
|
118
|
+
* **Explicit is better than implicit**
|
|
119
|
+
* **Bad data should break pipelines early**
|
|
120
|
+
* **Quality rules are part of your system design**
|
|
121
|
+
|
|
122
|
+
> dqflow is not a full data observability platform.
|
|
123
|
+
> It is a small, opinionated library meant to be embedded directly into pipelines.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Roadmap
|
|
128
|
+
|
|
129
|
+
* PySpark engine
|
|
130
|
+
* dbt / dlt integrations
|
|
131
|
+
* Incremental & backfill-aware validation
|
|
132
|
+
* Metrics export (Prometheus-compatible)
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
MIT
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## Status
|
|
143
|
+
|
|
144
|
+
🚧 Early development (v0.1.0)
|
|
145
|
+
|
|
146
|
+
APIs may change. Feedback and contributions are welcome.
|
dqflow-0.1.0/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# dqflow
|
|
2
|
+
|
|
3
|
+
**dqflow** is a lightweight, contract-first data quality engine for modern data pipelines.
|
|
4
|
+
|
|
5
|
+
Define explicit expectations for your data (schema, validity, freshness) and **fail fast** when data breaks — before bad data reaches downstream systems.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Why dqflow?
|
|
10
|
+
|
|
11
|
+
Data quality issues are inevitable — silent failures are not.
|
|
12
|
+
|
|
13
|
+
Most teams rely on ad-hoc checks, fragile assertions, or heavyweight frameworks that are hard to maintain. dqflow takes a different approach:
|
|
14
|
+
|
|
15
|
+
* **Contracts over checks** — expectations are explicit and versionable
|
|
16
|
+
* **Pipeline-first** — designed for ETL, ELT, and streaming workflows
|
|
17
|
+
* **Lightweight & Pythonic** — minimal API, easy to embed
|
|
18
|
+
* **Fail fast** — break pipelines intentionally, not silently
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Quick example
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from dqflow import Contract, Column
|
|
26
|
+
|
|
27
|
+
orders = Contract(
|
|
28
|
+
name="orders",
|
|
29
|
+
columns={
|
|
30
|
+
"order_id": Column(str, not_null=True),
|
|
31
|
+
"amount": Column(float, min=0),
|
|
32
|
+
"currency": Column(str, allowed=["USD", "EUR"]),
|
|
33
|
+
"created_at": Column("timestamp", freshness_minutes=60),
|
|
34
|
+
},
|
|
35
|
+
rules=[
|
|
36
|
+
"row_count > 1000",
|
|
37
|
+
"null_rate(amount) < 0.01",
|
|
38
|
+
],
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
result = orders.validate(df)
|
|
42
|
+
|
|
43
|
+
if not result.ok:
|
|
44
|
+
raise Exception(result.summary())
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Features (v0.1 scope)
|
|
50
|
+
|
|
51
|
+
* Contract-as-code (Python & YAML)
|
|
52
|
+
* Column-level checks
|
|
53
|
+
|
|
54
|
+
* type validation
|
|
55
|
+
* not null
|
|
56
|
+
* min / max
|
|
57
|
+
* allowed values
|
|
58
|
+
* Table-level checks
|
|
59
|
+
|
|
60
|
+
* row count
|
|
61
|
+
* freshness
|
|
62
|
+
* Structured validation results (JSON-friendly)
|
|
63
|
+
* Pandas engine
|
|
64
|
+
* CLI support
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## CLI usage
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
dq validate contracts/orders.yaml data/orders.parquet
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Supported engines
|
|
77
|
+
|
|
78
|
+
* ✅ Pandas
|
|
79
|
+
* 🚧 PySpark (planned)
|
|
80
|
+
* 🚧 SQL tables (planned)
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Philosophy
|
|
85
|
+
|
|
86
|
+
* **Explicit is better than implicit**
|
|
87
|
+
* **Bad data should break pipelines early**
|
|
88
|
+
* **Quality rules are part of your system design**
|
|
89
|
+
|
|
90
|
+
> dqflow is not a full data observability platform.
|
|
91
|
+
> It is a small, opinionated library meant to be embedded directly into pipelines.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## Roadmap
|
|
96
|
+
|
|
97
|
+
* PySpark engine
|
|
98
|
+
* dbt / dlt integrations
|
|
99
|
+
* Incremental & backfill-aware validation
|
|
100
|
+
* Metrics export (Prometheus-compatible)
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
MIT
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## Status
|
|
111
|
+
|
|
112
|
+
🚧 Early development (v0.1.0)
|
|
113
|
+
|
|
114
|
+
APIs may change. Feedback and contributions are welcome.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
name: orders
|
|
2
|
+
description: E-commerce order data quality contract
|
|
3
|
+
|
|
4
|
+
columns:
|
|
5
|
+
order_id:
|
|
6
|
+
type: string
|
|
7
|
+
not_null: true
|
|
8
|
+
|
|
9
|
+
customer_id:
|
|
10
|
+
type: string
|
|
11
|
+
not_null: true
|
|
12
|
+
|
|
13
|
+
amount:
|
|
14
|
+
type: float
|
|
15
|
+
min: 0
|
|
16
|
+
max: 100000
|
|
17
|
+
|
|
18
|
+
currency:
|
|
19
|
+
type: string
|
|
20
|
+
allowed: ["USD", "EUR", "GBP"]
|
|
21
|
+
|
|
22
|
+
status:
|
|
23
|
+
type: string
|
|
24
|
+
allowed: ["pending", "processing", "shipped", "delivered", "cancelled"]
|
|
25
|
+
|
|
26
|
+
created_at:
|
|
27
|
+
type: timestamp
|
|
28
|
+
freshness_minutes: 1440 # 24 hours
|
|
29
|
+
|
|
30
|
+
rules:
|
|
31
|
+
- row_count > 0
|
|
32
|
+
- null_rate(amount) < 0.01
|
|
33
|
+
- null_rate(customer_id) < 0.001
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dqflow"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Lightweight, contract-first data quality engine for modern data pipelines"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Quang", email = "nguyenvanquang247@gmail.com" },
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"data-quality",
|
|
17
|
+
"data-validation",
|
|
18
|
+
"data-contracts",
|
|
19
|
+
"etl",
|
|
20
|
+
"pandas",
|
|
21
|
+
]
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 3 - Alpha",
|
|
24
|
+
"Intended Audience :: Developers",
|
|
25
|
+
"License :: OSI Approved :: MIT License",
|
|
26
|
+
"Programming Language :: Python :: 3",
|
|
27
|
+
"Programming Language :: Python :: 3.9",
|
|
28
|
+
"Programming Language :: Python :: 3.10",
|
|
29
|
+
"Programming Language :: Python :: 3.11",
|
|
30
|
+
"Programming Language :: Python :: 3.12",
|
|
31
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
32
|
+
]
|
|
33
|
+
dependencies = [
|
|
34
|
+
"pandas>=1.5.0",
|
|
35
|
+
"pyyaml>=6.0",
|
|
36
|
+
"click>=8.0",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[project.optional-dependencies]
|
|
40
|
+
dev = [
|
|
41
|
+
"pytest>=7.0",
|
|
42
|
+
"pytest-cov>=4.0",
|
|
43
|
+
"ruff>=0.1.0",
|
|
44
|
+
"mypy>=1.0",
|
|
45
|
+
"pre-commit>=3.0",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[project.scripts]
|
|
49
|
+
dq = "dqflow.cli:main"
|
|
50
|
+
|
|
51
|
+
[project.urls]
|
|
52
|
+
Homepage = "https://github.com/quang/dqflow"
|
|
53
|
+
Repository = "https://github.com/quang/dqflow"
|
|
54
|
+
Issues = "https://github.com/quang/dqflow/issues"
|
|
55
|
+
|
|
56
|
+
[tool.hatch.build.targets.wheel]
|
|
57
|
+
packages = ["src/dqflow"]
|
|
58
|
+
|
|
59
|
+
[tool.ruff]
|
|
60
|
+
target-version = "py39"
|
|
61
|
+
line-length = 100
|
|
62
|
+
|
|
63
|
+
[tool.ruff.lint]
|
|
64
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
65
|
+
|
|
66
|
+
[tool.pytest.ini_options]
|
|
67
|
+
testpaths = ["tests"]
|
|
68
|
+
addopts = "-v --tb=short"
|
|
69
|
+
|
|
70
|
+
[tool.mypy]
|
|
71
|
+
python_version = "3.9"
|
|
72
|
+
strict = true
|
|
73
|
+
warn_return_any = true
|
|
74
|
+
warn_unused_configs = true
|
|
75
|
+
|
|
76
|
+
[tool.coverage.run]
|
|
77
|
+
source = ["src/dqflow"]
|
|
78
|
+
branch = true
|
|
79
|
+
|
|
80
|
+
[tool.coverage.report]
|
|
81
|
+
exclude_lines = [
|
|
82
|
+
"pragma: no cover",
|
|
83
|
+
"if TYPE_CHECKING:",
|
|
84
|
+
]
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Command-line interface for dqflow."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import click
|
|
10
|
+
import pandas as pd
|
|
11
|
+
|
|
12
|
+
from dqflow import __version__
|
|
13
|
+
from dqflow.contract import Contract
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@click.group()
|
|
17
|
+
@click.version_option(version=__version__, prog_name="dqflow")
|
|
18
|
+
def main() -> None:
|
|
19
|
+
"""dqflow - Contract-first data quality for modern pipelines."""
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@main.command()
|
|
24
|
+
@click.argument("contract", type=click.Path(exists=True, path_type=Path))
|
|
25
|
+
@click.argument("data", type=click.Path(exists=True, path_type=Path))
|
|
26
|
+
@click.option("--output", "-o", type=click.Choice(["text", "json"]), default="text")
|
|
27
|
+
@click.option("--fail-fast", is_flag=True, help="Exit with error code on validation failure")
|
|
28
|
+
def validate(contract: Path, data: Path, output: str, fail_fast: bool) -> None:
|
|
29
|
+
"""Validate DATA against CONTRACT.
|
|
30
|
+
|
|
31
|
+
CONTRACT: Path to contract YAML file
|
|
32
|
+
DATA: Path to data file (parquet, csv, json)
|
|
33
|
+
"""
|
|
34
|
+
# Load contract
|
|
35
|
+
c = Contract.from_yaml(contract)
|
|
36
|
+
|
|
37
|
+
# Load data based on extension
|
|
38
|
+
df = _load_dataframe(data)
|
|
39
|
+
|
|
40
|
+
# Validate
|
|
41
|
+
result = c.validate(df)
|
|
42
|
+
|
|
43
|
+
# Output results
|
|
44
|
+
if output == "json":
|
|
45
|
+
click.echo(json.dumps(result.to_dict(), indent=2))
|
|
46
|
+
else:
|
|
47
|
+
click.echo(result.summary())
|
|
48
|
+
|
|
49
|
+
if fail_fast and not result.ok:
|
|
50
|
+
sys.exit(1)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@main.command()
|
|
54
|
+
@click.argument("contract", type=click.Path(exists=True, path_type=Path))
|
|
55
|
+
def show(contract: Path) -> None:
|
|
56
|
+
"""Show details of a CONTRACT."""
|
|
57
|
+
c = Contract.from_yaml(contract)
|
|
58
|
+
|
|
59
|
+
click.echo(f"Contract: {c.name}")
|
|
60
|
+
if c.description:
|
|
61
|
+
click.echo(f"Description: {c.description}")
|
|
62
|
+
click.echo()
|
|
63
|
+
|
|
64
|
+
click.echo("Columns:")
|
|
65
|
+
for col_name, col_def in c.columns.items():
|
|
66
|
+
constraints = []
|
|
67
|
+
if col_def.not_null:
|
|
68
|
+
constraints.append("NOT NULL")
|
|
69
|
+
if col_def.min is not None:
|
|
70
|
+
constraints.append(f"min={col_def.min}")
|
|
71
|
+
if col_def.max is not None:
|
|
72
|
+
constraints.append(f"max={col_def.max}")
|
|
73
|
+
if col_def.allowed:
|
|
74
|
+
constraints.append(f"allowed={col_def.allowed}")
|
|
75
|
+
if col_def.freshness_minutes:
|
|
76
|
+
constraints.append(f"freshness={col_def.freshness_minutes}m")
|
|
77
|
+
|
|
78
|
+
constraint_str = f" ({', '.join(constraints)})" if constraints else ""
|
|
79
|
+
click.echo(f" {col_name}: {col_def.dtype}{constraint_str}")
|
|
80
|
+
|
|
81
|
+
if c.rules:
|
|
82
|
+
click.echo()
|
|
83
|
+
click.echo("Rules:")
|
|
84
|
+
for rule in c.rules:
|
|
85
|
+
click.echo(f" - {rule}")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@main.command()
|
|
89
|
+
@click.argument("data", type=click.Path(exists=True, path_type=Path))
|
|
90
|
+
@click.argument("output", type=click.Path(path_type=Path))
|
|
91
|
+
def infer(data: Path, output: Path) -> None:
|
|
92
|
+
"""Infer a contract from DATA and write to OUTPUT."""
|
|
93
|
+
df = _load_dataframe(data)
|
|
94
|
+
|
|
95
|
+
from dqflow.column import Column
|
|
96
|
+
|
|
97
|
+
columns = {}
|
|
98
|
+
for col in df.columns:
|
|
99
|
+
dtype = df[col].dtype
|
|
100
|
+
if pd.api.types.is_integer_dtype(dtype):
|
|
101
|
+
columns[col] = Column(dtype=int)
|
|
102
|
+
elif pd.api.types.is_float_dtype(dtype):
|
|
103
|
+
columns[col] = Column(dtype=float)
|
|
104
|
+
elif pd.api.types.is_bool_dtype(dtype):
|
|
105
|
+
columns[col] = Column(dtype=bool)
|
|
106
|
+
elif pd.api.types.is_datetime64_any_dtype(dtype):
|
|
107
|
+
columns[col] = Column(dtype="timestamp")
|
|
108
|
+
else:
|
|
109
|
+
columns[col] = Column(dtype=str)
|
|
110
|
+
|
|
111
|
+
contract = Contract(name=output.stem, columns=columns)
|
|
112
|
+
contract.to_yaml(output)
|
|
113
|
+
click.echo(f"Contract written to {output}")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _load_dataframe(path: Path) -> pd.DataFrame:
|
|
117
|
+
"""Load DataFrame from file based on extension."""
|
|
118
|
+
suffix = path.suffix.lower()
|
|
119
|
+
if suffix == ".parquet":
|
|
120
|
+
return pd.read_parquet(path)
|
|
121
|
+
elif suffix == ".csv":
|
|
122
|
+
return pd.read_csv(path)
|
|
123
|
+
elif suffix == ".json":
|
|
124
|
+
return pd.read_json(path)
|
|
125
|
+
else:
|
|
126
|
+
raise click.ClickException(f"Unsupported file format: {suffix}")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
if __name__ == "__main__":
|
|
130
|
+
main()
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Column definition and validation logic."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class Column:
|
|
12
|
+
"""Define expectations for a single column."""
|
|
13
|
+
|
|
14
|
+
dtype: type | str
|
|
15
|
+
not_null: bool = False
|
|
16
|
+
min: float | None = None
|
|
17
|
+
max: float | None = None
|
|
18
|
+
allowed: Sequence[Any] | None = None
|
|
19
|
+
freshness_minutes: int | None = None
|
|
20
|
+
description: str = ""
|
|
21
|
+
metadata: dict[str, Any] = field(default_factory=dict)
|
|
22
|
+
|
|
23
|
+
def __post_init__(self) -> None:
|
|
24
|
+
if self.min is not None and self.max is not None and self.min > self.max:
|
|
25
|
+
raise ValueError(f"min ({self.min}) cannot be greater than max ({self.max})")
|