Example: List of uniform Pydantic models (tabular format).
()
| 68 | |
| 69 | |
| 70 | def example_list_of_models(): |
| 71 | """Example: List of uniform Pydantic models (tabular format).""" |
| 72 | print("=== List of Pydantic Models (Tabular) ===") |
| 73 | |
| 74 | products = [ |
| 75 | Product(sku='LAP-001', name='Gaming Laptop', price=1299.99, stock=15, tags=['electronics', 'computers']), |
| 76 | Product(sku='MOU-042', name='Wireless Mouse', price=29.99, stock=128, tags=['electronics', 'accessories']), |
| 77 | Product(sku='KEY-789', name='Mechanical Keyboard', price=149.99, stock=67, tags=['electronics', 'accessories']) |
| 78 | ] |
| 79 | |
| 80 | print("Python objects:") |
| 81 | for p in products: |
| 82 | print(f" {p.sku}: {p.name} - ${p.price}") |
| 83 | print() |
| 84 | |
| 85 | # Compare with regular dict encoding |
| 86 | dict_data = {'products': [p.model_dump() if hasattr(p, 'model_dump') else p.dict() for p in products]} |
| 87 | json_str = json.dumps(dict_data) |
| 88 | toon_dict = encode(dict_data) |
| 89 | toon_pydantic = encode_pydantic(products) |
| 90 | |
| 91 | print(f"JSON size: {len(json_str)} bytes") |
| 92 | print(f"TOON size (from dict): {len(toon_dict)} bytes") |
| 93 | print(f"TOON size (from pydantic): {len(toon_pydantic)} bytes") |
| 94 | print() |
| 95 | |
| 96 | print("TOON format:") |
| 97 | print(toon_pydantic) |
| 98 | print() |
| 99 | |
| 100 | |
| 101 | def example_nested_models(): |
no test coverage detected
searching dependent graphs…