-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
159 lines (126 loc) · 5 KB
/
Copy pathtest.py
File metadata and controls
159 lines (126 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Example demonstrating the new simplified AF-Serializer API.
The key features are:
1. lvflatten(obj) - Serialize any @lvclass instance to bytes
2. lvunflatten(data) - Automatically identify and populate the correct class
"""
from af_serializer import (
lvclass, lvflatten, lvunflatten,
LVU16, LVI32, LVString, LVBoolean, LVDouble, LVArray, LVU8
)
# Define LabVIEW class hierarchy using @lvclass decorator
@lvclass(library="Actor Framework", class_name="Message")
class Message:
pass
@lvclass(library="Serializable Message", class_name="Serializable Msg",
version=(1, 0, 0, 7))
class SerializableMsg(Message):
pass
@lvclass(library="Commander", class_name="echo general Msg")
class EchoMsg(SerializableMsg):
message: str # → LVString
code: LVU16 # → U16 (2 bytes)
# ============================================================================
# Example: Simple roundtrip serialization/deserialization
# ============================================================================
print("=== Example: Simple Roundtrip ===")
msg = EchoMsg()
msg.message = "Hello World!"
msg.code = 42
# Serialize with lvflatten()
data = lvflatten(msg)
print(f"Serialized bytes: {data.hex()}")
# Deserialize with lvunflatten() - NO parameters needed!
restored = lvunflatten(data)
print(f"Restored type: {type(restored).__name__}")
print(f"Message: {restored.message}")
print(f"Code: {restored.code}")
# Verify
assert isinstance(restored, EchoMsg)
assert restored.message == "Hello World!"
assert restored.code == 42
print("✓ Roundtrip successful!")
# ============================================================================
# Example: Simple deserialization from HEX data
# ============================================================================
print("=== Example: Simple deserialization from HEX data ===")
@lvclass(version=(1, 0, 0, 6))
class Test():
pass
@lvclass(version=(1, 0, 0, 4))
class Child(Test):
boolean: LVBoolean
number: LVI32
double: LVDouble
text: LVString
number_array: LVArray(LVU8)
string_array: LVArray(LVString)
data = bytes.fromhex("0000 0002 0F0D 4368 696C 642E 6C76 636C 6173 7300 0001 0000 0000 0004 0001 0000 0000 0006 0000 0000 0000 0042 0100 0002 6640 091E B851 EB85 1F00 0000 0C48 656C 6C6F 2050 7974 686F 6E00 0000 0336 374A 0000 0003 0000 0005 4861 62ED 6100 0000 0375 F161 0000 0006 7665 7A2E 2E2E ")
print(f"Serialized bytes: {data.hex()}")
child = Child()
child.boolean = True
child.number = 614
child.double = 3.14
child.text = "Hello Python"
child.number_array = [54,55,74]
child.string_array = ["Había","uña","vez..."]
# Deserialize with lvunflatten() - NO parameters needed!
restored = lvunflatten(data)
print(f"Restored type: {type(restored).__name__}")
#print(f"{restored.__annotations__}")
print(f"Boolean: {restored.boolean}")
print(f"Number: {restored.number}")
print(f"Double: {restored.double}")
print(f"Text: {restored.text}")
print(f"Number Array: {restored.number_array}")
print(f"String Array: {restored.string_array}")
# Verify
assert isinstance(restored, Child)
assert restored.boolean == child.boolean
assert restored.number == child.number
assert abs(restored.double - child.double) < 1e-10
assert restored.text == child.text
assert all(a == b for a, b in zip(restored.number_array, child.number_array))
print("✓ Deserialization successful!")
# ============================================================================
# Example: 3-level inheritance
# ============================================================================
print("\n=== Example: 3-Level Inheritance ===")
# The EchoMsg class inherits from SerializableMsg -> Message
# This creates 3 levels in the LabVIEW Object
msg2 = EchoMsg()
msg2.message = "Testing 3-level inheritance"
msg2.code = 123
data2 = lvflatten(msg2)
print(f"Serialized 3-level object: {data2[:8].hex()}...") # Show first 8 bytes
# First 4 bytes are NumLevels
num_levels = int.from_bytes(data2[:4], 'big')
print(f"NumLevels in binary: {num_levels}")
assert num_levels == 3
restored2 = lvunflatten(data2)
assert isinstance(restored2, EchoMsg)
assert restored2.message == "Testing 3-level inheritance"
assert restored2.code == 123
print("✓ 3-level inheritance works correctly!")
# ============================================================================
# Example: Class with multiple field types
# ============================================================================
print("\n=== Example: Multiple Field Types ===")
@lvclass(library="MyLib", class_name="ComplexClass")
class ComplexClass:
name: str
count: LVI32
value: LVU16
obj = ComplexClass()
obj.name = "Test Object"
obj.count = 100
obj.value = 65535
data3 = lvflatten(obj)
restored3 = lvunflatten(data3)
assert isinstance(restored3, ComplexClass)
assert restored3.name == "Test Object"
assert restored3.count == 100
assert restored3.value == 65535
print(f"Restored: name={restored3.name}, count={restored3.count}, value={restored3.value}")
print("✓ Multiple field types work correctly!")
print("\n=== All examples completed successfully! ===")