Context#

TODO

Context classes#

class caterpillar.context.Context[source]#

Represents a context object with attribute-style access.

class caterpillar.context.ContextPath(path: str = None)[source]#

Represents a lambda function for retrieving a value from a Context based on a specified path.

class caterpillar.context.ContextLength(path: ContextPath)[source]#
class caterpillar.context.ConditionContext(condition: _ContextLambda | bool, depth=2)[source]#

Class implementation of an inline condition.

Use this class to automatically apply a condition to multiple field definitions. Note that this class will only work if it has access to the parent stack frame.

@struct
class Format:
    magic: b"MGK"
    length: uint32

    with this.length > 32:
        # other field definitions here
        foo: uint8

This class will replace any existing fields!

Parameters:

condition (Union[_ContextLambda, bool]) – a context lambda or constant boolean value

Special paths#

context.this#

Represents a lambda function for retrieving a value from a Context based on a specified path.

context.ctx#

Represents a lambda function for retrieving a value from a Context based on a specified path.

context.parent#

Represents a lambda function for retrieving a value from a Context based on a specified path.

Special Attributes#

context.CTX_PARENT = '_parent'#
context.CTX_OBJECT = '_obj'#
context.CTX_OFFSETS = '_offsets'#
context.CTX_STREAM = '_io'#
context.CTX_FIELD = '_field'#
context.CTX_VALUE = '_value'#
context.CTX_POS = '_pos'#
context.CTX_INDEX = '_index'#
context.CTX_PATH = '_path'#
context.CTX_SEQ = '_is_seq'#

Expressions#

class caterpillar.context.ExprMixin[source]#

A mixin class providing methods for creating binary and unary expressions.

class caterpillar.context.BinaryExpression(operand: Callable[[Any, Any], Any], left: Any | _ContextLambda, right: Any | _ContextLambda)[source]#

Represents a binary expression.

Parameters:
  • operand – The binary operator function.

  • left – The left operand.

  • right – The right operand.

class caterpillar.context.UnaryExpression(name: str, operand: Callable[[Any], Any], value: Any | _ContextLambda)[source]#

Represents a unary expression.

Parameters:
  • name – The name of the unary operator.

  • operand – The unary operator function.

  • value – The operand.