API reference
DataclassStructInternal
Bases: Generic[T]
Source code in dataclasses_struct/dataclass.py
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
format
property
The format string used by the struct
module to pack/unpack data.
See https://docs.python.org/3/library/struct.html#format-strings.
mode
property
The struct
mode character that determines size, alignment, and
byteorder.
This is the first character of the format
field. See
https://docs.python.org/3/library/struct.html#byte-order-size-and-alignment
for more info.
size
property
Size of the packed representation in bytes.
DataclassStructProtocol
Bases: Protocol
Source code in dataclasses_struct/dataclass.py
__dataclass_struct__
class-attribute
Internal data used by the library for packing and unpacking structs.
from_packed(data)
classmethod
Return an instance of the class from its packed representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Buffer
|
The packed representation of the class as returned by
|
required |
Returns:
Type | Description |
---|---|
T
|
An instance of the class unpacked from |
Raises:
Type | Description |
---|---|
error
|
If |
Source code in dataclasses_struct/dataclass.py
pack()
Return the packed representation in bytes
of the object.
Returns:
Type | Description |
---|---|
bytes
|
The packed representation. Can be used to instantiate a new object
with
|
Raises:
Type | Description |
---|---|
error
|
If any of the fields are out of range or the wrong type. |
Source code in dataclasses_struct/dataclass.py
dataclass_struct(*, size='native', byteorder='native', validate_defaults=True, **dataclass_kwargs)
Create a dataclass struct.
Should be used as a decorator on a class:
import dataclasses_struct as dcs
@dcs.dataclass_struct()
class A:
data: dcs.Pointer
size: dcs.UnsignedSize
The allowed size
and byteorder
argument combinations are as as follows.
size |
byteorder |
Notes |
---|---|---|
"native" |
"native" |
The default. Native alignment and padding. |
"std" |
"native" |
Standard integer sizes and system endianness, no alignment/padding. |
"std" |
"little" |
Standard integer sizes and little endian, no alignment/padding. |
"std" |
"big" |
Standard integer sizes and big endian, no alignment/padding. |
"std" |
"network" |
Equivalent to byteorder="big" . |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
Literal['native', 'std']
|
The size mode. |
'native'
|
byteorder
|
Literal['native', 'big', 'little', 'network']
|
The byte order of the generated struct. If |
'native'
|
validate_defaults
|
bool
|
Whether to validate the default values of any fields. |
True
|
dataclass_kwargs
|
Unpack[DataclassKwargs]
|
Any additional keyword arguments to pass to the
stdlib
|
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
If the |
TypeError
|
If any of the fields' type annotations are invalid or not supported. |
Source code in dataclasses_struct/dataclass.py
get_struct_size(cls_or_obj)
Get the size of the packed representation of the struct in bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls_or_obj
|
object
|
A class that has been decorated with
|
required |
Returns:
Type | Description |
---|---|
int
|
The size of the packed representation in bytes. |
Raises:
Type | Description |
---|---|
TypeError
|
if |
Source code in dataclasses_struct/dataclass.py
is_dataclass_struct(obj)
Determine whether a type or object is a dataclass-struct.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Union[type, object]
|
A class or object. |
required |
Returns:
Type | Description |
---|---|
Union[TypeGuard[DataclassStructProtocol], TypeGuard[type[DataclassStructProtocol]]]
|
|