binobj.pep526 module¶
Support for declaring fields in structs using PEP 526 variable annotations.
You can use the dataclass()
decorator on your Struct
.
If you use this decorator, all fields must be declared with PEP 526 annotations. You can’t mix this system with the original assignment-based one.
Here are a few examples of how you can declare your fields:
@binobj.dataclass
class MyStruct(binobj.Struct):
# Preferred: use a class object
foo: UInt16
# You can define default values like this
bar: StringZ = ""
# You can pass struct classes -- no need for a `Nested` wrapper. Forward
# references using strings are *not* supported.
sub_struct: MyOtherStruct
# Instances are allowed but are less readable. Be careful not to *assign*
# the field instance!
baz: Timestamp64(signed=False)
New in version 0.9.0.
Deprecated since version 0.11.0: Do not pass a callable as the default value, use the factory
argument to the
field instead.
- dataclass(class_object: Type[TStruct]) Type[TStruct] ¶
Mark a Struct as using PEP 526 declarations for its fields.
If you use this decorator, all fields must be declared with PEP 526 annotations. You can’t mix this system with the original assignment-based one.
Here are a few examples of how you can declare your fields:
class MyStruct(binobj.Struct): # Preferred: use a class object foo: UInt16 # You can define default values like this bar: StringZ = "" # You can pass struct classes -- no need for a `Nested` wrapper. Forward # references using strings are *not* supported. sub_struct: MyOtherStruct # Instances are allowed but are less readable. Be careful not to *assign* # the field instance! baz: Timestamp64(signed=False)
New in version 0.9.0.