Structure Collection¶
[1]:
from byteclasses import ByteOrder
from byteclasses.print import byteclass_info, byteclass_inspect
from byteclasses.types.collections import ByteclassCollection, member, structure
from byteclasses.types.primitives.characters import UChar
from byteclasses.types.primitives.floats import Float32
from byteclasses.types.primitives.generics import BitField, Byte
from byteclasses.types.primitives.integers import Int8, UInt8, UInt16, UInt32
Basic Structure
[2]:
@structure
class BasicStructure:
"""A basic structure byteclass."""
var1: UInt8
var2: UInt16
[3]:
bs = BasicStructure()
bs.data = b"\x00\x01\x02\x03"
byteclass_info(bs)
byteclass_inspect(bs)
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ BasicStructure │ │ is_byteclass() │ True │ │ is_collection_instance() │ True │ │ is_primitive_instance() │ False │ │ mro │ BasicStructure -> object │ │ len() │ 4 │ │ str() │ BasicStructure(var1=UInt8(0), var2=UInt16(770)) │ │ repr() │ BasicStructure(byte_order=b'@',data=bytearray(b'\x00\x01\x02\x03')) │ │ .data │ bytearray(b'\x00\x01\x02\x03') │ └──────────────────────────┴─────────────────────────────────────────────────────────────────────┘
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |00|01|02 03 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━┩ │ var1 │ 0 │ │ var2 │ 770 │ └────────┴───────┘
Packed Basic Structure
[4]:
@structure(packed=True)
class PackedStructure:
"""A Packed structure byteclass."""
var1: UInt8
var2: UInt16
[5]:
ps = PackedStructure()
byteclass_info(ps)
byteclass_inspect(ps)
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ PackedStructure │ │ is_byteclass() │ True │ │ is_collection_instance() │ True │ │ is_primitive_instance() │ False │ │ mro │ PackedStructure -> object │ │ len() │ 3 │ │ str() │ PackedStructure(var1=UInt8(0), var2=UInt16(0)) │ │ repr() │ PackedStructure(byte_order=b'@',data=bytearray(b'\x00\x00\x00')) │ │ .data │ bytearray(b'\x00\x00\x00') │ └──────────────────────────┴──────────────────────────────────────────────────────────────────┘
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |00|00 00 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━┩ │ var1 │ 0 │ │ var2 │ 0 │ └────────┴───────┘
Simple Structure
[6]:
@structure(byte_order=b"!")
class SimpleStruct:
a: UInt8
b: UInt16
c: UChar
d: Float32
e: UInt8
ss1: ByteclassCollection = SimpleStruct()
[7]:
byteclass_info(ss1)
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ SimpleStruct │ │ is_byteclass() │ True │ │ is_collection_instance() │ True │ │ is_primitive_instance() │ False │ │ mro │ SimpleStruct -> object │ │ len() │ 13 │ │ str() │ SimpleStruct(a=UInt8(0), b=UInt16(0), c=UChar('\x00'), d=Float32(0.0), e=UInt8(0)) │ │ repr() │ SimpleStruct(byte_order=b'!',data=bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\… │ │ .data │ bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') │ └──────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────┘
[8]:
byteclass_inspect(ss1)
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |00|00|00 00|00|00 00 00|00 00 00 00|00 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━┩ │ a │ 0 │ │ b │ 0 │ │ c │ │ │ d │ 0.0 │ │ e │ 0 │ └────────┴───────┘
Direct data access
[9]:
ss1.data = b"\x00\x00\x00\x00\x00\x00\x00\x00\xcd\xcc\x8c?\x00"
[10]:
byteclass_inspect(ss1)
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |00|00|00 00|00|00 00 00|cd cc 8c 3f|00 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━━━━━━━━┩ │ a │ 0 │ │ b │ 0 │ │ c │ │ │ d │ -428967904.0 │ │ e │ 0 │ └────────┴──────────────┘
[11]:
ss2 = SimpleStruct()
ss2.a = 1
ss2.b = 2
ss2.c = "c"
ss2.d = 4
[12]:
byteclass_inspect(ss2)
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |01|00|00 02|63|00 00 00|40 80 00 00|00 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━┩ │ a │ 1 │ │ b │ 2 │ │ c │ c │ │ d │ 4.0 │ │ e │ 0 │ └────────┴───────┘
Member Assignment
[13]:
print(ss1.a.endianness)
ss1.a = 1
ss1.b = 2
ss1.c = "c"
ss1.d = 4
NET
[14]:
byteclass_inspect(ss1)
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |01|00|00 02|63|00 00 00|40 80 00 00|00 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━┩ │ a │ 1 │ │ b │ 2 │ │ c │ c │ │ d │ 4.0 │ │ e │ 0 │ └────────┴───────┘
Index Assignment
[15]:
ss1[1] = 6
ss1[2] = 7
ss1[3] = 8
[16]:
byteclass_inspect(ss1)
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |01|06|07 08|63|00 00 00|40 80 00 00|00 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━┩ │ a │ 1 │ │ b │ 1800 │ │ c │ c │ │ d │ 4.0 │ │ e │ 0 │ └────────┴───────┘
Nested Collections
[17]:
@structure
class InnerStruct:
var1: BitField
var2: Byte
[18]:
@structure(byte_order=ByteOrder.NATIVE, packed=True)
class OuterStruct:
var3: Int8
var4: UInt32
var5: Float32
var6: InnerStruct = member(factory=InnerStruct)
def test(self):
print("test")
[19]:
nested_struct = OuterStruct()
byteclass_info(nested_struct)
byteclass_inspect(nested_struct)
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ OuterStruct │ │ is_byteclass() │ True │ │ is_collection_instance() │ True │ │ is_primitive_instance() │ False │ │ mro │ OuterStruct -> object │ │ len() │ 11 │ │ str() │ OuterStruct(var3=Int8(0), var4=UInt32(0), var5=Float32(0.0), │ │ │ var6=InnerStruct(byte_order=b'@',data=bytearray(b'\x00\x00'))) │ │ repr() │ OuterStruct(byte_order=b'@',data=bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x… │ │ .data │ bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') │ └──────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────┘
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |00|00 00 00 00|00 00 00 00|00 00 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ var3 │ 0 │ │ var4 │ 0 │ │ var5 │ 0.0 │ │ var6 │ InnerStruct(var1=BitField(data=b'\x00'), var2=Byte(data=b'\x00', byte_order=b'@')) │ └────────┴────────────────────────────────────────────────────────────────────────────────────┘
Member Assignment
[20]:
nested_struct.var4 = 1
byteclass_inspect(nested_struct)
byteclass_inspect(nested_struct.var6)
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |00|01 00 00 00|00 00 00 00|00 00 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ var3 │ 0 │ │ var4 │ 1 │ │ var5 │ 0.0 │ │ var6 │ InnerStruct(var1=BitField(data=b'\x00'), var2=Byte(data=b'\x00', byte_order=b'@')) │ └────────┴────────────────────────────────────────────────────────────────────────────────────┘
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |00|00 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ var1 │ BitField(00000000, flags={}) │ │ var2 │ b'\x00' │ └────────┴──────────────────────────────┘
Data Attribute Assignment
[21]:
nested_struct.data = b"\xfa\xf9\xf8\xf7\xf6\xf5\xf4\xf3\xf2\xf1\xf0"
byteclass_inspect(nested_struct)
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |fa|f9 f8 f7 f6|f5 f4 f3 f2|f1 f0 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ var3 │ -6 │ │ var4 │ 4143446265 │ │ var5 │ -9.664127010094224e+30 │ │ var6 │ InnerStruct(var1=BitField(data=b'\xf1'), var2=Byte(data=b'\xf0', byte_order=b'@')) │ └────────┴────────────────────────────────────────────────────────────────────────────────────┘
Slice Assignment
[22]:
nested_struct[2:5] = b"\x08"
byteclass_inspect(nested_struct)
╭───────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ---------------------------------------------------- │ │ 0x0 |fa|f9 08 08 08|f5 f4 f3 f2|f1 f0 │ ╰──────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ var3 │ -6 │ │ var4 │ 134744313 │ │ var5 │ -9.664127010094224e+30 │ │ var6 │ InnerStruct(var1=BitField(data=b'\xf1'), var2=Byte(data=b'\xf0', byte_order=b'@')) │ └────────┴────────────────────────────────────────────────────────────────────────────────────┘