-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_python.py
More file actions
97 lines (82 loc) · 3.58 KB
/
Copy pathverify_python.py
File metadata and controls
97 lines (82 loc) · 3.58 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
import json
import os
import sys
from toon_parser import encode, decode
def deep_equal(obj1, obj2, path=""):
if type(obj1) != type(obj2):
print(f"Type mismatch at {path}: {type(obj1)} vs {type(obj2)}")
return False
if isinstance(obj1, dict):
if len(obj1) != len(obj2):
print(f"Dict length mismatch at {path}: {len(obj1)} keys vs {len(obj2)} keys")
keys1 = set(obj1.keys())
keys2 = set(obj2.keys())
if keys1 != keys2:
print(f"Missing keys in 1: {keys2 - keys1}")
print(f"Missing keys in 2: {keys1 - keys2}")
return False
for key in obj1:
if key not in obj2:
print(f"Key {key} missing at {path}")
return False
if not deep_equal(obj1[key], obj2[key], f"{path}.{key}"):
return False
return True
if isinstance(obj1, list):
if len(obj1) != len(obj2):
print(f"List length mismatch at {path}: {len(obj1)} vs {len(obj2)}")
return False
for i in range(len(obj1)):
if not deep_equal(obj1[i], obj2[i], f"{path}[{i}]"):
return False
return True
if obj1 != obj2:
print(f"Value mismatch at {path}: {repr(obj1)} vs {repr(obj2)}")
return False
return True
def run_verification():
base_dir = "/home/johnpaez/TOON"
json_path = os.path.join(base_dir, "test.json")
toon_path = os.path.join(base_dir, "test.toon")
print("--- Loading Ground Truth ---")
with open(json_path, 'r') as f:
original_json = json.load(f)
print(f"Loaded {json_path}")
# 1. Decode test.toon and compare with test.json
print("\n--- Phase 1: Decoding test.toon ---")
if os.path.exists(toon_path):
with open(toon_path, 'r') as f:
toon_content = f.read()
decoded_from_toon = decode(toon_content)
if deep_equal(original_json, decoded_from_toon):
print("✅ SUCCESS: Decoded test.toon matches test.json")
else:
print("❌ FAILURE: Decoded test.toon does NOT match test.json")
# Save for inspection
with open("python_decoded_from_toon.json", "w") as f:
json.dump(decoded_from_toon, f, indent=2)
else:
print("Skipping Phase 1: test.toon not found.")
# 2. Roundtrip test.json (Encode -> Decode)
print("\n--- Phase 2: Roundtrip test.json (Encode -> Decode) ---")
encoded_toon = encode(original_json)
with open("python_encoded.toon", "w") as f:
f.write(encoded_toon)
roundtrip_json = decode(encoded_toon)
if deep_equal(original_json, roundtrip_json):
print("✅ SUCCESS: Roundtrip (Encode -> Decode) successful")
original_file_size = os.path.getsize(json_path)
encoded_size = len(encoded_toon)
compact_json_size = len(json.dumps(original_json))
reduction_vs_file = (original_file_size - encoded_size) / original_file_size * 100
reduction_vs_compact = (compact_json_size - encoded_size) / compact_json_size * 100
print(f"Original JSON Size (Indented): {original_file_size} bytes")
print(f"TOON Size: {encoded_size} bytes")
print(f"Reduction vs Indented JSON: {reduction_vs_file:.2f}%")
print(f"Reduction vs Compact JSON: {reduction_vs_compact:.2f}%")
else:
print("❌ FAILURE: Roundtrip mismatch")
with open("python_roundtrip_fail.json", "w") as f:
json.dump(roundtrip_json, f, indent=2)
if __name__ == "__main__":
run_verification()