binobj.fields.containers module¶
Fields used for forming more complex structures with other fields.
- class Array(*_args: Any, **kwargs: Any)¶
Bases:
Field[List[Optional[T]]]An array of other serializable objects.
- Parameters:
component (Field) – The component this array is comprised of. Must be an instance.
count –
Optional. Some way of indicating the number of elements in this array. The value for this argument can be one of the following:
An integer. The array always contains this many elements.
A
Fieldinstance that must 1) be an integer; 2) occur before this array in the same struct.A string naming a field fitting the above criteria. You’ll need this if your size field’s name is a Python keyword.
halt_check (callable) – A function taking five arguments. See
should_halt()for the default implementation. Subclasses can override this function if desired to avoid having to pass in a custom function every time.
Changed in version 0.3.0:
countcan now be aFieldor string.Changed in version 0.6.1:
to_stream()andto_bytes()throw anArraySizeErrorifcountis set and the iterable passed in is too long. Due to a bug it used to be ignored when dumping.Changed in version 0.7.0:
sizeis set ifcomponent.sizeis defined andcountis an integer constant.- const: T | _Undefined¶
The fixed value of a field, if applicable.
This is mostly useful for fields that act as magic numbers or reserved fields in a struct that should be set to nulls.
- discard: bool¶
If True, indicates that a field should be discarded when read.
This is best used for filler fields that are of no use to the application but are nonetheless important to ensure the proper layout of the struct.
- get_final_element_count(field_values: Mapping[str, Any]) int | None¶
Calculate the number of elements in the array based on other fields’ values.
- Parameters:
field_values (dict) – A dict mapping field names to their deserialized values. It doesn’t need to have every value in the struct; if
countreferences a field, it only requires that field to be present here.- Returns:
The expected number of elements in this array, or
Noneif the array doesn’t have a fixed size.- Return type:
New in version 0.6.1.
Changed in version 0.8.0: Throws a ConfigurationError if this field’s
countis a Field but doesn’t have an assigned name.
- name: str¶
The name of the field.
Technical Note: This attribute can only be
Noneif the field was created without passing a value fornameto the constructor and the field has never been bound to a struct. Since this is highly unlikely in normal usage, this attribute is declared asstrrather thanOptional[str].
- static should_halt(seq: Array[T], stream: BinaryIO, values: List[T | None], context: Any, loaded_fields: Mapping[str, Any]) bool¶
Determine if the deserializer should stop reading from the input.
This function should return
Trueto indicate loading for this field should stop, orFalseto continue adding elements.The default implementation does the following:
If
countis an integer, it comparescountagainst the length ofvalues. Iflen(values)is equal to or more thancountit’ll returnTrue(halt),Falseotherwise.If
countis aField, that field should already have been loaded and inloaded_fields. The expected array size is taken from there, and compared as above.If
countis a string, it’s the name of a field already loaded and inloaded_fields. The expected array size is taken from there, and compared as above.Otherwise, the function assumes the array ends at EOF and only returns
Trueif there’s no more data in the stream.
Subclasses’ implementations must handle all four cases.
- Parameters:
seq (Array) – The sequence being checked.
stream (BinaryIO) – The data stream to read from. Except in rare circumstances, this is the same stream that was passed to
from_stream(). The stream pointer should be returned to its original position when the function exits.values (list) – A list of the objects that have been deserialized so far. In general this function should not modify the list. A possible exception to this rule is to remove a sentinel value from the end of the list.
context – The
contextobject passed tofrom_stream().loaded_fields (dict) – The fields in the struct that have been loaded so far.
- Returns:
Trueif the deserializer should stop reading,Falseotherwise.- Return type:
Changed in version 0.8.0: The default implementation now throws
UndefinedSizeErrorif the length of the array couldn’t be determined. Previously this would crash with aTypeError.
- class Nested(*_args: Any, **kwargs: Any)¶
Bases:
Field[TStruct]Used to nest one struct inside of another.
- Parameters:
struct_class (Type[Struct]) – The struct class to wrap as a field. Not an instance!
class Address(Struct): ... class Person(Struct): name = fields.StringZ() address = fields.Nested(Address)
Changed in version 0.7.0:
sizeis set if the struct passed in is of fixed size. Prior to 0.7.0,Person.get_size()would be None even ifAddress.get_size()returned a value. Now the sizes are the same.- const: T | _Undefined¶
The fixed value of a field, if applicable.
This is mostly useful for fields that act as magic numbers or reserved fields in a struct that should be set to nulls.
- discard: bool¶
If True, indicates that a field should be discarded when read.
This is best used for filler fields that are of no use to the application but are nonetheless important to ensure the proper layout of the struct.
- name: str¶
The name of the field.
Technical Note: This attribute can only be
Noneif the field was created without passing a value fornameto the constructor and the field has never been bound to a struct. Since this is highly unlikely in normal usage, this attribute is declared asstrrather thanOptional[str].
- class Union(*_args: Any, **kwargs: Any)¶
-
A field that can be one of several different types of structs or fields.
- Parameters:
choices – One or more
Structclasses orFieldinstances that can be used for loading and dumping.load_decider (callable) –
A function that decides which
Structclass orFieldinstance to use for loading the input. It must take four arguments:stream: The stream being loaded from.classes: A list of classes that can be used for loading.context: The context object to pass directly to the loader selected fromclasses.loaded_fields: A dictionary of the fields that have already been loaded. This is guaranteed to not beNone.
dump_decider (callable) –
A function that decides which
Structclass orFieldinstance to use for dumping the given data. It must take four arguments:data: The data to dump. This can be any type.classes: A list of classes that can be used for dumping.context: The context object to pass directly to the dumper selected fromclasses.all_fields: A dictionary of the fields about to be dumped. This is guaranteed to not beNone.
Usage with Structs:
def load_decider(stream, classes, context, loaded_fields): data_type_id = loaded_fields['data_type'] return classes[data_type_id] def dump_decider(data, classes, context, all_fields): data_type_id = all_fields['data_type'] return classes[data_type_id] class MyStruct(Struct): data_type = UInt8() data = Union(UserInfo, FileInfo, SystemInfo, load_decider=load_decider, dump_decider=dump_decider)
Usage with Fields:
class FieldsUnionContainer(binobj.Struct): data_type = fields.UInt8() item = fields.Union(fields.StringZ(), fields.UInt16(endian='little'), load_decider=fields_load_decider, dump_decider=fields_dump_decider)
New in version 0.3.0.
- const: T | _Undefined¶
The fixed value of a field, if applicable.
This is mostly useful for fields that act as magic numbers or reserved fields in a struct that should be set to nulls.
- discard: bool¶
If True, indicates that a field should be discarded when read.
This is best used for filler fields that are of no use to the application but are nonetheless important to ensure the proper layout of the struct.
- name: str¶
The name of the field.
Technical Note: This attribute can only be
Noneif the field was created without passing a value fornameto the constructor and the field has never been bound to a struct. Since this is highly unlikely in normal usage, this attribute is declared asstrrather thanOptional[str].