Smart Dictionary Compression

This example demonstrates how to use Smart Dictionary Compression (SDC) to compress repetitive data structures while maintaining a JSON-serializable format.

 1"""Example of Smart Dictionary Compression (SDC).
 2
 3This example demonstrates how to use SDC to compress repetitive data structures
 4while maintaining a JSON-serializable format.
 5"""
 6
 7import json
 8import toonverter as toon
 9from toonverter.optimization import SmartCompressor
10
11
12def run_compression_example():
13    """Run compression example."""
14    print("--- Smart Dictionary Compression (SDC) Example ---\n")
15
16    # 1. Create repetitive data
17    # A list of objects with same keys and repeated values
18    data = {
19        "users": [
20            {
21                "id": 1,
22                "name": "Alice",
23                "role": "admin",
24                "department": "engineering",
25                "active": True,
26                "preferences": {"theme": "dark", "notifications": True}
27            },
28            {
29                "id": 2,
30                "name": "Bob",
31                "role": "user",
32                "department": "sales",
33                "active": True,
34                "preferences": {"theme": "light", "notifications": True}
35            },
36            {
37                "id": 3,
38                "name": "Charlie",
39                "role": "user",
40                "department": "engineering",
41                "active": False,
42                "preferences": {"theme": "dark", "notifications": False}
43            },
44            # Repeat pattern to show benefits
45            {
46                "id": 4,
47                "name": "Dave",
48                "role": "user",
49                "department": "sales",
50                "active": True,
51                "preferences": {"theme": "light", "notifications": True}
52            }
53        ] * 5  # Multiply to simulate larger dataset
54    }
55
56    print(f"Original Items: {len(data['users'])}")
57
58    # 2. Compress the data
59    print("\nCompressing data...")
60    compressed = toon.compress(data)
61
62    # Show compressed structure structure
63    print("\nCompressed Structure Keys:", compressed.keys())
64    # Keys will be: '_sdc_schema', '_sdc_table', etc.
65
66    # 3. Compare sizes (serialized JSON)
67    original_json = json.dumps(data)
68    compressed_json = json.dumps(compressed)
69    
70    orig_size = len(original_json)
71    comp_size = len(compressed_json)
72    
73    print(f"\nOriginal JSON Size: {orig_size} chars")
74    print(f"Compressed JSON Size: {comp_size} chars")
75    print(f"Reduction: {(orig_size - comp_size) / orig_size * 100:.1f}%")
76
77    # 4. Decompress back to original
78    print("\nDecompressing...")
79    restored = toon.decompress(compressed)
80
81    # Verify integrity
82    if restored == data:
83        print("SUCCESS: Data restored perfectly!")
84    else:
85        print("ERROR: Data mismatch!")
86
87    # 5. Using the Class directly for advanced options
88    print("\n--- Advanced Usage ---")
89    compressor = SmartCompressor()
90    
91    # Analyze potential savings without compressing
92    if compressor.should_compress(data):
93        print("Analyzer suggests compression is beneficial.")
94    else:
95        print("Analyzer suggests compression is NOT beneficial.")
96
97
98if __name__ == "__main__":
99    run_compression_example()