rowdelta 0.1.0__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.
- rowdelta/__init__.py +20 -0
- rowdelta/__main__.py +5 -0
- rowdelta/cli.py +202 -0
- rowdelta/engine.py +619 -0
- rowdelta/model.py +157 -0
- rowdelta/py.typed +0 -0
- rowdelta/report/__init__.py +55 -0
- rowdelta/report/html.py +137 -0
- rowdelta/report/markdown.py +91 -0
- rowdelta/report/terminal.py +143 -0
- rowdelta/sources.py +161 -0
- rowdelta-0.1.0.dist-info/METADATA +277 -0
- rowdelta-0.1.0.dist-info/RECORD +16 -0
- rowdelta-0.1.0.dist-info/WHEEL +4 -0
- rowdelta-0.1.0.dist-info/entry_points.txt +2 -0
- rowdelta-0.1.0.dist-info/licenses/LICENSE +21 -0
rowdelta/__init__.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""rowdelta: diff two tables by key, fast, on DuckDB."""
|
|
2
|
+
|
|
3
|
+
from rowdelta.engine import Diff, DiffError, Options, compare
|
|
4
|
+
from rowdelta.model import ColumnType, DiffResult, Duplicates, RowSample, SchemaDiff, SourceInfo
|
|
5
|
+
|
|
6
|
+
__version__ = "0.1.0"
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"ColumnType",
|
|
10
|
+
"Diff",
|
|
11
|
+
"DiffError",
|
|
12
|
+
"DiffResult",
|
|
13
|
+
"Duplicates",
|
|
14
|
+
"Options",
|
|
15
|
+
"RowSample",
|
|
16
|
+
"SchemaDiff",
|
|
17
|
+
"SourceInfo",
|
|
18
|
+
"__version__",
|
|
19
|
+
"compare",
|
|
20
|
+
]
|
rowdelta/__main__.py
ADDED
rowdelta/cli.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""Command line: rowdelta BEFORE AFTER --key id
|
|
2
|
+
|
|
3
|
+
Exit codes: 0 no differences, 1 differences found, 2 error (bad arguments, unreadable input).
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
import contextlib
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from rich.console import Console
|
|
15
|
+
|
|
16
|
+
from rowdelta import __version__
|
|
17
|
+
from rowdelta.engine import DiffError, compare
|
|
18
|
+
from rowdelta.model import DiffResult
|
|
19
|
+
from rowdelta.report import html, markdown, terminal, to_json
|
|
20
|
+
|
|
21
|
+
EPILOG = """\
|
|
22
|
+
sources:
|
|
23
|
+
file.csv / .tsv / .parquet / .json / .jsonl (also .gz), a Parquet glob such as "data/*.parquet",
|
|
24
|
+
db.sqlite::table, db.duckdb::table, or REV:path for a file at a git revision (main:data/users.csv)
|
|
25
|
+
|
|
26
|
+
examples:
|
|
27
|
+
rowdelta before.csv after.csv --key id
|
|
28
|
+
rowdelta main:data/customers.csv data/customers.csv --key customer_id
|
|
29
|
+
rowdelta old.parquet new.parquet --key order_id,line --atol 0.005 --ignore updated_at
|
|
30
|
+
rowdelta a.csv b.csv --key id --format markdown --output diff.md
|
|
31
|
+
rowdelta a.csv b.csv --key id --export changes.parquet
|
|
32
|
+
|
|
33
|
+
exit codes: 0 no differences, 1 differences found, 2 error
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
FAIL_ON = {"any", "rows", "schema", "added", "removed", "changed", "none"}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def parser() -> argparse.ArgumentParser:
|
|
40
|
+
p = argparse.ArgumentParser(
|
|
41
|
+
prog="rowdelta",
|
|
42
|
+
description="Diff two tables by key: added, removed and changed rows, column by column.",
|
|
43
|
+
epilog=EPILOG,
|
|
44
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
45
|
+
)
|
|
46
|
+
p.add_argument("before", help="the old table (A)")
|
|
47
|
+
p.add_argument("after", help="the new table (B)")
|
|
48
|
+
p.add_argument(
|
|
49
|
+
"-k",
|
|
50
|
+
"--key",
|
|
51
|
+
action="append",
|
|
52
|
+
default=[],
|
|
53
|
+
help="column(s) that identify a row; repeat or comma-separate for a composite key",
|
|
54
|
+
)
|
|
55
|
+
p.add_argument("--keyless", action="store_true", help="compare whole rows as a multiset, without a key")
|
|
56
|
+
g = p.add_argument_group("what to compare")
|
|
57
|
+
g.add_argument("-c", "--columns", action="append", default=[], help="compare only these columns")
|
|
58
|
+
g.add_argument("-i", "--ignore", action="append", default=[], help="leave these columns out")
|
|
59
|
+
g.add_argument("--atol", type=float, default=0.0, help="absolute tolerance for numbers (e.g. 0.01)")
|
|
60
|
+
g.add_argument("--rtol", type=float, default=0.0, help="relative tolerance for numbers (e.g. 1e-6)")
|
|
61
|
+
g.add_argument("--ignore-case", action="store_true", help="treat text that differs only in case as equal")
|
|
62
|
+
g.add_argument("--trim", action="store_true", help="ignore spaces at the start and end of text")
|
|
63
|
+
g.add_argument("--where", help="SQL filter applied to both sides, e.g. \"country = 'GB'\"")
|
|
64
|
+
o = p.add_argument_group("output")
|
|
65
|
+
o.add_argument(
|
|
66
|
+
"-f",
|
|
67
|
+
"--format",
|
|
68
|
+
choices=["terminal", "markdown", "json", "html"],
|
|
69
|
+
default=None,
|
|
70
|
+
help="report format (default: terminal, or taken from the --output extension)",
|
|
71
|
+
)
|
|
72
|
+
o.add_argument(
|
|
73
|
+
"-o",
|
|
74
|
+
"--output",
|
|
75
|
+
action="append",
|
|
76
|
+
help="write the report to a file instead of the screen; the format comes from the "
|
|
77
|
+
"extension (.md, .json, .html, anything else is text). Repeat for several files",
|
|
78
|
+
)
|
|
79
|
+
o.add_argument("-n", "--sample", type=int, default=10, help="example rows to show per kind (default 10)")
|
|
80
|
+
o.add_argument(
|
|
81
|
+
"--export",
|
|
82
|
+
metavar="FILE",
|
|
83
|
+
help="write every added, removed and changed row, with before and after values, "
|
|
84
|
+
"to .csv, .parquet or .json",
|
|
85
|
+
)
|
|
86
|
+
o.add_argument("--title", help="title for Markdown and HTML reports")
|
|
87
|
+
o.add_argument("--labels", nargs=2, metavar=("A", "B"), help="names to show instead of the paths")
|
|
88
|
+
o.add_argument("-q", "--quiet", action="store_true", help="print nothing; use the exit code")
|
|
89
|
+
o.add_argument(
|
|
90
|
+
"--fail-on",
|
|
91
|
+
default="any",
|
|
92
|
+
help="what counts as a difference for the exit code: any, rows, schema, added, removed, "
|
|
93
|
+
"changed or none; comma-separate several (default: any)",
|
|
94
|
+
)
|
|
95
|
+
p.add_argument("--memory-limit", help="cap DuckDB's memory, e.g. 2GB; larger work spills to disk")
|
|
96
|
+
p.add_argument("--no-color", action="store_true", help="plain text output")
|
|
97
|
+
p.add_argument("-V", "--version", action="version", version=f"rowdelta {__version__}")
|
|
98
|
+
return p
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def failed(r: DiffResult, rule: str) -> bool:
|
|
102
|
+
parts = {x.strip() for x in rule.split(",") if x.strip()}
|
|
103
|
+
unknown = parts - FAIL_ON
|
|
104
|
+
if unknown:
|
|
105
|
+
raise DiffError(
|
|
106
|
+
f"--fail-on: unknown value(s) {', '.join(sorted(unknown))}; use {', '.join(sorted(FAIL_ON))}"
|
|
107
|
+
)
|
|
108
|
+
checks = {
|
|
109
|
+
"any": r.has_differences,
|
|
110
|
+
"rows": r.row_changes > 0,
|
|
111
|
+
"schema": r.schema.changed,
|
|
112
|
+
"added": r.added > 0,
|
|
113
|
+
"removed": r.removed > 0,
|
|
114
|
+
"changed": r.changed > 0,
|
|
115
|
+
"none": False,
|
|
116
|
+
}
|
|
117
|
+
return any(checks[x] for x in parts)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
EXTENSIONS = {".md": "markdown", ".markdown": "markdown", ".json": "json", ".html": "html", ".htm": "html"}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _write(result: DiffResult, fmt: str, path: str | None, title: str | None, color: bool) -> None:
|
|
124
|
+
"""Render one report to a file, or to stdout when path is None."""
|
|
125
|
+
if fmt == "terminal":
|
|
126
|
+
if path:
|
|
127
|
+
with open(path, "w", encoding="utf-8") as f:
|
|
128
|
+
terminal.render(result, Console(file=f, width=120, no_color=True))
|
|
129
|
+
else:
|
|
130
|
+
terminal.render(result, Console(no_color=not color))
|
|
131
|
+
return
|
|
132
|
+
text = {
|
|
133
|
+
"markdown": lambda: markdown.render(result, title),
|
|
134
|
+
"json": lambda: to_json(result),
|
|
135
|
+
"html": lambda: html.render(result, title),
|
|
136
|
+
}[fmt]()
|
|
137
|
+
if path:
|
|
138
|
+
Path(path).write_text(text, encoding="utf-8")
|
|
139
|
+
else:
|
|
140
|
+
sys.stdout.write(text if text.endswith("\n") else text + "\n")
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _utf8_streams() -> None:
|
|
144
|
+
"""Windows pipes default to a legacy code page that cannot print → or ✓; switch them to UTF-8."""
|
|
145
|
+
for stream in (sys.stdout, sys.stderr):
|
|
146
|
+
enc = (getattr(stream, "encoding", None) or "").lower().replace("-", "")
|
|
147
|
+
if enc != "utf8" and hasattr(stream, "reconfigure") and not stream.isatty():
|
|
148
|
+
with contextlib.suppress(OSError, ValueError):
|
|
149
|
+
stream.reconfigure(encoding="utf-8")
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def main(argv: list[str] | None = None) -> int:
|
|
153
|
+
_utf8_streams()
|
|
154
|
+
args = parser().parse_args(argv)
|
|
155
|
+
err = Console(stderr=True, no_color=args.no_color or bool(os.environ.get("NO_COLOR")))
|
|
156
|
+
labels = args.labels or (args.before, args.after)
|
|
157
|
+
try:
|
|
158
|
+
with compare(
|
|
159
|
+
args.before,
|
|
160
|
+
args.after,
|
|
161
|
+
key=args.key or None,
|
|
162
|
+
keyless=args.keyless,
|
|
163
|
+
columns=args.columns or None,
|
|
164
|
+
ignore=args.ignore or None,
|
|
165
|
+
atol=args.atol,
|
|
166
|
+
rtol=args.rtol,
|
|
167
|
+
ignore_case=args.ignore_case,
|
|
168
|
+
trim=args.trim,
|
|
169
|
+
where=args.where,
|
|
170
|
+
sample=args.sample,
|
|
171
|
+
a_label=labels[0],
|
|
172
|
+
b_label=labels[1],
|
|
173
|
+
memory_limit=args.memory_limit,
|
|
174
|
+
) as diff:
|
|
175
|
+
result = diff.result
|
|
176
|
+
exported = diff.export(args.export) if args.export else None
|
|
177
|
+
code = 1 if failed(result, args.fail_on) else 0
|
|
178
|
+
except DiffError as e:
|
|
179
|
+
err.print(f"[bold red]rowdelta:[/] {e}")
|
|
180
|
+
return 2
|
|
181
|
+
except KeyboardInterrupt:
|
|
182
|
+
err.print("[bold red]rowdelta:[/] interrupted")
|
|
183
|
+
return 130
|
|
184
|
+
|
|
185
|
+
color = not (args.no_color or os.environ.get("NO_COLOR"))
|
|
186
|
+
outputs: list[str] = args.output or []
|
|
187
|
+
for path in outputs:
|
|
188
|
+
single = args.format if args.format and len(outputs) == 1 else None
|
|
189
|
+
_write(
|
|
190
|
+
result, single or EXTENSIONS.get(Path(path).suffix.lower(), "terminal"), path, args.title, color
|
|
191
|
+
)
|
|
192
|
+
if not args.quiet:
|
|
193
|
+
err.print(f"[dim]report written to {path}[/]")
|
|
194
|
+
if not outputs and not args.quiet:
|
|
195
|
+
_write(result, args.format or "terminal", None, args.title, color)
|
|
196
|
+
if exported is not None and not args.quiet:
|
|
197
|
+
err.print(f"[dim]{exported:,} rows written to {args.export}[/]")
|
|
198
|
+
return code
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
if __name__ == "__main__": # pragma: no cover
|
|
202
|
+
sys.exit(main())
|