Union Collection

[1]:
from byteclasses.print import byteclass_info, byteclass_inspect
from byteclasses.types.collections import ByteclassCollection, String, member, union
from byteclasses.types.primitives.integers import Int8, Int16, UInt8, UInt16, UInt32, UInt64

Basic Union

[2]:
@union
class BasicUnion:
    """A basic union byteclass."""

    var1: UInt8
    var2: UInt16
[3]:
bu = BasicUnion()
byteclass_info(bu)
byteclass_inspect(bu)
                                    Byteclass Info                                    
┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Property                 ┃ Value                                                   ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ type()                   │ BasicUnion                                              │
│ is_byteclass()           │ True                                                    │
│ is_collection_instance() │ True                                                    │
│ is_primitive_instance()  │ False                                                   │
│ mro                      │ BasicUnion -> object                                    │
│ len()                    │ 2                                                       │
│ str()                    │ BasicUnion(var1=UInt8(0), var2=UInt16(0))               │
│ repr()                   │ BasicUnion(byte_order=b'@',data=bytearray(b'\x00\x00')) │
│ .data                    │ bytearray(b'\x00\x00')                                  │
└──────────────────────────┴─────────────────────────────────────────────────────────┘
╭───────────────── Byteclass Inspect ──────────────────╮
│      00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │
│ ---------------------------------------------------- │
│ 0x0 |00                                              │
│ 0x0 |00 00                                           │
╰──────────────────────────────────────────────────────╯
      Legend      
┏━━━━━━━━┳━━━━━━━┓
┃ Member ┃ Value ┃
┡━━━━━━━━╇━━━━━━━┩
│ var1   │ 0     │
│ var2   │ 0     │
└────────┴───────┘

Large Union Byteclass

[4]:
@union(byte_order=b">")
class LargeUnion:
    """A large union class."""

    uint64: UInt64
    uint32: UInt32
    uint16: UInt16
    int16: Int16
    uint8: UInt8
    int8: Int8
    string: String = member(
        factory=lambda byte_order: String(64, value="The quick brown fox jumped over the lazy dog.")
    )
[5]:
lu: ByteclassCollection = LargeUnion()
lu.uint64 = 2
lu.uint32 = 4
lu.uint16 = 8
lu.int16 = 16
lu.uint8 = 32
lu.int8 = 64
byteclass_info(lu)
byteclass_inspect(lu)
                                                  Byteclass Info                                                   
┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Property                 ┃ Value                                                                                ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ type()                   │ LargeUnion                                                                           │
│ is_byteclass()           │ True                                                                                 │
│ is_collection_instance() │ True                                                                                 │
│ is_primitive_instance()  │ False                                                                                │
│ mro                      │ LargeUnion -> object                                                                 │
│ len()                    │ 64                                                                                   │
│ str()                    │ LargeUnion(uint64=UInt64(4616189635234627586), uint32=UInt32(1074790404),            │
│                          │ uint16=UInt16(16400), int16=Int16(16400), uint8=UInt8(64), int8=Int8(64),            │
│                          │ string=String(64, value='@\x10\x00\x04\x00\x00\x00\x02k brown fox jumped over the    │
│                          │ lazy dog.'))                                                                         │
│ repr()                   │ LargeUnion(byte_order=b'>',data=bytearray(b'@\x10\x00\x04\x00\x00\x00\x02k brown fox │
│                          │ jumped over the lazy                                                                 │
│                          │ dog.\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'))  │
│ .data                    │ bytearray(b'@\x10\x00\x04\x00\x00\x00\x02k brown fox jumped over the lazy            │
│                          │ dog.\x00\x00\x00\x00\x00\x00\x00\x00\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  |40 10 00 04 00 00 00 02                         │
│ 0x0  |40 10 00 04                                     │
│ 0x0  |40 10                                           │
│ 0x0  |40 10                                           │
│ 0x0  |40                                              │
│ 0x0  |40                                              │
│ 0x0  |40 10 00 04 00 00 00 02 6b 20 62 72 6f 77 6e 20 │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10 66 6f 78 20 6a 75 6d 70 65 64 20 6f 76 65 72 20  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20 74 68 65 20 6c 61 7a 79 20 64 6f 67 2e 00 00 00  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  │
╰───────────────────────────────────────────────────────╯
                      Legend                       
┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Member ┃ Value                                  ┃
┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ uint64 │ 4616189635234627586                    │
│ uint32 │ 1074790404                             │
│ uint16 │ 16400                                  │
│ int16  │ 16400                                  │
│ uint8  │ 64                                     │
│ int8   │ 64                                     │
│ string │ @k brown fox jumped over the lazy dog. │
└────────┴────────────────────────────────────────┘
[6]:
lu.uint64 = 0xFFFFFFFF00
[7]:
byteclass_inspect(lu)
╭────────────────── Byteclass Inspect ──────────────────╮
│       00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │
│ ----------------------------------------------------- │
│ 0x0  |00 00 00 ff ff ff ff 00                         │
│ 0x0  |00 00 00 ff                                     │
│ 0x0  |00 00                                           │
│ 0x0  |00 00                                           │
│ 0x0  |00                                              │
│ 0x0  |00                                              │
│ 0x0  |00 00 00 ff ff ff ff 00 6b 20 62 72 6f 77 6e 20 │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10                                                  │
│ 0x10 66 6f 78 20 6a 75 6d 70 65 64 20 6f 76 65 72 20  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20                                                  │
│ 0x20 74 68 65 20 6c 61 7a 79 20 64 6f 67 2e 00 00 00  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30                                                  │
│ 0x30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  │
╰───────────────────────────────────────────────────────╯
                        Legend                        
┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Member ┃ Value                                     ┃
┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ uint64 │ 1099511627520                             │
│ uint32 │ 255                                       │
│ uint16 │ 0                                         │
│ int16  │ 0                                         │
│ uint8  │ 0                                         │
│ int8   │ 0                                         │
│ string │ ÿÿÿÿk brown fox jumped over the lazy dog. │
└────────┴───────────────────────────────────────────┘