MCPcopy Create free account
hub / github.com/ScrapeGraphAI/toonify / example_list_of_models

Function example_list_of_models

examples/pydantic_usage.py:70–98  ·  view source on GitHub ↗

Example: List of uniform Pydantic models (tabular format).

()

Source from the content-addressed store, hash-verified

68
69
70def 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
101def example_nested_models():

Callers 1

pydantic_usage.pyFile · 0.85

Calls 3

encodeFunction · 0.90
encode_pydanticFunction · 0.90
ProductClass · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…