Example: Round-trip conversion.
()
| 198 | |
| 199 | |
| 200 | def 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 | |
| 238 | def example_comparison(): |
no test coverage detected
searching dependent graphs…