Skip to content

Commit 8116b50

Browse files
author
SoundsSerious
committed
Refactor diff_get class to Diffr and enhance type assertion checks; update version to 0.2.0
1 parent 982822d commit 8116b50

3 files changed

Lines changed: 39 additions & 28 deletions

File tree

diffgetr/diff_get.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@
77
from pprint import pprint
88

99

10-
class diff_get:
10+
class Diffr:
1111

1212
def __init__(
1313
self, s0, s1, loc=None, path=None, deep_diff_kw=None, ignore_added=False
1414
):
1515

16-
assert type(s0) is type(s1), f"bad types!"
17-
if isinstance(s0, (tuple, list)) and isinstance(s1, (tuple, list)):
16+
#TODO: fail here for type differences
17+
st0 = type(s0)
18+
st1 = type(s1)
19+
if st0 != st1:
20+
preview_0 = str(s0)[:100]
21+
preview_1 = str(s1)[:100]
22+
raise Exception(f'{loc}.{path}| types different: {st0} vs {st1} |0: {preview_0} | 1:{preview_1}')
23+
elif st0 in (str,int,float):
24+
preview_0 = str(s0)[:100]
25+
preview_1 = str(s1)[:100]
26+
if s0 != s1:
27+
raise Exception(f'{loc}.{path}| values different: {preview_0} vs {preview_1}')
28+
elif isinstance(s0, (tuple, list)) and isinstance(s1, (tuple, list)):
1829
print(f"converting lists -> dict")
1930
s0 = {i: v for i, v in enumerate(s0)}
2031
s1 = {i: v for i, v in enumerate(s1)}
@@ -39,7 +50,7 @@ def __init__(
3950

4051
def __getitem__(self, key):
4152
if key in self.s0 and key in self.s1:
42-
return diff_get(
53+
return Diffr(
4354
self.s0[key],
4455
self.s1[key],
4556
path=key,
@@ -68,7 +79,7 @@ def dict_keys(self)->set:
6879
and isinstance(self.s1[k],dict)
6980
))
7081

71-
def path_diffs(self,syskey:str):
82+
def path_diffs(self,syskey:str) -> "Diffr":
7283
"""take sys key like root.p1.p2.pk[0].*.po[*].val and generate diffs through each matching key. If there is a key prefix'd with path, such as root.p1.p2 be sure to strip that so you can navigate the data.
7384
"""
7485
if '.' not in syskey and '[' not in syskey:
@@ -107,7 +118,7 @@ def path_diffs(self,syskey:str):
107118
for j in range(min(len(array1),len(array2))):
108119
v1 = array1[j]
109120
v2 = array2[j]
110-
for val in diff_getr(v1,v2).path_diffs(nxt):
121+
for val in Diffrr(v1,v2).path_diffs(nxt):
111122
yield val
112123

113124
elif key_seg in self.dict_keys():
@@ -327,14 +338,14 @@ def parent_key(key):
327338
)
328339
if pct is not None and abs(pct) > threshold:
329340
if group_print is False:
330-
print(f"\nGROUP: {p}")
341+
print(f"\nGROUP: {self.path}.{p}")
331342
group_print = True
332343
print(
333344
f" >{key:<50} | {v0s:^30} | {v1s:^30} | {diff:>14.4f} |{pct_diff:>10}"
334345
)
335346
elif pct is None and v0 != v1:
336347
if group_print is False:
337-
print(f"\nGROUP: {p}")
348+
print(f"\nGROUP: {self.path}.{p}")
338349
group_print = True
339350
print(
340351
f" >{key:<50} | {v0s:^30} | {v1s:^30} | {'-':^14} | {'-':^10}"
@@ -417,7 +428,7 @@ def main():
417428
with open(args.file2, "r", encoding="utf-8") as f:
418429
s1 = json.load(f)
419430

420-
DIFF = diff_get(s0, s1)
431+
DIFF = Diffr(s0, s1)
421432
keys = args.path.split(".")
422433
try:
423434
for key in keys:
@@ -427,7 +438,7 @@ def main():
427438
DIFF = DIFF[base]
428439
loc = DIFF.loc.copy()
429440
loc.append(f"[{idx}]")
430-
DIFF = diff_get(DIFF.s0[idx], DIFF.s1[idx], loc=loc)
441+
DIFF = Diffr(DIFF.s0[idx], DIFF.s1[idx], loc=loc)
431442
continue
432443
else:
433444
DIFF = DIFF[key]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "diffgetr"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
description = "A Python library for comparing nested data structures with detailed diff reporting and interactive navigation."
55
authors = [
66
{ name = "Your Name", email = "your.email@example.com" }

tests/test_diff_get.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
import json
33
import io
4-
from diffgetr.diff_get import diff_get
4+
from diffgetr.diff_get import Diffr
55

66

77
class TestDiffGet(unittest.TestCase):
@@ -11,7 +11,7 @@ def test_basic_diff(self):
1111
s0 = {"a": 1, "b": 2, "c": {"d": 3}}
1212
s1 = {"a": 1, "b": 3, "c": {"d": 4}}
1313

14-
diff = diff_get(s0, s1)
14+
diff = Diffr(s0, s1)
1515
assert diff.location == "root"
1616

1717
# Test that diff object is created
@@ -23,7 +23,7 @@ def test_navigation(self):
2323
s0 = {"level1": {"level2": {"value": 10}}}
2424
s1 = {"level1": {"level2": {"value": 20}}}
2525

26-
diff = diff_get(s0, s1)
26+
diff = Diffr(s0, s1)
2727
nested_diff = diff['level1']['level2']
2828

2929
assert nested_diff.location == "root.level1.level2"
@@ -35,7 +35,7 @@ def test_list_conversion(self):
3535
s0 = [1, 2, 3]
3636
s1 = [1, 2, 4]
3737

38-
diff = diff_get(s0, s1)
38+
diff = Diffr(s0, s1)
3939
assert isinstance(diff.s0, dict)
4040
assert isinstance(diff.s1, dict)
4141
assert diff.s0[2] == 3
@@ -46,7 +46,7 @@ def test_keys_method(self):
4646
s0 = {"a": 1, "b": 2, "c": 3}
4747
s1 = {"a": 1, "b": 3, "d": 4}
4848

49-
diff = diff_get(s0, s1)
49+
diff = Diffr(s0, s1)
5050
keys = diff.keys()
5151

5252
assert keys == {"a", "b"}
@@ -56,7 +56,7 @@ def test_ignore_added(self):
5656
s0 = {"a": 1, "b": 2}
5757
s1 = {"a": 1, "b": 2, "c": 3}
5858

59-
diff = diff_get(s0, s1, ignore_added=True)
59+
diff = Diffr(s0, s1, ignore_added=True)
6060
diff_obj = diff.diff_obj
6161

6262
# Should not contain dictionary_item_added
@@ -68,19 +68,19 @@ def test_custom_deep_diff_params(self):
6868
s1 = {"value": 1.123457}
6969

7070
# With default precision (3), should see no difference
71-
diff1 = diff_get(s0, s1)
71+
diff1 = Diffr(s0, s1)
7272
assert len(diff1.diff_obj) == 0
7373

7474
# With high precision, should see difference
75-
diff2 = diff_get(s0, s1, deep_diff_kw={'significant_digits': 6})
75+
diff2 = Diffr(s0, s1, deep_diff_kw={'significant_digits': 6})
7676
assert len(diff2.diff_obj) > 0
7777

7878
def test_keyerror_handling(self):
7979
"""Test KeyError handling when navigating to non-existent keys"""
8080
s0 = {"a": {"b": 1}}
8181
s1 = {"a": {"c": 2}}
8282

83-
diff = diff_get(s0, s1)
83+
diff = Diffr(s0, s1)
8484

8585
with self.assertRaises(KeyError) as context:
8686
diff['a']['nonexistent']
@@ -92,7 +92,7 @@ def test_string_representation(self):
9292
s0 = {"a": 1}
9393
s1 = {"a": 2}
9494

95-
diff = diff_get(s0, s1)
95+
diff = Diffr(s0, s1)
9696
str_repr = str(diff)
9797

9898
assert "root diffing summary" in str_repr
@@ -103,7 +103,7 @@ def test_repr(self):
103103
s0 = {"a": 1}
104104
s1 = {"a": 2}
105105

106-
diff = diff_get(s0, s1)
106+
diff = Diffr(s0, s1)
107107
repr_str = repr(diff)
108108

109109
assert repr_str == "diff[root]"
@@ -113,7 +113,7 @@ def test_diff_summary_output(self):
113113
s0 = {"a": 1, "b": {"c": 2}}
114114
s1 = {"a": 2, "b": {"c": 3}}
115115

116-
diff = diff_get(s0, s1)
116+
diff = Diffr(s0, s1)
117117

118118
# Test with StringIO
119119
output = io.StringIO()
@@ -128,7 +128,7 @@ def test_diff_all_output(self):
128128
s0 = {"a": 1}
129129
s1 = {"a": 2}
130130

131-
diff = diff_get(s0, s1)
131+
diff = Diffr(s0, s1)
132132

133133
# Test with StringIO
134134
output = io.StringIO()
@@ -140,14 +140,14 @@ def test_diff_all_output(self):
140140
def test_type_assertion(self):
141141
"""Test that different types raise assertion error"""
142142
with self.assertRaises(AssertionError):
143-
diff_get({"a": 1}, ["a", 1])
143+
Diffr({"a": 1}, ["a", 1])
144144

145145
def test_ipython_key_completions(self):
146146
"""Test IPython tab completion support"""
147147
s0 = {"a": 1, "b": 2, "c": 3}
148148
s1 = {"a": 1, "b": 3, "d": 4}
149149

150-
diff = diff_get(s0, s1)
150+
diff = Diffr(s0, s1)
151151
completions = diff._ipython_key_completions_()
152152

153153
assert set(completions) == {"a", "b"}
@@ -158,7 +158,7 @@ class TestCLI(unittest.TestCase):
158158

159159
def test_main_function_exists(self):
160160
"""Test that main function exists and is callable"""
161-
from diffgetr.diff_get import main
161+
from diffgetr.Diffr import main
162162
self.assertTrue(callable(main))
163163

164164

@@ -169,7 +169,7 @@ def test_uuid_pattern_replacement(self):
169169
s0 = {"id": "550e8400-e29b-41d4-a716-446655440000"}
170170
s1 = {"id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8"}
171171

172-
diff = diff_get(s0, s1)
172+
diff = Diffr(s0, s1)
173173
output = io.StringIO()
174174
diff.diff_summary(file=output)
175175
summary = output.getvalue()

0 commit comments

Comments
 (0)