diffmonkey 1.0.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.
- diffmonkey/__init__.py +49 -0
- diffmonkey/cli.py +168 -0
- diffmonkey/comparators.py +259 -0
- diffmonkey/compare.py +253 -0
- diffmonkey/formatters/__init__.py +13 -0
- diffmonkey/formatters/csv_out.py +55 -0
- diffmonkey/formatters/html.py +93 -0
- diffmonkey/formatters/markdown.py +94 -0
- diffmonkey/matching.py +141 -0
- diffmonkey/models.py +185 -0
- diffmonkey/readers.py +117 -0
- diffmonkey-1.0.0.dist-info/METADATA +153 -0
- diffmonkey-1.0.0.dist-info/RECORD +17 -0
- diffmonkey-1.0.0.dist-info/WHEEL +5 -0
- diffmonkey-1.0.0.dist-info/entry_points.txt +2 -0
- diffmonkey-1.0.0.dist-info/licenses/LICENSE +21 -0
- diffmonkey-1.0.0.dist-info/top_level.txt +1 -0
diffmonkey/models.py
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"""Result dataclasses for a diffmonkey comparison.
|
|
2
|
+
|
|
3
|
+
This module exists to give every comparison a typed, introspectable shape
|
|
4
|
+
instead of a bag of nested dicts. ``compare()`` returns a :class:`DiffResult`;
|
|
5
|
+
downstream code reads ``.added`` / ``.removed`` / ``.changed`` / ``.unchanged``
|
|
6
|
+
and ``.summary`` with autocomplete and type-checking, and renders reports via
|
|
7
|
+
the ``to_*`` helpers. The row-level types (:class:`FieldChange`,
|
|
8
|
+
:class:`RowDiff`) are frozen so a result is a stable record of one comparison.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from dataclasses import dataclass, field
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class DiffMonkeyError(Exception):
|
|
18
|
+
"""Base class for all diffmonkey errors."""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class DuplicateKeyError(DiffMonkeyError):
|
|
22
|
+
"""Raised when a key value occurs more than once and ``on_duplicate='error'``."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class MissingKeyError(DiffMonkeyError):
|
|
26
|
+
"""Raised when a row lacks a key column and ``on_missing_key='error'``."""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class FieldChange:
|
|
31
|
+
"""A single column whose value differs between matched rows.
|
|
32
|
+
|
|
33
|
+
``old`` and ``new`` are the *original* (un-normalised) values, so a report
|
|
34
|
+
shows what the data actually said, not its comparison-normalised form.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
column: str
|
|
38
|
+
old: Any
|
|
39
|
+
new: Any
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True)
|
|
43
|
+
class RowDiff:
|
|
44
|
+
"""One row's place in the diff.
|
|
45
|
+
|
|
46
|
+
``key`` is always a tuple (length 1 for a single key column, longer for a
|
|
47
|
+
composite key) so callers can treat single and composite keys uniformly.
|
|
48
|
+
``old`` / ``new`` are the source row dicts: ``new`` is ``None`` for removed
|
|
49
|
+
rows, ``old`` is ``None`` for added rows, and both are present for changed
|
|
50
|
+
and unchanged rows. ``changes`` is non-empty only for changed rows.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
key: tuple[Any, ...]
|
|
54
|
+
old: dict[str, Any] | None = None
|
|
55
|
+
new: dict[str, Any] | None = None
|
|
56
|
+
changes: tuple[FieldChange, ...] = ()
|
|
57
|
+
|
|
58
|
+
def key_dict(self, key_columns: tuple[str, ...]) -> dict[str, Any]:
|
|
59
|
+
"""Return the key as a ``{column: value}`` mapping for display/export."""
|
|
60
|
+
return dict(zip(key_columns, self.key))
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True)
|
|
64
|
+
class DiffSummary:
|
|
65
|
+
"""Aggregate counts for a comparison.
|
|
66
|
+
|
|
67
|
+
``matched`` is the number of keys present in *both* inputs (``changed +
|
|
68
|
+
unchanged``). ``total_old`` / ``total_new`` count input rows *after*
|
|
69
|
+
duplicate resolution, so ``added + removed + matched`` need not equal their
|
|
70
|
+
sum when duplicates were collapsed; ``duplicate_keys`` records how many
|
|
71
|
+
distinct keys were duplicated on either side.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
total_old: int
|
|
75
|
+
total_new: int
|
|
76
|
+
matched: int
|
|
77
|
+
added: int
|
|
78
|
+
removed: int
|
|
79
|
+
changed: int
|
|
80
|
+
unchanged: int
|
|
81
|
+
key_columns: tuple[str, ...]
|
|
82
|
+
compared_columns: tuple[str, ...]
|
|
83
|
+
duplicate_keys: int = 0
|
|
84
|
+
|
|
85
|
+
def as_dict(self) -> dict[str, Any]:
|
|
86
|
+
return {
|
|
87
|
+
"total_old": self.total_old,
|
|
88
|
+
"total_new": self.total_new,
|
|
89
|
+
"matched": self.matched,
|
|
90
|
+
"added": self.added,
|
|
91
|
+
"removed": self.removed,
|
|
92
|
+
"changed": self.changed,
|
|
93
|
+
"unchanged": self.unchanged,
|
|
94
|
+
"duplicate_keys": self.duplicate_keys,
|
|
95
|
+
"key_columns": list(self.key_columns),
|
|
96
|
+
"compared_columns": list(self.compared_columns),
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
def one_line(self) -> str:
|
|
100
|
+
"""A single human sentence, e.g. the headline of a report."""
|
|
101
|
+
return (
|
|
102
|
+
f"{self.added} added, {self.removed} removed, "
|
|
103
|
+
f"{self.changed} changed (of {self.total_new} current), "
|
|
104
|
+
f"{self.unchanged} unchanged"
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@dataclass
|
|
109
|
+
class DiffResult:
|
|
110
|
+
"""The full outcome of comparing two tabular datasets.
|
|
111
|
+
|
|
112
|
+
Rows are bucketed into :attr:`added` (key only in *new*), :attr:`removed`
|
|
113
|
+
(key only in *old*), :attr:`changed` (matched, at least one compared field
|
|
114
|
+
differs) and :attr:`unchanged` (matched, no compared field differs).
|
|
115
|
+
:attr:`unchanged` is only populated when ``compare(..., include_unchanged=
|
|
116
|
+
True)``. :attr:`warnings` collects non-fatal issues (duplicate or missing
|
|
117
|
+
keys) surfaced during matching.
|
|
118
|
+
"""
|
|
119
|
+
|
|
120
|
+
added: list[RowDiff]
|
|
121
|
+
removed: list[RowDiff]
|
|
122
|
+
changed: list[RowDiff]
|
|
123
|
+
unchanged: list[RowDiff]
|
|
124
|
+
summary: DiffSummary
|
|
125
|
+
key_columns: tuple[str, ...]
|
|
126
|
+
compared_columns: tuple[str, ...]
|
|
127
|
+
warnings: list[str] = field(default_factory=list)
|
|
128
|
+
|
|
129
|
+
# -- serialisation -----------------------------------------------------
|
|
130
|
+
|
|
131
|
+
def to_dict(self) -> dict[str, Any]:
|
|
132
|
+
"""A JSON-serialisable dict (given JSON-serialisable cell values)."""
|
|
133
|
+
|
|
134
|
+
def row(rd: RowDiff, body: str) -> dict[str, Any]:
|
|
135
|
+
out: dict[str, Any] = {"key": rd.key_dict(self.key_columns)}
|
|
136
|
+
if body == "added":
|
|
137
|
+
out["row"] = rd.new
|
|
138
|
+
elif body == "removed":
|
|
139
|
+
out["row"] = rd.old
|
|
140
|
+
elif body == "unchanged":
|
|
141
|
+
out["row"] = rd.new
|
|
142
|
+
else: # changed
|
|
143
|
+
out["changes"] = {
|
|
144
|
+
c.column: {"old": c.old, "new": c.new} for c in rd.changes
|
|
145
|
+
}
|
|
146
|
+
return out
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
"summary": self.summary.as_dict(),
|
|
150
|
+
"added": [row(r, "added") for r in self.added],
|
|
151
|
+
"removed": [row(r, "removed") for r in self.removed],
|
|
152
|
+
"changed": [row(r, "changed") for r in self.changed],
|
|
153
|
+
"unchanged": [row(r, "unchanged") for r in self.unchanged],
|
|
154
|
+
"warnings": list(self.warnings),
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
def to_markdown(self, *, max_rows: int | None = None) -> str:
|
|
158
|
+
"""Human-readable markdown report. See :mod:`diffmonkey.formatters.markdown`."""
|
|
159
|
+
from .formatters import markdown
|
|
160
|
+
|
|
161
|
+
return markdown.render(self, max_rows=max_rows)
|
|
162
|
+
|
|
163
|
+
def to_html(self, *, title: str = "diffmonkey report") -> str:
|
|
164
|
+
"""Standalone HTML diff report. See :mod:`diffmonkey.formatters.html`."""
|
|
165
|
+
from .formatters import html
|
|
166
|
+
|
|
167
|
+
return html.render(self, title=title)
|
|
168
|
+
|
|
169
|
+
def to_csv(self) -> str:
|
|
170
|
+
"""CSV of every changed field (one row per field change). See
|
|
171
|
+
:mod:`diffmonkey.formatters.csv_out`."""
|
|
172
|
+
from .formatters import csv_out
|
|
173
|
+
|
|
174
|
+
return csv_out.render(self)
|
|
175
|
+
|
|
176
|
+
def write_csv(self, path: str) -> None:
|
|
177
|
+
"""Write :meth:`to_csv` output to ``path`` (UTF-8, ``\\n`` line endings)."""
|
|
178
|
+
with open(path, "w", encoding="utf-8", newline="") as fh:
|
|
179
|
+
fh.write(self.to_csv())
|
|
180
|
+
|
|
181
|
+
# -- convenience -------------------------------------------------------
|
|
182
|
+
|
|
183
|
+
def has_changes(self) -> bool:
|
|
184
|
+
"""True when anything was added, removed, or changed."""
|
|
185
|
+
return bool(self.added or self.removed or self.changed)
|
diffmonkey/readers.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Read tabular inputs into lists of dicts for :func:`diffmonkey.compare`.
|
|
2
|
+
|
|
3
|
+
This module exists so the CLI (and callers who start from files) can hand
|
|
4
|
+
``compare()`` the list-of-dicts it expects, regardless of source format. DSV
|
|
5
|
+
files (CSV/TSV/pipe/…) are read with the stdlib ``csv`` module — robust and
|
|
6
|
+
dependency-free. Excel and richer DSV repair are *optional*: if ``openpyxl`` or
|
|
7
|
+
``dsvmonkey`` is installed we use it, otherwise reading those formats raises a
|
|
8
|
+
clear, actionable error rather than failing obscurely.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import csv
|
|
14
|
+
import io
|
|
15
|
+
import os
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
DELIMITER_BY_EXT = {
|
|
19
|
+
".csv": ",",
|
|
20
|
+
".tsv": "\t",
|
|
21
|
+
".tab": "\t",
|
|
22
|
+
".psv": "|",
|
|
23
|
+
".pipe": "|",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def read_csv(
|
|
28
|
+
path: str,
|
|
29
|
+
*,
|
|
30
|
+
delimiter: str | None = None,
|
|
31
|
+
encoding: str = "utf-8-sig",
|
|
32
|
+
) -> list[dict[str, Any]]:
|
|
33
|
+
"""Read a delimited file into a list of dicts using the stdlib ``csv``.
|
|
34
|
+
|
|
35
|
+
``encoding`` defaults to ``utf-8-sig`` so a leading BOM is stripped from the
|
|
36
|
+
first header. ``delimiter`` defaults to the one implied by the file
|
|
37
|
+
extension (``,`` for unknown extensions). All cell values are strings; type
|
|
38
|
+
awareness happens later in :func:`compare`.
|
|
39
|
+
"""
|
|
40
|
+
if delimiter is None:
|
|
41
|
+
ext = os.path.splitext(path)[1].lower()
|
|
42
|
+
delimiter = DELIMITER_BY_EXT.get(ext, ",")
|
|
43
|
+
with open(path, "r", encoding=encoding, newline="") as fh:
|
|
44
|
+
reader = csv.DictReader(fh, delimiter=delimiter)
|
|
45
|
+
rows = [dict(r) for r in reader]
|
|
46
|
+
return rows
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def read_excel(path: str, *, sheet: str | int | None = None) -> list[dict[str, Any]]:
|
|
50
|
+
"""Read the first (or named) worksheet into a list of dicts.
|
|
51
|
+
|
|
52
|
+
Requires the ``excel`` extra (``pip install diffmonkey[excel]``). The first
|
|
53
|
+
row is treated as the header. Raises :class:`RuntimeError` with install
|
|
54
|
+
guidance if ``openpyxl`` is unavailable.
|
|
55
|
+
"""
|
|
56
|
+
try:
|
|
57
|
+
from openpyxl import load_workbook
|
|
58
|
+
except ImportError as exc: # pragma: no cover - exercised via monkeypatch
|
|
59
|
+
raise RuntimeError(
|
|
60
|
+
"Reading Excel files requires openpyxl. "
|
|
61
|
+
"Install it with: pip install diffmonkey[excel]"
|
|
62
|
+
) from exc
|
|
63
|
+
|
|
64
|
+
wb = load_workbook(path, read_only=True, data_only=True)
|
|
65
|
+
ws = wb[sheet] if isinstance(sheet, str) else (
|
|
66
|
+
wb.worksheets[sheet] if isinstance(sheet, int) else wb.active
|
|
67
|
+
)
|
|
68
|
+
rows_iter = ws.iter_rows(values_only=True)
|
|
69
|
+
try:
|
|
70
|
+
header = next(rows_iter)
|
|
71
|
+
except StopIteration:
|
|
72
|
+
return []
|
|
73
|
+
headers = [("" if h is None else str(h)) for h in header]
|
|
74
|
+
out: list[dict[str, Any]] = []
|
|
75
|
+
for raw in rows_iter:
|
|
76
|
+
out.append({headers[i]: raw[i] if i < len(raw) else None for i in range(len(headers))})
|
|
77
|
+
wb.close()
|
|
78
|
+
return out
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
EXCEL_EXTENSIONS = (".xlsx", ".xlsm", ".xltx", ".xltm")
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def read_table(
|
|
85
|
+
path: str,
|
|
86
|
+
*,
|
|
87
|
+
delimiter: str | None = None,
|
|
88
|
+
encoding: str = "utf-8-sig",
|
|
89
|
+
sheet: str | int | None = None,
|
|
90
|
+
) -> list[dict[str, Any]]:
|
|
91
|
+
"""Read ``path`` by dispatching on its extension.
|
|
92
|
+
|
|
93
|
+
``.xlsx``/``.xlsm``/``.xltx``/``.xltm`` go to :func:`read_excel`; everything
|
|
94
|
+
else is treated as delimited text via :func:`read_csv`. Arguments are routed
|
|
95
|
+
to the format that understands them: ``delimiter``/``encoding`` apply to
|
|
96
|
+
delimited text only, ``sheet`` to Excel only. Passing ``delimiter`` for an
|
|
97
|
+
Excel input raises :class:`ValueError` rather than failing obscurely deep in
|
|
98
|
+
the Excel reader.
|
|
99
|
+
"""
|
|
100
|
+
ext = os.path.splitext(path)[1].lower()
|
|
101
|
+
if ext in EXCEL_EXTENSIONS:
|
|
102
|
+
if delimiter is not None:
|
|
103
|
+
raise ValueError(
|
|
104
|
+
f"delimiter is not applicable to Excel input {path!r}"
|
|
105
|
+
)
|
|
106
|
+
return read_excel(path, sheet=sheet)
|
|
107
|
+
if sheet is not None:
|
|
108
|
+
raise ValueError(f"sheet is not applicable to delimited input {path!r}")
|
|
109
|
+
return read_csv(path, delimiter=delimiter, encoding=encoding)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def read_csv_string(
|
|
113
|
+
text: str, *, delimiter: str = ","
|
|
114
|
+
) -> list[dict[str, Any]]:
|
|
115
|
+
"""Parse a delimited *string* into a list of dicts (used in tests/pipes)."""
|
|
116
|
+
reader = csv.DictReader(io.StringIO(text), delimiter=delimiter)
|
|
117
|
+
return [dict(r) for r in reader]
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: diffmonkey
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Type-aware, key-based structural diffing of tabular datasets with human- and machine-readable reports.
|
|
5
|
+
Author-email: RexBytes <pythonic@rexbytes.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 RexBytes
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/RexBytes/diffmonkey
|
|
29
|
+
Project-URL: Issues, https://github.com/RexBytes/diffmonkey/issues
|
|
30
|
+
Keywords: diff,csv,tabular,compare,dataset,changes,reconciliation
|
|
31
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Operating System :: OS Independent
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
38
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
39
|
+
Classifier: Topic :: Utilities
|
|
40
|
+
Requires-Python: >=3.11
|
|
41
|
+
Description-Content-Type: text/markdown
|
|
42
|
+
License-File: LICENSE
|
|
43
|
+
Requires-Dist: cleanmonkey
|
|
44
|
+
Requires-Dist: typemonkey
|
|
45
|
+
Requires-Dist: datemonkey
|
|
46
|
+
Provides-Extra: excel
|
|
47
|
+
Requires-Dist: openpyxl>=3.0; extra == "excel"
|
|
48
|
+
Provides-Extra: dsv
|
|
49
|
+
Requires-Dist: dsvmonkey; extra == "dsv"
|
|
50
|
+
Provides-Extra: dev
|
|
51
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
52
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
53
|
+
Requires-Dist: hypothesis>=6.0; extra == "dev"
|
|
54
|
+
Dynamic: license-file
|
|
55
|
+
|
|
56
|
+
# diffmonkey
|
|
57
|
+
|
|
58
|
+
Type-aware, key-based structural diffing of tabular datasets — answer "what
|
|
59
|
+
changed between last month's export and this month's?" in one call, with
|
|
60
|
+
human- and machine-readable reports.
|
|
61
|
+
|
|
62
|
+
diffmonkey matches rows by a key column (or composite key), compares the
|
|
63
|
+
remaining columns *with type awareness* (numbers by value, dates by calendar
|
|
64
|
+
date, booleans by truth, strings whitespace-normalised, nulls unified), and
|
|
65
|
+
buckets the result into **added / removed / changed / unchanged** with summary
|
|
66
|
+
statistics. It is built on the rexbytes ecosystem — [`typemonkey`] for type
|
|
67
|
+
inference and number parsing, [`datemonkey`] for date parsing, [`cleanmonkey`]
|
|
68
|
+
for whitespace and invisible-character normalisation — so it does not re-derive
|
|
69
|
+
those wheels.
|
|
70
|
+
|
|
71
|
+
In scope: structural comparison, change detection, change reporting. Out of
|
|
72
|
+
scope: merge/reconciliation, text diffing, schema migration, version control.
|
|
73
|
+
|
|
74
|
+
## Install
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install diffmonkey # CSV/TSV/pipe input built in
|
|
78
|
+
pip install "diffmonkey[excel]" # add .xlsx reading (openpyxl)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Requires Python 3.11+.
|
|
82
|
+
|
|
83
|
+
## Quick start
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from diffmonkey import compare
|
|
87
|
+
|
|
88
|
+
old = [{"id": "1", "name": "Widget", "price": "1,234"},
|
|
89
|
+
{"id": "2", "name": "Gadget", "price": "50"}]
|
|
90
|
+
new = [{"id": "1", "name": "Widget", "price": "1234"}, # price reformatted, not changed
|
|
91
|
+
{"id": "3", "name": "Gizmo", "price": "9"}] # id 2 removed, id 3 added
|
|
92
|
+
|
|
93
|
+
result = compare(old, new, key="id")
|
|
94
|
+
|
|
95
|
+
print(result.summary.one_line())
|
|
96
|
+
# 1 added, 1 removed, 0 changed (of 2 current), 0 unchanged
|
|
97
|
+
|
|
98
|
+
print(result.to_markdown()) # human report
|
|
99
|
+
result.to_dict() # machine-readable
|
|
100
|
+
result.write_csv("changes.csv")
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`"1,234"` vs `"1234"` is **not** reported as a change — type-aware numeric
|
|
104
|
+
comparison sees one number. The same applies to `"01/02/2025"` vs `"2025-01-02"`
|
|
105
|
+
(dates, with a `locale` hint), `" foo "` vs `"foo"` (whitespace), and
|
|
106
|
+
`None`/`""`/`"NA"` (nulls).
|
|
107
|
+
|
|
108
|
+
## CLI
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
diffmonkey compare old.csv new.csv --key id
|
|
112
|
+
diffmonkey compare old.csv new.csv --key region,sku --ignore updated_at --format markdown
|
|
113
|
+
diffmonkey compare old.xlsx new.xlsx --key id --format json -o diff.json
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Exit code is `0` when the datasets are identical and `1` when they differ —
|
|
117
|
+
handy in CI and scripts.
|
|
118
|
+
|
|
119
|
+
## Key options
|
|
120
|
+
|
|
121
|
+
| Option | Purpose |
|
|
122
|
+
|---|---|
|
|
123
|
+
| `key` | Identity column, or list for a composite key |
|
|
124
|
+
| `columns` / `ignore` | Restrict / exclude columns from comparison |
|
|
125
|
+
| `column_map={"old":"new"}` | Handle renamed columns (avoids false add+remove) |
|
|
126
|
+
| `rel_tol` / `abs_tol` | Floating-point tolerance for numeric columns |
|
|
127
|
+
| `locale="us"` / `"eu"` | Disambiguate slash dates and number separators |
|
|
128
|
+
| `null_equivalent` | Treat all null spellings as one value (default on) |
|
|
129
|
+
| `type_aware` / `date_aware` | Toggle type/date-aware comparison |
|
|
130
|
+
| `include_unchanged` | Retain unchanged rows in the result |
|
|
131
|
+
| `on_duplicate` / `on_missing_key` | Policies for messy keys |
|
|
132
|
+
|
|
133
|
+
## Output formats
|
|
134
|
+
|
|
135
|
+
- `result.to_dict()` — JSON-serialisable structure
|
|
136
|
+
- `result.to_markdown()` — report for PRs, chat, email
|
|
137
|
+
- `result.to_html()` — standalone HTML diff report
|
|
138
|
+
- `result.to_csv()` / `result.write_csv(path)` — one row per field change
|
|
139
|
+
|
|
140
|
+
## Using with AI assistants
|
|
141
|
+
|
|
142
|
+
See [`SKILL.md`](./SKILL.md) for LLM-oriented usage (decision tree, worked
|
|
143
|
+
examples, anti-patterns). See [`LIMITATIONS.md`](./LIMITATIONS.md) for the
|
|
144
|
+
deliberate design tradeoffs (date/locale ambiguity, null vocabulary, duplicate
|
|
145
|
+
handling) so behaviour that looks surprising is not mistaken for a bug.
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
|
|
149
|
+
MIT — see [`LICENSE`](./LICENSE).
|
|
150
|
+
|
|
151
|
+
[`typemonkey`]: https://pypi.org/project/typemonkey/
|
|
152
|
+
[`datemonkey`]: https://pypi.org/project/datemonkey/
|
|
153
|
+
[`cleanmonkey`]: https://pypi.org/project/cleanmonkey/
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
diffmonkey/__init__.py,sha256=24HiUU_WjZx9kJ5WCACQ8NVVtEeKDKrXgHQGIJ1wE-8,1259
|
|
2
|
+
diffmonkey/cli.py,sha256=w4LP70cP0UeQpUxZSVQzBbMK0ZUpzFatelTjRPAj1VM,6508
|
|
3
|
+
diffmonkey/comparators.py,sha256=P-7d9OJTCGFsb8ty3bJwq52gICnS3LNX11PWcOhYeK0,9722
|
|
4
|
+
diffmonkey/compare.py,sha256=tZFIoKW6ZJRJbihVOQ50j4P2wQ3lpTnZjg56pto0xKw,9600
|
|
5
|
+
diffmonkey/matching.py,sha256=zSkWf8tjUi-4NdFTdGk2Ofkpjw1t4y41xy_B4Ka-iK4,5294
|
|
6
|
+
diffmonkey/models.py,sha256=Khdl5xuS6zZT7JrEm0V40SrUNfk-JyweIWH4fnZsZZg,6639
|
|
7
|
+
diffmonkey/readers.py,sha256=ZdhCTUaiVWmlTFD_QSi1EUL9h_Ke4I9ltp8PW-IuxCw,4186
|
|
8
|
+
diffmonkey/formatters/__init__.py,sha256=AOO53gkYHHwcXeIoRw_qg820cCRT5d0NVPWmmGSv5nc,461
|
|
9
|
+
diffmonkey/formatters/csv_out.py,sha256=7bo7rZSGwUeg32RzgcwWbx2dGn5CktBbl6gZDKGSGdI,1807
|
|
10
|
+
diffmonkey/formatters/html.py,sha256=lQGRSCjncNLp0AR9DEs0ki7JFLhvZRq0XYCY_q7ps-U,3427
|
|
11
|
+
diffmonkey/formatters/markdown.py,sha256=SFnWZ0-nbh1lwmt_70Dfzv_qE5tE3N4vB4t2uEuBWFc,3271
|
|
12
|
+
diffmonkey-1.0.0.dist-info/licenses/LICENSE,sha256=srNahN_Cxejm5SlFsCghF2Mml1gXgqlnuqWlDt7F1ck,1065
|
|
13
|
+
diffmonkey-1.0.0.dist-info/METADATA,sha256=Vf1kiH8sR4u2jLL2WGo53Zyuvn6iam_sxWO2YPfI-eA,6320
|
|
14
|
+
diffmonkey-1.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
15
|
+
diffmonkey-1.0.0.dist-info/entry_points.txt,sha256=CuC3tfsyD9w705dxxGpk41euAKTubnI3wOdL045BPcs,51
|
|
16
|
+
diffmonkey-1.0.0.dist-info/top_level.txt,sha256=m7WLtVq4qOOi5_ygdJl9wsktjtIrd4F4sZPznl9W5yQ,11
|
|
17
|
+
diffmonkey-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 RexBytes
|
|
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
|
+
diffmonkey
|