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

Function example_roundtrip

examples/pydantic_usage.py:200–235  ·  view source on GitHub ↗

Example: Round-trip conversion.

()

Source from the content-addressed store, hash-verified

198
199
200def example_roundtrip():
201 """Example: Round-trip conversion."""
202 print("=== Round-trip Conversion ===")
203
204 original = [
205 Product(sku='KEY-001', name='Wireless Keyboard', price=79.99, stock=45, tags=['wireless', 'keyboard']),
206 Product(sku='MOU-002', name='Gaming Mouse', price=59.99, stock=78, tags=['gaming', 'mouse']),
207 ]
208
209 print("Original objects:")
210 for p in original:
211 print(f" {p.sku}: {p.name} - ${p.price} (stock: {p.stock})")
212 print()
213
214 # Encode to TOON
215 toon = encode_pydantic(original)
216 print("TOON format:")
217 print(toon)
218 print()
219
220 # Decode back to Pydantic
221 decoded = decode_to_pydantic(toon, Product)
222 print("Decoded objects:")
223 for p in decoded:
224 print(f" {p.sku}: {p.name} - ${p.price} (stock: {p.stock})")
225 print()
226
227 # Verify equality
228 print("Round-trip successful:", all(
229 orig.sku == dec.sku and
230 orig.name == dec.name and
231 orig.price == dec.price and
232 orig.stock == dec.stock
233 for orig, dec in zip(original, decoded)
234 ))
235 print()
236
237
238def example_comparison():

Callers 1

pydantic_usage.pyFile · 0.85

Calls 3

encode_pydanticFunction · 0.90
decode_to_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…