FieldTypes¶
The FieldType class is an abstract base class for ‘field-like’ types that must support some basic operations to be used in a Format.
You shouldn’t usually need to deal with this type directly, but its methods are available for all of the other field types.
The concrete field-type classes are:
Field: An amount of binary data with a single data type, and optionally a name.Format: A sequence ofFieldTypeobjects, such asFieldor otherFormatinstances.If: Uses a condition to choose between a pair ofFieldTypeobjects.Let: Creates or modifies a variable.
- class FieldType¶
Bases:
ABC- abstract clear()¶
Clear the value of the field, unless it is a constant.
- Return type:
None
- classmethod from_string(s)¶
Create a FieldType instance from a string.
The type is inferred from the string, so it can be a Field, Format, or other FieldType.
- Parameters:
s (
str) – The string to parse.- Return type:
- Returns:
The FieldType instance.
- abstract has_dynamic_size()¶
Returns whether this FieldType can stretch to fit the available data.
- Return type:
bool
- info()¶
Return a descriptive string with information about the FieldType.
Note that the output is designed to be helpful to users and is not considered part of the API. You should not use the output programmatically as it may change even between point versions.
- Return type:
str
- abstract is_const()¶
Returns whether this FieldType is a constant, so doesn’t need any values to be packed.
- Return type:
bool
- pack(values, /, **kwargs)¶
Pack with values for each non-const field.
- parse(b=None, /, **kwargs)¶
Parse the field type from the supplied bits.
The parsing is done lazily, so any values might not be calculated at this point, instead waiting until they are explicitly asked for.
- Parameters:
b (BitsType | None) – The bits to parse.
- Return type:
int
- Returns:
The number of bits used.
- pp(stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, indent=None, depth=None)¶
Pretty-print the fieldtype to a stream (or stdout by default).
- Parameters:
stream (
TextIO) – The stream to write to.indent (
int|None) – The number of spaces for each level of indentation. Defaults to Options().indent_size which defaults to 4.depth (
int|None) – The maximum depth to print, or None for no limit.
- Return type:
None
- abstract to_bits()¶
Return the bits that represent the field.
- Return type:
- Returns:
The bits that represent the field.
- to_bytes()¶
Return the bytes that represent the field. Pads with up to 7 zero bits if necessary.
- Return type:
bytes- Returns:
The bytes that represent the field.
- unpack(b=None, **kwargs)¶
Unpack the field type from bits.
- Parameters:
b (BitsType | None) – The bits to unpack.
- Return type:
Any | list[Any]
- Returns:
The unpacked value.
- property bit_length: int¶
- property name: str | None¶
- property value: Any¶
- class Field(s: str)¶
Bases:
FieldType- clear()¶
Clear the value of the field, unless it is a constant.
- Return type:
None
- classmethod from_bits(b, /, name='', const=False)¶
Create a Field instance of dtype Bits.
- Parameters:
b (
Union[Bits,str,Iterable,bytearray,bytes,memoryview]) – The Bits or something that can be converted to Bits (such as a formatted string).name (
str) – The name of the field, optional.const (
bool) – Whether the field is constant, defaults to False.
- Return type:
Self- Returns:
The Field instance.
- classmethod from_bytes(b, /, name='', const=False)¶
Create a Field instance of dtype bytes.
- Parameters:
b (
bytes|bytearray) – The bytes or bytearray value to use.name (
str) – The name of the field, optional.const (
bool) – Whether the field is constant, defaults to False.
- Return type:
- Returns:
The Field instance.
- classmethod from_params(dtype, name='', value=None, const=False)¶
Create a Field instance from parameters.
- Parameters:
dtype (
Dtype|str) – The data type of the field.name (
str) – The name of the field, optional.value (
Any) – The value of the field, optional.const (
bool) – Whether the field is constant, optional.
- Return type:
Self- Returns:
The Field instance.
- classmethod from_string(s, /)¶
Create a FieldType instance from a string.
The type is inferred from the string, so it can be a Field, Format, or other FieldType.
- Parameters:
s (
str) – The string to parse.- Return type:
Self- Returns:
The FieldType instance.
- has_dynamic_size()¶
Returns whether this FieldType can stretch to fit the available data.
- Return type:
bool
- info()¶
Return a descriptive string with information about the FieldType.
Note that the output is designed to be helpful to users and is not considered part of the API. You should not use the output programmatically as it may change even between point versions.
- Return type:
str
- is_const()¶
Returns whether this FieldType is a constant, so doesn’t need any values to be packed.
- Return type:
bool
- pack(values, /, **kwargs)¶
Pack with values for each non-const field.
- parse(b=None, /, **kwargs)¶
Parse the field type from the supplied bits.
The parsing is done lazily, so any values might not be calculated at this point, instead waiting until they are explicitly asked for.
- Parameters:
b (BitsType | None) – The bits to parse.
- Return type:
int
- Returns:
The number of bits used.
- pp(stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, indent=None, depth=None)¶
Pretty-print the fieldtype to a stream (or stdout by default).
- Parameters:
stream (
TextIO) – The stream to write to.indent (
int|None) – The number of spaces for each level of indentation. Defaults to Options().indent_size which defaults to 4.depth (
int|None) – The maximum depth to print, or None for no limit.
- Return type:
None
- to_bits()¶
Return the bits that represent the field.
- Return type:
- Returns:
The bits that represent the field.
- to_bytes()¶
Return the bytes that represent the field. Pads with up to 7 zero bits if necessary.
- Return type:
bytes- Returns:
The bytes that represent the field.
- unpack(b=None, **kwargs)¶
Unpack the field type from bits.
- Parameters:
b (BitsType | None) – The bits to unpack.
- Return type:
Any | list[Any]
- Returns:
The unpacked value.
- property bit_length: int¶
- property name: str | None¶
- property value: Any¶
- class Format(s: str | None = None)¶
Bases:
FieldTypeA sequence of
FieldTypeobjects, used to group fields together.- append(value)¶
Append a field to the format.
- Parameters:
value (
Any) – The field to append.- Return type:
None
- clear()¶
Clear the value of the field, unless it is a constant.
- Return type:
None
- extend(values)¶
Extend the format with multiple fields.
- Parameters:
values (
Iterable) – The fields to add.- Return type:
None
- classmethod from_params(fields=None, name='')¶
Create a Format instance.
- Parameters:
fields (
Optional[Sequence[FieldType|str]]) – The field types to include in the format, optional.name (
str) – The name of the format, optional.
- Return type:
Self- Returns:
The Format instance.
- classmethod from_string(s, /)¶
Create a
Formatinstance from a string.The string should be of the form
'(field1, field2, ...)'or'name: (field1, field2, ...)', with commas separating strings that will be used to create otherFieldTypeinstances.- Parameters:
s (
str) – The string to parse.- Return type:
Self- Returns:
The Format instance.
f1 = Format.from_string('(u8, bool=True, val: i7)') f2 = Format.from_string('my_format: (float16,)') f3 = Format.from_string('(u16, another_format: ([u8; 64], [bool; 8]))')
- has_dynamic_size()¶
Returns whether this FieldType can stretch to fit the available data.
- Return type:
bool
- info()¶
Return a descriptive string with information about the FieldType.
Note that the output is designed to be helpful to users and is not considered part of the API. You should not use the output programmatically as it may change even between point versions.
- Return type:
str
- is_const()¶
Returns whether this FieldType is a constant, so doesn’t need any values to be packed.
- Return type:
bool
- pack(values, /, **kwargs)¶
Pack with values for each non-const field.
- parse(b=None, /, **kwargs)¶
Parse the field type from the supplied bits.
The parsing is done lazily, so any values might not be calculated at this point, instead waiting until they are explicitly asked for.
- Parameters:
b (BitsType | None) – The bits to parse.
- Return type:
int
- Returns:
The number of bits used.
- pp(stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, indent=None, depth=None)¶
Pretty-print the fieldtype to a stream (or stdout by default).
- Parameters:
stream (
TextIO) – The stream to write to.indent (
int|None) – The number of spaces for each level of indentation. Defaults to Options().indent_size which defaults to 4.depth (
int|None) – The maximum depth to print, or None for no limit.
- Return type:
None
- to_bits()¶
Return the bits that represent the field.
- Return type:
- Returns:
The bits that represent the field.
- to_bytes()¶
Return the bytes that represent the field. Pads with up to 7 zero bits if necessary.
- Return type:
bytes- Returns:
The bytes that represent the field.
- unpack(b=None, **kwargs)¶
Unpack the field type from bits.
- Parameters:
b (BitsType | None) – The bits to unpack.
- Return type:
Any | list[Any]
- Returns:
The unpacked value.
- property bit_length: int¶
- property name: str | None¶
- property value: Any¶
- vars: dict[str, Any]¶
- class If(s: str)¶
Bases:
FieldType- clear()¶
Clear the value of the field, unless it is a constant.
- Return type:
None
- classmethod from_params(condition, then_, else_=None, /)¶
The
else_parameter is optional, and defaults to aPassfield if not provided.Note that only a single
FieldTypecan be provided for each of thethen_andelse_clauses. If you need to provide multiple fields, use aFormat.- Return type:
- classmethod from_string(s, /)¶
Create an If field from a string.
The string should be in the format:
- Return type:
- if {expression}:
then_field
- else:
else_field
The else clause is optional, and defaults to a
Passfield if not provided.
- has_dynamic_size()¶
Returns whether this FieldType can stretch to fit the available data.
- Return type:
bool
- is_const()¶
Returns whether this FieldType is a constant, so doesn’t need any values to be packed.
- Return type:
bool
- to_bits()¶
Return the bits that represent the field.
- Return type:
- Returns:
The bits that represent the field.
-
condition:
Expression¶
-
condition_value:
bool|None¶
- class Repeat(s: str)¶
Bases:
FieldType- clear()¶
Clear the value of the field, unless it is a constant.
- Return type:
None
- classmethod from_params(count, fieldtype, value=None)¶
Create a Repeat instance.
- Parameters:
count (
int|str|Expression) – An Expression or int giving the number of repetitions to do.fieldtype (
FieldType|str) – The FieldType to repeat.value (
Optional[Iterable[Any]]) – The list of values for the FieldType, one for each repetition.
- Return type:
- Returns:
The Repeat instance.
- classmethod from_string(s)¶
Create a
Repeatinstance from a string.The string should be of the form
'repeat {expression}: fieldtype'. The fieldtype can be aFormatto group multiple fields together.- Parameters:
s (
str) – The string to parse.- Return type:
- Returns:
The Repeat instance.
r1 = Repeat.from_string('repeat {5}: u8') r2 = Repeat.from_string('repeat {3}: f32 = [0.0, 13.5, -5.05]') r3 = Repeat.from_string('repeat {x + 1}: (bool, f64)')
- has_dynamic_size()¶
Returns whether this FieldType can stretch to fit the available data.
- Return type:
bool
- is_const()¶
Returns whether this FieldType is a constant, so doesn’t need any values to be packed.
- Return type:
bool
- to_bits()¶
Return the bits that represent the field.
- Return type:
- Returns:
The bits that represent the field.
- property count: Expression¶
The count of the Repeat field.
- Returns:
The count of the Repeat field.
- class Let(s: str)¶
Bases:
FieldTypeA
FieldTypethat assigns expression results to variables.This is only useful within a
Formatwhere other fields can refer to the variable.- clear()¶
Clearing a Let field has no effect.
- Return type:
None
- classmethod from_params(name, expr)¶
Create a Let instance.
- Parameters:
name (
str) – The variable name to assign to.expr (
str|int|Expression) – The Expression to evaluate.
- Return type:
- Returns:
The Let instance.
increment_x = Let.from_params('x', '{x + 1}')
- has_dynamic_size()¶
Returns False for a Let field.
- Return type:
bool
- is_const()¶
Returns True for a Let field.
- Return type:
bool
- class Pass¶
Bases:
FieldTypeAn empty placeholder
FieldType.A Pass field has zero bit_length and no value. It can be used in conditionals when no action is required. When used elsewhere in a
Formatit may be removed as an optimisation. All Pass fields are identical, so they are implemented as a singleton.It is usually created implicitly, for example in an
Iffield when noelsefield is provided.cond = If.from_params('{ x > 0 }', Pass(), 'bool')
- clear()¶
Clearing a Pass field has no effect.
- Return type:
None
- classmethod from_params(*args, **kwargs)¶
Returns the singleton Pass instance. The args and kwargs are ignored.
- Return type:
- has_dynamic_size()¶
Returns False for a Pass field.
- Return type:
bool
- is_const()¶
Returns True for a Pass field.
- Return type:
bool