-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
224 lines (177 loc) · 5.94 KB
/
examples.py
File metadata and controls
224 lines (177 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""
Example usage of tablediff-arrow library.
Install from PyPI: pip install tablediff-arrow
This script demonstrates various ways to use the tablediff-arrow package
for comparing tables with different formats and options.
"""
import tempfile
from pathlib import Path
import pyarrow as pa
import pyarrow.parquet as pq
from tablediff_arrow import TableDiff
from tablediff_arrow.reports import generate_csv_report, generate_html_report
def example_basic_comparison():
"""Basic table comparison example."""
print("=" * 60)
print("Example 1: Basic Table Comparison")
print("=" * 60)
# Create sample tables
left = pa.table(
{
"id": [1, 2, 3, 4],
"name": ["Alice", "Bob", "Charlie", "David"],
"amount": [100.0, 200.0, 300.0, 400.0],
}
)
right = pa.table(
{
"id": [1, 2, 3, 5],
"name": ["Alice", "Bob", "Charlie", "Eve"],
"amount": [100.0, 205.0, 300.0, 500.0],
}
)
# Compare tables
differ = TableDiff(key_columns=["id"])
result = differ.compare_tables(left, right)
# Print summary
print("\n" + result.summary())
print()
def example_with_tolerance():
"""Table comparison with numeric tolerance."""
print("=" * 60)
print("Example 2: Comparison with Numeric Tolerance")
print("=" * 60)
left = pa.table({"id": [1, 2, 3], "value": [100.0, 200.0, 300.0]})
right = pa.table({"id": [1, 2, 3], "value": [100.01, 200.02, 300.03]})
# Without tolerance
print("\nWithout tolerance:")
differ = TableDiff(key_columns=["id"])
result = differ.compare_tables(left, right)
print(f"Changed rows: {result.changed_rows}")
# With absolute tolerance
print("\nWith absolute tolerance of 0.05:")
differ = TableDiff(key_columns=["id"], tolerance={"value": 0.05})
result = differ.compare_tables(left, right)
print(f"Changed rows: {result.changed_rows}")
print()
def example_file_comparison():
"""Compare files from disk."""
print("=" * 60)
print("Example 3: File Comparison")
print("=" * 60)
with tempfile.TemporaryDirectory() as tmpdir:
tmppath = Path(tmpdir)
# Create test files
left = pa.table({"id": [1, 2, 3], "value": [10, 20, 30]})
right = pa.table({"id": [1, 2, 3], "value": [10, 21, 30]})
left_path = tmppath / "left.parquet"
right_path = tmppath / "right.parquet"
pq.write_table(left, left_path)
pq.write_table(right, right_path)
# Compare files
differ = TableDiff(key_columns=["id"])
result = differ.compare_files(left_path, right_path)
print("\n" + result.summary())
print()
def example_html_report():
"""Generate an HTML report."""
print("=" * 60)
print("Example 4: HTML Report Generation")
print("=" * 60)
left = pa.table(
{
"id": [1, 2, 3, 4],
"category": ["A", "B", "C", "D"],
"value": [100, 200, 300, 400],
}
)
right = pa.table(
{
"id": [1, 2, 3, 5],
"category": ["A", "B", "C", "E"],
"value": [100, 210, 300, 500],
}
)
differ = TableDiff(key_columns=["id"])
result = differ.compare_tables(left, right)
with tempfile.TemporaryDirectory() as tmpdir:
report_path = Path(tmpdir) / "report.html"
generate_html_report(result, report_path)
print(f"\nHTML report generated at: {report_path}")
print(f"Report size: {report_path.stat().st_size} bytes")
print()
def example_csv_reports():
"""Generate CSV reports."""
print("=" * 60)
print("Example 5: CSV Reports Generation")
print("=" * 60)
left = pa.table(
{
"year": [2020, 2020, 2021],
"month": [1, 2, 1],
"sales": [1000, 2000, 3000],
}
)
right = pa.table(
{
"year": [2020, 2020, 2021],
"month": [1, 2, 1],
"sales": [1000, 2100, 3000],
}
)
differ = TableDiff(key_columns=["year", "month"])
result = differ.compare_tables(left, right)
with tempfile.TemporaryDirectory() as tmpdir:
generate_csv_report(result, tmpdir, prefix="sales_diff")
print("\nCSV reports generated:")
for csv_file in Path(tmpdir).glob("*.csv"):
print(f" - {csv_file.name} ({csv_file.stat().st_size} bytes)")
print()
def example_relative_tolerance():
"""Compare with relative tolerance."""
print("=" * 60)
print("Example 6: Relative Tolerance")
print("=" * 60)
left = pa.table(
{
"id": [1, 2],
"small_value": [10.0, 20.0],
"large_value": [1000.0, 2000.0],
}
)
right = pa.table(
{
"id": [1, 2],
"small_value": [10.1, 20.1], # 1% difference
"large_value": [1010.0, 2020.0], # 1% difference
}
)
# With absolute tolerance
print("\nWith absolute tolerance of 0.5:")
differ = TableDiff(key_columns=["id"], tolerance={"small_value": 0.5, "large_value": 0.5})
result = differ.compare_tables(left, right)
print(f"Changed rows: {result.changed_rows}")
print(f"Changes: {result.column_changes}")
# With relative tolerance
print("\nWith relative tolerance of 2% (0.02):")
differ = TableDiff(
key_columns=["id"],
relative_tolerance={"small_value": 0.02, "large_value": 0.02},
)
result = differ.compare_tables(left, right)
print(f"Changed rows: {result.changed_rows}")
print(f"Changes: {result.column_changes}")
print()
if __name__ == "__main__":
print("\n" + "=" * 60)
print("tablediff-arrow Examples")
print("=" * 60 + "\n")
example_basic_comparison()
example_with_tolerance()
example_file_comparison()
example_html_report()
example_csv_reports()
example_relative_tolerance()
print("=" * 60)
print("All examples completed successfully!")
print("=" * 60)