|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """ |
3 | | -Self-contained demo of pydsdl serialization and deserialization. |
4 | | -
|
5 | | -Demonstrates the complete workflow: |
6 | | -1. Load a custom DSDL type using read_files() |
7 | | -2. Create an object using the representation convention (dict, list, str, bytes, primitives) |
8 | | -3. Serialize the object to bytes |
9 | | -4. Deserialize the bytes back to an object |
10 | | -5. Verify roundtrip equality |
| 3 | +Self-contained demo of PyDSDL serialization. |
11 | 4 | """ |
12 | 5 |
|
13 | 6 | import pydsdl |
14 | 7 | from pathlib import Path |
15 | 8 |
|
| 9 | +SCRIPT_DIR = Path(__file__).parent |
| 10 | +DSDL_FILE = SCRIPT_DIR / "DemoMessage.1.0.dsdl" |
16 | 11 |
|
17 | | -def main() -> None: |
18 | | - SCRIPT_DIR = Path(__file__).parent |
19 | | - DSDL_FILE = SCRIPT_DIR / "DemoMessage.1.0.dsdl" |
20 | | - |
21 | | - print("=" * 70) |
22 | | - print("PyDSDL Serialization/Deserialization Demo") |
23 | | - print("=" * 70) |
24 | 12 |
|
25 | | - print("\n[Step 1] Loading DSDL type from:", DSDL_FILE.name) |
26 | | - |
| 13 | +def main() -> None: |
| 14 | + print("Loading DSDL type from:", DSDL_FILE.name) |
27 | 15 | types, _ = pydsdl.read_files( |
28 | 16 | dsdl_files=DSDL_FILE, |
29 | 17 | root_namespace_directories_or_names=SCRIPT_DIR, |
30 | | - lookup_directories=[] |
| 18 | + lookup_directories=[], |
31 | 19 | ) |
32 | | - |
33 | 20 | schema = types[0] |
34 | 21 | print(f"✓ Loaded type: {schema.full_name} v{schema.version.major}.{schema.version.minor}") |
35 | 22 | print(f" Fields: {[f.name for f in schema.fields_except_padding]}") |
36 | 23 |
|
37 | | - print("\n[Step 2] Creating example object") |
38 | | - print(" Representation convention: dict→struct, list→array, primitives as-is") |
| 24 | + print("Creating example object:") |
39 | 25 | obj = { |
40 | 26 | "flag": True, |
41 | 27 | "counter": 42, |
42 | 28 | "temperature": 23.5, |
43 | | - "numeric_data": [1.0, 2.0, 3.0, 4.0], |
44 | | - "text_data": list("Hello, SerDes!".encode("utf-8")), |
45 | | - "binary_data": [0x00, 0x01, 0x02, 0x03] |
| 29 | + "numeric_data": [1.0, 2.0, 3, 4], |
| 30 | + "text_data": "Hello, SerDes!", |
| 31 | + "binary_data": b"\x00\x01\x02\x03", |
46 | 32 | } |
47 | | - |
48 | | - print(" Object structure:") |
49 | 33 | for key, value in obj.items(): |
50 | 34 | value_repr = repr(value) |
51 | 35 | if len(value_repr) > 50: |
52 | 36 | value_repr = value_repr[:47] + "..." |
53 | 37 | print(f" {key:15} = {value_repr} ({type(value).__name__})") |
54 | 38 |
|
55 | | - print("\n[Step 3] Serializing object to bytes") |
| 39 | + print("Serializing object to bytes") |
56 | 40 | serialized_data = pydsdl.serialize(schema, obj) |
57 | | - print(f"✓ Serialized to {len(serialized_data)} bytes") |
58 | | - print(f" Hex: {serialized_data.hex()}") |
| 41 | + print(f"✓ Serialized to {len(serialized_data)} bytes:") |
| 42 | + print(f" {serialized_data.hex()}") |
59 | 43 |
|
60 | | - print("\n[Step 4] Deserializing bytes back to object") |
| 44 | + print("Deserializing bytes back to object") |
61 | 45 | deserialized = pydsdl.deserialize(schema, serialized_data) |
62 | | - print("✓ Deserialized successfully") |
63 | | - print(" Deserialized structure:") |
| 46 | + print("✓ Deserialized successfully:") |
64 | 47 | for key, value in deserialized.items(): |
65 | 48 | value_repr = repr(value) |
66 | 49 | if len(value_repr) > 50: |
67 | 50 | value_repr = value_repr[:47] + "..." |
68 | 51 | print(f" {key:15} = {value_repr} ({type(value).__name__})") |
69 | 52 |
|
70 | | - print("\n[Step 5] Verifying roundtrip equality") |
| 53 | + print("Verifying roundtrip equality") |
71 | 54 | assert obj == deserialized, "Roundtrip failed! Objects don't match." |
72 | 55 | print("✓ Roundtrip verification passed: Original == Deserialized") |
73 | 56 |
|
74 | | - print("\n" + "=" * 70) |
75 | | - print("Demo completed successfully!") |
76 | | - print("=" * 70) |
77 | | - |
78 | 57 |
|
79 | 58 | if __name__ == "__main__": |
80 | 59 | main() |
0 commit comments