Expressions

Expression

class pystructs.expressions.Expression[source]

Bases: ABC

Abstract base class for expressions.

Expressions are used in validators to compute expected values.

abstractmethod evaluate(instance: Struct) Any[source]

Evaluate the expression.

Args:

instance: The struct instance to evaluate against

Returns:

The computed value

Len

class pystructs.expressions.Len(field: str)[source]

Bases: Expression

Length of a field’s value.

Examples:
>>> Consistency('payload_size', equals=Len('payload'))
evaluate(instance: Struct) int[source]

Get the length of the field value.

Args:

instance: The struct instance

Returns:

Length of the field value

Value

class pystructs.expressions.Value(field: str)[source]

Bases: Expression

Value of a field.

Examples:
>>> Consistency('checksum', equals=Value('expected_checksum'))
evaluate(instance: Struct) Any[source]

Get the field value.

Args:

instance: The struct instance

Returns:

The field value

Const

class pystructs.expressions.Const(value: Any)[source]

Bases: Expression

Constant value.

Examples:
>>> Consistency('magic', equals=Const(0xDEADBEEF))
evaluate(instance: Struct) Any[source]

Return the constant value.

Args:

instance: The struct instance (unused)

Returns:

The constant value

Checksum

class pystructs.expressions.Checksum(field: str, algorithm: str = 'crc32')[source]

Bases: Expression

Checksum of a field’s value.

Supported algorithms: ‘crc32’

Examples:
>>> Consistency('checksum', equals=Checksum('payload', 'crc32'))
evaluate(instance: Struct) int[source]

Compute the checksum of the field value.

Args:

instance: The struct instance

Returns:

The checksum value

Raises:

ValueError: If algorithm is not supported