Sync System

SyncRule

class pystructs.sync.SyncRule(target: str, from_field: str | None = None, from_fields: List[str] | None = None, compute: Callable | None = None)[source]

Bases: object

Rule for synchronizing field values.

SyncRule defines how one field’s value should be computed from other fields. Rules are executed when sync() is called on a struct.

Examples:
>>> class Packet(Struct):
...     class Meta:
...         sync_rules = [
...             # Update payload_size from payload length
...             SyncRule('payload_size', from_field='payload', compute=len),
...             # Update checksum from payload
...             SyncRule('checksum', compute=lambda self: crc32(self.payload)),
...         ]
...     payload_size = UInt16()
...     payload = Bytes(size=Ref('payload_size'))
...     checksum = UInt32()
apply(instance: Struct) None[source]

Apply this rule to update the target field.

Args:

instance: The struct instance to update