signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def extend(self, iterable):
<EOL>if is_sequence(iterable):<EOL><INDENT>self._data.extend(iterable)<EOL><DEDENT>elif is_structure(iterable):<EOL><INDENT>members = [item for item in iterable.values()]<EOL>self._data.extend(members)<EOL><DEDENT>elif is_field(iterable):<EOL><INDENT>self._data.extend([iterable])<EOL><DEDENT>elif isinstance(iterable, (set, tuple, list)):<EOL><INDENT>self._data.extend(Sequence(iterable))<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, iterable, member=len(self))<EOL><DEDENT>
Extends the `Sequence` by appending items from the *iterable*. :param iterable: any *iterable* that contains items of :class:`Structure`, :class:`Sequence`, :class:`Array` or :class:`Field` instances. If the *iterable* is one of these instances itself then the *iterable* itself is appended to the `Sequence`.
f12145:c3:m16
@nested_option()<EOL><INDENT>def read_from(self, provider, **options):<DEDENT>
for item in iter(self):<EOL><INDENT>if is_mixin(item):<EOL><INDENT>item.read_from(provider, **options)<EOL><DEDENT><DEDENT>
All :class:`Pointer` fields in the `Sequence` read the necessary number of bytes from the data :class:`Provider` for their referenced :attr:`~Pointer.data` object. Null pointer are ignored. :param Provider provider: data :class:`Provider`. :keyword bool nested: if ``True`` all :class:`Pointer` fields in the :attr:`~Pointer.data` objects of all :class:`Pointer` fields in the `Sequence` reads their referenced :attr:`~Pointer.data` object as well (chained method call). Each :class:`Pointer` field stores the bytes for its referenced :attr:`~Pointer.data` object in its :attr:`~Pointer.bytestream`.
f12145:c3:m17
@byte_order_option()<EOL><INDENT>@nested_option()<EOL>def deserialize(self, buffer=bytes(), index=Index(), **options):<DEDENT>
for item in iter(self):<EOL><INDENT>index = item.deserialize(buffer, index, **options)<EOL><DEDENT>return index<EOL>
De-serializes the `Sequence` from the byte *buffer* starting at the begin of the *buffer* or with the given *index* by mapping the bytes to the :attr:`~Field.value` for each :class:`Field` in the `Sequence` in accordance with the decoding *byte order* for the de-serialization and the decoding :attr:`byte_order` of each :class:`Field` in the `Sequence`. A specific decoding :attr:`~Field.byte_order` of a :class:`Field` overrules the decoding *byte order* for the de-serialization. Returns the :class:`Index` of the *buffer* after the last de-serialized :class:`Field` in the `Sequence`. Optional the de-serialization of the referenced :attr:`~Pointer.data` objects of all :class:`Pointer` fields in the `Sequence` can be enabled. :param bytes buffer: byte stream. :param Index index: current read :class:`Index` within the *buffer*. :keyword byte_order: decoding byte order for the de-serialization. :type byte_order: :class:`Byteorder`, :class:`str` :keyword bool nested: if ``True`` all :class:`Pointer` fields of a `Sequence` de-serialize their referenced :attr:`~Pointer.data` object as well (chained method call). Each :class:`Pointer` field uses for the de-serialization of its referenced :attr:`~Pointer.data` object its own :attr:`~Pointer.bytestream`.
f12145:c3:m18
@byte_order_option()<EOL><INDENT>@nested_option()<EOL>def serialize(self, buffer=bytearray(), index=Index(), **options):<DEDENT>
for item in iter(self):<EOL><INDENT>index = item.serialize(buffer, index, **options)<EOL><DEDENT>return index<EOL>
Serializes the `Sequence` to the byte *buffer* starting at begin of the *buffer* or with the given *index* by mapping the :attr:`~Field.value` for each :class:`Field` in the `Sequence` to the byte *buffer* in accordance with the encoding *byte order* for the serialization and the encoding :attr:`byte_order` of each :class:`Field` in the `Sequence`. A specific encoding :attr:`~Field.byte_order` of a :class:`Field` overrules the encoding *byte order* for the serialization. Returns the :class:`Index` of the *buffer* after the last serialized :class:`Field` in the `Sequence`. Optional the serialization of the referenced :attr:`~Pointer.data` objects of all :class:`Pointer` fields in the `Sequence` can be enabled. :param bytearray buffer: byte stream. :param Index index: current write :class:`Index` within the *buffer*. :keyword byte_order: encoding byte order for the serialization. :type byte_order: :class:`Byteorder`, :class:`str` :keyword bool nested: if ``True`` all :class:`Pointer` fields of a `Sequence` serialize their referenced :attr:`~Pointer.data` object as well (chained method call). Each :class:`Pointer` field uses for the serialization of its referenced :attr:`~Pointer.data` object its own :attr:`~Pointer.bytestream`.
f12145:c3:m19
@nested_option()<EOL><INDENT>def index_fields(self, index=Index(), **options):<DEDENT>
for name, item in enumerate(self):<EOL><INDENT>if is_container(item):<EOL><INDENT>index = item.index_fields(index, **options)<EOL><DEDENT>elif is_pointer(item) and get_nested(options):<EOL><INDENT>index = item.index_field(index)<EOL>item.index_data()<EOL><DEDENT>elif is_field(item):<EOL><INDENT>index = item.index_field(index)<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item, name, index)<EOL><DEDENT><DEDENT>return index<EOL>
Indexes all fields in the `Sequence` starting with the given *index* and returns the :class:`Index` after the last :class:`Field` in the `Sequence`. :param Index index: start :class:`Index` for the first :class:`Field` in the `Sequence`. :keyword bool nested: if ``True`` all :class:`Pointer` fields in the `Sequence` indexes their referenced :attr:`~Pointer.data` object fields as well (chained method call).
f12145:c3:m20
def container_size(self):
length = <NUM_LIT:0><EOL>for name, item in enumerate(self):<EOL><INDENT>if is_container(item):<EOL><INDENT>byte_length, bit_length = item.container_size()<EOL>length += bit_length + byte_length * <NUM_LIT:8><EOL><DEDENT>elif is_field(item):<EOL><INDENT>length += item.bit_size<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item, name)<EOL><DEDENT><DEDENT>return divmod(length, <NUM_LIT:8>)<EOL>
Returns the accumulated bit size of all fields in the `Sequence` as a tuple in the form of ``(number of bytes, remaining number of bits)``.
f12145:c3:m21
def first_field(self):
for name, item in enumerate(self):<EOL><INDENT>if is_container(item):<EOL><INDENT>field = item.first_field()<EOL>if field is not None:<EOL><INDENT>return field<EOL><DEDENT><DEDENT>elif is_field(item):<EOL><INDENT>return item<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item, name)<EOL><DEDENT><DEDENT>return None<EOL>
Returns the first :class:`Field` in the `Sequence` or ``None`` for an empty `Sequence`.
f12145:c3:m22
def initialize_fields(self, content):
for name, pair in enumerate(zip(self, content)):<EOL><INDENT>item, value = pair<EOL>if is_mixin(item):<EOL><INDENT>item.initialize_fields(value)<EOL><DEDENT>elif is_field(item):<EOL><INDENT>item.value = value<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item, name)<EOL><DEDENT><DEDENT>
Initializes the :class:`Field` items in the `Sequence` with the *values* in the *content* list. :param list content: a list contains the :class:`Field` values for each item in the `Sequence`.
f12145:c3:m23
@nested_option()<EOL><INDENT>def view_fields(self, *attributes, **options):<DEDENT>
items = list()<EOL>for index, item in enumerate(self):<EOL><INDENT>if is_container(item):<EOL><INDENT>items.append(item.view_fields(*attributes, **options))<EOL><DEDENT>elif is_pointer(item) and get_nested(options):<EOL><INDENT>items.append(item.view_fields(*attributes, **options))<EOL><DEDENT>elif is_field(item):<EOL><INDENT>if attributes:<EOL><INDENT>field_getter = attrgetter(*attributes)<EOL><DEDENT>else:<EOL><INDENT>field_getter = attrgetter('<STR_LIT:value>')<EOL><DEDENT>if len(attributes) > <NUM_LIT:1>:<EOL><INDENT>fieldnames = options.get('<STR_LIT>', attributes)<EOL>items.append(dict(zip(fieldnames, field_getter(item))))<EOL><DEDENT>else:<EOL><INDENT>items.append(field_getter(item))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item, index)<EOL><DEDENT><DEDENT>return items<EOL>
Returns a list with the selected field *attribute* or a list with the dictionaries of the selected field *attributes* for each :class:`Field` *nested* in the `Sequence`. The *attributes* of each :class:`Field` for containers *nested* in the `Sequence` are viewed as well (chained method call). :param str attributes: selected :class:`Field` attributes. Fallback is the field :attr:`~Field.value`. :keyword tuple fieldnames: sequence of dictionary keys for the selected field *attributes*. Defaults to ``(*attributes)``. :keyword bool nested: if ``True`` all :class:`Pointer` fields nested in the `Sequence` views their referenced :attr:`~Pointer.data` object field attributes as well (chained method call).
f12145:c3:m24
@nested_option()<EOL><INDENT>def field_items(self, path=str(), **options):<DEDENT>
items = list()<EOL>for index, item in enumerate(self):<EOL><INDENT>if path:<EOL><INDENT>item_path = "<STR_LIT>".format(path, str(index))<EOL><DEDENT>else:<EOL><INDENT>item_path = "<STR_LIT>".format(str(index))<EOL><DEDENT>if is_container(item):<EOL><INDENT>for field_item in item.field_items(item_path, **options):<EOL><INDENT>items.append(field_item)<EOL><DEDENT><DEDENT>elif is_pointer(item) and get_nested(options):<EOL><INDENT>for field_item in item.field_items(item_path, **options):<EOL><INDENT>items.append(field_item)<EOL><DEDENT><DEDENT>elif is_field(item):<EOL><INDENT>items.append((item_path, item))<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item, item_path)<EOL><DEDENT><DEDENT>return items<EOL>
Returns a **flatten** list of ``('field path', field item)`` tuples for each :class:`Field` *nested* in the `Sequence`. :param str path: field path of the `Sequence`. :keyword bool nested: if ``True`` all :class:`Pointer` fields in the :attr:`~Pointer.data` objects of all :class:`Pointer` fields in the `Sequence` list their referenced :attr:`~Pointer.data` object field items as well (chained method call).
f12145:c3:m25
@nested_option(True)<EOL><INDENT>def describe(self, name=str(), **options):<DEDENT>
members = list()<EOL>metadata = OrderedDict()<EOL>metadata['<STR_LIT:class>'] = self.__class__.__name__<EOL>metadata['<STR_LIT:name>'] = name if name else self.__class__.__name__<EOL>metadata['<STR_LIT:size>'] = len(self)<EOL>metadata['<STR_LIT:type>'] = self.item_type.name<EOL>metadata['<STR_LIT>'] = members<EOL>for member_name, item in enumerate(self):<EOL><INDENT>if is_container(item):<EOL><INDENT>members.append(item.describe("<STR_LIT>".<EOL>format(metadata['<STR_LIT:name>'], member_name),<EOL>**options))<EOL><DEDENT>elif is_pointer(item) and get_nested(options):<EOL><INDENT>members.append(item.describe("<STR_LIT>".<EOL>format(metadata['<STR_LIT:name>'], member_name),<EOL>**options))<EOL><DEDENT>elif is_field(item):<EOL><INDENT>members.append(item.describe("<STR_LIT>".<EOL>format(metadata['<STR_LIT:name>'], member_name),<EOL>nested=False))<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item, member_name)<EOL><DEDENT><DEDENT>return metadata<EOL>
Returns the **metadata** of the `Sequence` as an :class:`ordered dictionary <collections.OrderedDict>`. .. code-block:: python metadata = { 'class': self.__class__.__name__, 'name': name if name else self.__class__.__name__, 'size': len(self), 'type': Sequence.item_type.name 'member': [ item.describe('name[idx]') for idx, item in enumerate(self) ] } :param str name: optional name for the `Sequence`. Fallback is the class name. :keyword bool nested: if ``True`` all :class:`Pointer` fields in the `Sequence` lists their referenced :attr:`~Pointer.data` object fields as well (chained method call). Default is ``True``.
f12145:c3:m26
def append(self):
super().append(self.__create__())<EOL>
Appends a new *array element* to the `Array`.
f12145:c4:m2
def insert(self, index):
super().insert(index, self.__create__())<EOL>
Inserts a new *array element* before the *index* of the `Array`. :param int index: `Array` index.
f12145:c4:m3
def resize(self, capacity):
count = max(int(capacity), <NUM_LIT:0>) - len(self)<EOL>if count == <NUM_LIT:0>:<EOL><INDENT>pass<EOL><DEDENT>elif -count == len(self):<EOL><INDENT>self.clear()<EOL><DEDENT>elif count > <NUM_LIT:0>:<EOL><INDENT>for i in range(count):<EOL><INDENT>self.append()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for i in range(abs(count)):<EOL><INDENT>self.pop()<EOL><DEDENT><DEDENT>
Re-sizes the `Array` by appending new *array elements* or removing *array elements* from the end. :param int capacity: new capacity of the `Array` in number of *array elements*.
f12145:c4:m4
def initialize_fields(self, content):
if isinstance(content, (list, tuple)):<EOL><INDENT>capacity = len(content)<EOL>for i in range(<NUM_LIT:0>, len(self), capacity):<EOL><INDENT>for name, pair in enumerate(zip(self[i:i + capacity],<EOL>content),<EOL>start=i):<EOL><INDENT>item, value = pair<EOL>if is_mixin(item):<EOL><INDENT>item.initialize_fields(value)<EOL><DEDENT>elif is_field(item):<EOL><INDENT>item.value = value<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item, name)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for name, item in enumerate(self):<EOL><INDENT>if is_mixin(item):<EOL><INDENT>item.initialize_fields(content)<EOL><DEDENT>elif is_field(item):<EOL><INDENT>item.value = content<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item, name)<EOL><DEDENT><DEDENT><DEDENT>
Initializes the :class:`Field` elements in the `Array` with the *values* in the *content* list. If the *content* list is shorter than the `Array` then the *content* list is used as a rotating fill pattern for the :class:`Field` elements in the `Array`. :param list content: a list contains the :class:`Field` values for each element in the `Array` or one :class:`Field` value for all elements in the `Array`.
f12145:c4:m5
@property<EOL><INDENT>def alignment(self):<DEDENT>
return Alignment(self._align_to_byte_size, self._align_to_bit_offset)<EOL>
Returns the :class:`Alignment` of the `Field` (read-only).
f12145:c5:m3
@property<EOL><INDENT>def bit_size(self):<DEDENT>
return self._bit_size<EOL>
Returns the size of the `Field` in bits (read-only).
f12145:c5:m4
@property<EOL><INDENT>def byte_order(self):<DEDENT>
return self._byte_order<EOL>
:class:`Byteorder` used to decode and encode the :attr:`value` of the `Field`.
f12145:c5:m5
@property<EOL><INDENT>def index(self):<DEDENT>
return self._index<EOL>
:class:`Index` of the `Field`.
f12145:c5:m7
@property<EOL><INDENT>def name(self):<DEDENT>
return self.item_type.name.capitalize() + str(self.bit_size)<EOL>
Returns the type name of the `Field` (read-only).
f12145:c5:m9
@property<EOL><INDENT>def value(self):<DEDENT>
return self._value<EOL>
Field value.
f12145:c5:m10
@staticmethod<EOL><INDENT>def is_bit():<DEDENT>
return False<EOL>
Returns ``False``.
f12145:c5:m12
@staticmethod<EOL><INDENT>def is_bool():<DEDENT>
return False<EOL>
Returns ``False``.
f12145:c5:m13
@staticmethod<EOL><INDENT>def is_decimal():<DEDENT>
return False<EOL>
Returns ``False``.
f12145:c5:m14
@staticmethod<EOL><INDENT>def is_float():<DEDENT>
return False<EOL>
Returns ``False``.
f12145:c5:m15
@staticmethod<EOL><INDENT>def is_pointer():<DEDENT>
return False<EOL>
Returns ``False``.
f12145:c5:m16
@staticmethod<EOL><INDENT>def is_stream():<DEDENT>
return False<EOL>
Returns ``False``.
f12145:c5:m17
@staticmethod<EOL><INDENT>def is_string():<DEDENT>
return False<EOL>
Returns ``False``.
f12145:c5:m18
@abc.abstractmethod<EOL><INDENT>@byte_order_option()<EOL>def unpack(self, buffer=bytes(), index=Index(), **options):<DEDENT>
<EOL>return None<EOL>
Unpacks the field :attr:`value` from the *buffer* at the given *index* in accordance with the decoding *byte order* for the de-serialization and the :attr:`byte_order` and :attr:`alignment` of the `Field`. The specific decoding :attr:`byte_order` of the `Field` overrules the decoding *byte order* for the de-serialization. Returns the deserialized field :attr:`value`. :param bytes buffer: byte stream. :param Index index: current read :class:`Index` within the *buffer*. :keyword byte_order: decoding byte order for the de-serialization. :type byte_order: :class:`Byteorder`, :class:`str` .. note:: This abstract method must be implemented by a derived class.
f12145:c5:m19
@abc.abstractmethod<EOL><INDENT>@byte_order_option()<EOL>def pack(self, buffer=bytearray(), **options):<DEDENT>
<EOL>return bytes()<EOL>
Packs the field :attr:`value` to the *buffer* at the given *index* in accordance with the encoding *byte order* for the serialization and the :attr:`byte_order` and :attr:`alignment` of the `Field`. The specific encoding :attr:`byte_order` of the `Field` overrules the encoding *byte order* for the serialization. Returns the :class:`bytes` for the serialized field :attr:`value`. :param bytearray buffer: byte stream. :keyword byte_order: encoding byte order for the serialization. :type byte_order: :class:`Byteorder`, :class:`str` .. note:: This abstract method must be implemented by a derived class.
f12145:c5:m20
@byte_order_option()<EOL><INDENT>@nested_option()<EOL>def deserialize(self, buffer=bytes(), index=Index(), **options):<DEDENT>
self.index = index<EOL>self._value = self.unpack(buffer, index, **options)<EOL>return self.index_field(index)<EOL>
De-serializes the `Field` from the byte *buffer* starting at the begin of the *buffer* or with the given *index* by unpacking the bytes to the :attr:`value` of the `Field` in accordance with the decoding *byte order* for the de-serialization and the decoding :attr:`byte_order` of the `Field`. The specific decoding :attr:`byte_order` of the `Field` overrules the decoding *byte order* for the de-serialization. Returns the :class:`Index` of the *buffer* after the `Field`. Optional the de-serialization of the referenced :attr:`~Pointer.data` object of a :class:`Pointer` field can be enabled. :param bytes buffer: byte stream. :param Index index: current read :class:`Index` within the *buffer*. :keyword byte_order: decoding byte order for the de-serialization. :type byte_order: :class:`Byteorder`, :class:`str` :keyword bool nested: if ``True`` a :class:`Pointer` field de-serialize its referenced :attr:`~Pointer.data` object as well (chained method call). Each :class:`Pointer` field uses for the de-serialization of its referenced :attr:`~Pointer.data` object its own :attr:`~Pointer.bytestream`.
f12145:c5:m21
@byte_order_option()<EOL><INDENT>@nested_option()<EOL>def serialize(self, buffer=bytearray(), index=Index(), **options):<DEDENT>
self.index = index<EOL>buffer += self.pack(buffer, **options)<EOL>return self.index_field(index)<EOL>
Serializes the `Field` to the byte *buffer* starting at the begin of the *buffer* or with the given *index* by packing the :attr:`value` of the `Field` to the byte *buffer* in accordance with the encoding *byte order* for the serialization and the encoding :attr:`byte_order` of the `Field`. The specific encoding :attr:`byte_order` of the `Field` overrules the encoding *byte order* for the serialization. Returns the :class:`Index` of the *buffer* after the `Field`. Optional the serialization of the referenced :attr:`~Pointer.data` object of a :class:`Pointer` field can be enabled. :param bytearray buffer: byte stream. :param Index index: current write :class:`Index` of the *buffer*. :keyword byte_order: encoding byte order for the serialization. :type byte_order: :class:`Byteorder`, :class:`str` :keyword bool nested: if ``True`` a :class:`Pointer` field serializes its referenced :attr:`~Pointer.data` object as well (chained method call). Each :class:`Pointer` field uses for the encoding of its referenced :attr:`~Pointer.data` object its own :attr:`~Pointer.bytestream`.
f12145:c5:m22
def index_field(self, index=Index()):
<EOL>self.index = index<EOL>byte, bit, address, base, update = index<EOL>bit += self.bit_size<EOL>group_size, offset = divmod(bit, <NUM_LIT:8>)<EOL>if self.alignment.byte_size == group_size:<EOL><INDENT>if offset is not <NUM_LIT:0>:<EOL><INDENT>raise FieldGroupSizeError(self, index,<EOL>Alignment(group_size + <NUM_LIT:1>,<EOL>self.alignment.bit_offset))<EOL><DEDENT>else:<EOL><INDENT>byte += self.alignment.byte_size<EOL>bit = <NUM_LIT:0><EOL>address += self.alignment.byte_size<EOL><DEDENT><DEDENT>return Index(byte, bit, address, base, update)<EOL>
Indexes the `Field` with the given *index* und returns the :class:`Index` after the `Field`. :param Index index: start :class:`Index` for the `Field`.
f12145:c5:m23
@nested_option(True)<EOL><INDENT>def describe(self, name=str(), **options):<DEDENT>
metadata = {<EOL>'<STR_LIT:address>': self.index.address,<EOL>'<STR_LIT>': list(self.alignment),<EOL>'<STR_LIT:class>': self.name,<EOL>'<STR_LIT>': self.byte_order.value,<EOL>'<STR_LIT:index>': [self.index.byte, self.index.bit],<EOL>'<STR_LIT:name>': name if name else self.name,<EOL>'<STR_LIT:size>': self.bit_size,<EOL>'<STR_LIT:type>': Field.item_type.name,<EOL>'<STR_LIT:value>': self.value<EOL>}<EOL>return OrderedDict(sorted(metadata.items()))<EOL>
Returns the **metadata** of a `Field` as an :class:`ordered dictionary <collections.OrderedDict>`. .. code-block:: python metadata = { 'address': self.index.address, 'alignment': [self.alignment.byte_size, self.alignment.bit_offset], 'class': self.name, 'index': [self.index.byte, self.index.bit], 'name': name if name else self.name, 'order': self.byte_order.value, 'size': self.bit_size, 'type': Field.item_type.name, 'value': self.value } :param str name: optional name for the `Field`. Fallback is the class name. :keyword bool nested: if ``True`` a :class:`Pointer` field lists its referenced :attr:`~Pointer.data` object fields as well (chained method call). Default is ``True``.
f12145:c5:m24
@property<EOL><INDENT>def name(self):<DEDENT>
size = len(self)<EOL>if size > <NUM_LIT:0>:<EOL><INDENT>return self.item_type.name.capitalize() + str(size)<EOL><DEDENT>else:<EOL><INDENT>return self.item_type.name.capitalize()<EOL><DEDENT>
Returns the type name of the `Stream` field (read-only).
f12145:c6:m6
@property<EOL><INDENT>def value(self):<DEDENT>
return hexlify(self._value).decode('<STR_LIT:ascii>')<EOL>
Field value as a lowercase hexadecimal encoded string.
f12145:c6:m7
def hex(self):
return hexlify(self._value).decode('<STR_LIT:ascii>')<EOL>
Returns a string containing two hexadecimal digits for each byte in the :attr:`value` of the `Stream` field.
f12145:c6:m9
@staticmethod<EOL><INDENT>def is_stream():<DEDENT>
return True<EOL>
Returns ``True``.
f12145:c6:m10
def resize(self, size):
count = max(int(size), <NUM_LIT:0>) - len(self)<EOL>if count == <NUM_LIT:0>:<EOL><INDENT>pass<EOL><DEDENT>elif -count == len(self):<EOL><INDENT>self._value = bytes()<EOL><DEDENT>elif count > <NUM_LIT:0>:<EOL><INDENT>self._value += b'<STR_LIT:\x00>' * count<EOL><DEDENT>else:<EOL><INDENT>self._value = self._value[:count]<EOL><DEDENT>size = len(self)<EOL>self._bit_size = size * <NUM_LIT:8><EOL>self._align_to_byte_size = size<EOL>
Re-sizes the `Stream` field by appending zero bytes or removing bytes from the end. :param int size: `Stream` size in number of bytes.
f12145:c6:m14
@property<EOL><INDENT>def value(self):<DEDENT>
length = self._value.find(b'<STR_LIT:\x00>')<EOL>if length >= <NUM_LIT:0>:<EOL><INDENT>return self._value[:length].decode('<STR_LIT:ascii>')<EOL><DEDENT>else:<EOL><INDENT>return self._value.decode('<STR_LIT:ascii>')<EOL><DEDENT>
Field value as an ascii encoded string.
f12145:c7:m0
@staticmethod<EOL><INDENT>def is_string():<DEDENT>
return True<EOL>
Returns ``True``.
f12145:c7:m2
def is_terminated(self):
return self._value.find(b'<STR_LIT:\x00>') >= <NUM_LIT:0><EOL>
Returns ``True`` if the `String` field is zero-terminated.
f12145:c7:m3
@property<EOL><INDENT>def value(self):<DEDENT>
return float(self._value)<EOL>
Field value as a single precision floating point number.
f12145:c8:m5
@staticmethod<EOL><INDENT>def is_float():<DEDENT>
return True<EOL>
Returns ``True``.
f12145:c8:m7
@staticmethod<EOL><INDENT>def smallest():<DEDENT>
return <NUM_LIT:2> ** -<NUM_LIT><EOL>
Returns the smallest possible field *value* of the `Float` field.
f12145:c8:m10
@staticmethod<EOL><INDENT>def max():<DEDENT>
return (<NUM_LIT:1> - Float.epsilon()) * <NUM_LIT:2> ** <NUM_LIT><EOL>
Returns the maximal possible field *value* of the `Float` field.
f12145:c8:m11
@staticmethod<EOL><INDENT>def min():<DEDENT>
return -(<NUM_LIT:1> - Float.epsilon()) * <NUM_LIT:2> ** <NUM_LIT><EOL>
Returns the minimal possible field *value* of the `Float` field.
f12145:c8:m12
@property<EOL><INDENT>def value(self):<DEDENT>
return int(self._value)<EOL>
Field value as a decimal number.
f12145:c9:m6
@property<EOL><INDENT>def signed(self):<DEDENT>
return self._signed<EOL>
Returns ``True`` if the `Decimal` field is signed.
f12145:c9:m8
@staticmethod<EOL><INDENT>def is_decimal():<DEDENT>
return True<EOL>
Returns ``True``.
f12145:c9:m10
def _set_alignment(self, group_size, bit_offset=<NUM_LIT:0>, auto_align=False):
<EOL>field_offset = int(bit_offset)<EOL>if auto_align:<EOL><INDENT>field_size, bit_offset = divmod(field_offset, <NUM_LIT:8>)<EOL>if bit_offset is not <NUM_LIT:0>:<EOL><INDENT>field_size += <NUM_LIT:1><EOL><DEDENT>field_size = max(field_size, <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>field_size = int(group_size)<EOL><DEDENT>alignment = Alignment(field_size, field_offset)<EOL>if field_size not in range(<NUM_LIT:1>, <NUM_LIT:8>):<EOL><INDENT>raise FieldAlignmentError(self, self.index, alignment)<EOL><DEDENT>if not (<NUM_LIT:0> <= field_offset <= <NUM_LIT>):<EOL><INDENT>raise FieldAlignmentError(self, self.index, alignment)<EOL><DEDENT>if field_offset >= field_size * <NUM_LIT:8>:<EOL><INDENT>raise FieldAlignmentError(self, self.index, alignment)<EOL><DEDENT>self._align_to_byte_size = alignment.byte_size<EOL>self._align_to_bit_offset = alignment.bit_offset<EOL>
Sets the alignment of the ``Decimal`` field. :param int group_size: size of the aligned `Field` group in bytes, can be between ``1`` and ``8``. :param int bit_offset: bit offset of the `Decimal` field within the aligned `Field` group, can be between ``0`` and ``63``. :param bool auto_align: if ``True`` the `Decimal` field aligns itself to the next matching byte size according to the *size* of the `Decimal` field.
f12145:c9:m12
def _set_bit_size(self, size, step=<NUM_LIT:1>, auto_align=False):
<EOL>bit_size = int(size)<EOL>if bit_size % step != <NUM_LIT:0> or not (<NUM_LIT:1> <= bit_size <= <NUM_LIT:64>):<EOL><INDENT>raise FieldSizeError(self, self.index, bit_size)<EOL><DEDENT>group_size, offset = divmod(bit_size, <NUM_LIT:8>)<EOL>if auto_align:<EOL><INDENT>if offset is not <NUM_LIT:0>:<EOL><INDENT>self._align_to_byte_size = group_size + <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>self._align_to_byte_size = group_size<EOL><DEDENT><DEDENT>elif group_size > self.alignment.byte_size:<EOL><INDENT>raise FieldAlignmentError(self, self.index,<EOL>Alignment(group_size,<EOL>self.alignment.bit_offset))<EOL><DEDENT>self._bit_size = bit_size<EOL>
Sets the *size* of the `Decimal` field. :param int size: is the *size* of the `Decimal` field in bits, can be between ``1`` and ``64``. :param int step: is the minimal required step *size* for the `Decimal` field in bits. :param bool auto_align: if ``True`` the `Decimal` field aligns itself to the next matching byte size according to the *size* of the `Decimal` field.
f12145:c9:m13
def max(self):
return self._max(self._signed)<EOL>
Returns the maximal possible field *value* of the `Decimal` field.
f12145:c9:m18
def min(self):
return self._min(self._signed)<EOL>
Returns the minimal possible field *value* of the `Decimal` field.
f12145:c9:m19
def as_unsigned(self):
return self._cast(self._value, self._min(False), self._max(False), False)<EOL>
Returns the field *value* of the `Decimal` field as an unsigned integer.
f12145:c9:m20
def as_signed(self):
return self._cast(self._value, self._min(True), self._max(True), True)<EOL>
Returns the field *value* of the `Decimal` field as a signed integer.
f12145:c9:m21
@property<EOL><INDENT>def name(self):<DEDENT>
return self.item_type.name.capitalize()<EOL>
Returns the type name of the `Bit` field (read-only).
f12145:c10:m1
@staticmethod<EOL><INDENT>def is_bit():<DEDENT>
return True<EOL>
Returns ``True``.
f12145:c10:m2
@property<EOL><INDENT>def name(self):<DEDENT>
return self.item_type.name.capitalize()<EOL>
Returns the type name of the `Byte` field (read-only).
f12145:c11:m1
@property<EOL><INDENT>def value(self):<DEDENT>
return hex(self._value)<EOL>
Field value as a lowercase hexadecimal string prefixed with ``0x``.
f12145:c11:m2
@property<EOL><INDENT>def name(self):<DEDENT>
return self.item_type.name.capitalize()<EOL>
Returns the type name of the `Char` field (read-only).
f12145:c12:m1
@property<EOL><INDENT>def value(self):<DEDENT>
return chr(self._value)<EOL>
Field value as an unicode string character.
f12145:c12:m2
@property<EOL><INDENT>def value(self):<DEDENT>
return hex(self._value)<EOL>
Field value as a lowercase hexadecimal string prefixed with ``0x``.
f12145:c14:m1
@property<EOL><INDENT>def value(self):<DEDENT>
return '<STR_LIT>'.format(self._value, self.bit_size + <NUM_LIT:2>)<EOL>
Field value as a binary string prefixed with ``0b``.
f12145:c15:m1
@property<EOL><INDENT>def value(self):<DEDENT>
return bool(self._value)<EOL>
Field value as a boolean value, ``True`` or ``False``.
f12145:c16:m1
@staticmethod<EOL><INDENT>def is_bool():<DEDENT>
return True<EOL>
Returns ``True``.
f12145:c16:m3
@property<EOL><INDENT>def value(self):<DEDENT>
if self._enum and issubclass(self._enum, Enumeration):<EOL><INDENT>name = self._enum.get_name(self._value)<EOL>if name:<EOL><INDENT>return name<EOL><DEDENT><DEDENT>return self._value<EOL>
Field value as an enum name string. Fall back is an unsigned integer number.
f12145:c17:m1
@property<EOL><INDENT>def value(self):<DEDENT>
return self.as_float(self._value)<EOL>
Field value as a floating point number.
f12145:c18:m2
@property<EOL><INDENT>def scale(self):<DEDENT>
return self._scale<EOL>
Scaling factor of the `Scaled` field.
f12145:c18:m6
def scaling_base(self):
return <NUM_LIT:2> ** (self.bit_size - <NUM_LIT:1>) / <NUM_LIT:2><EOL>
Returns the scaling base of the `Scaled` field.
f12145:c18:m8
@property<EOL><INDENT>def name(self):<DEDENT>
return "<STR_LIT>".format(self.item_type.name.capitalize(),<EOL>self._bits_integer,<EOL>self.bit_size)<EOL>
Returns the type name of the `Fraction` field (read-only).
f12145:c19:m2
@property<EOL><INDENT>def value(self):<DEDENT>
return self.as_float(self._value)<EOL>
Field value as a floating point number.
f12145:c19:m3
@property<EOL><INDENT>def value(self):<DEDENT>
return str(datetime.datetime.utcfromtimestamp(self._value))<EOL>
Field value as an UTC datetime string in the ISO format ``YYYY-mm-dd HH:MM:SS``
f12145:c22:m1
@property<EOL><INDENT>def value(self):<DEDENT>
return str(ipaddress.IPv4Address(self._value))<EOL>
Field value as an IPv4 address formatted string.
f12145:c23:m1
@property<EOL><INDENT>def address(self):<DEDENT>
return self._value<EOL>
Returns the *data source* address of the :attr:`data` object referenced by the `Pointer` field (read-only).
f12145:c24:m1
@property<EOL><INDENT>def base_address(self):<DEDENT>
return self._value<EOL>
Returns the *data source* base address of the :attr:`data` object referenced by the `Pointer` field (read-only).
f12145:c24:m2
@property<EOL><INDENT>def data(self):<DEDENT>
return self._data<EOL>
`Data` object referenced by the `Pointer` field.
f12145:c24:m5
@property<EOL><INDENT>def data_byte_order(self):<DEDENT>
return self._data_byte_order<EOL>
:class:`Byteorder` used to deserialize and serialize the :attr:`data` object referenced by the `Pointer` field.
f12145:c24:m7
@property<EOL><INDENT>def data_size(self):<DEDENT>
<EOL>if is_container(self._data):<EOL><INDENT>byte_length, bit_length = self._data.container_size()<EOL>return byte_length + math.ceil(bit_length / <NUM_LIT:8>)<EOL><DEDENT>elif is_field(self._data):<EOL><INDENT>return math.ceil(self._data.bit_size / <NUM_LIT:8>)<EOL><DEDENT>else:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>
Returns the size of the :attr:`data` object in bytes (read-only).
f12145:c24:m9
@property<EOL><INDENT>def value(self):<DEDENT>
return hex(self._value)<EOL>
Field value as a lowercase hexadecimal string prefixed with ``0x``.
f12145:c24:m10
@staticmethod<EOL><INDENT>def is_pointer():<DEDENT>
return True<EOL>
Returns ``True``.
f12145:c24:m12
def is_null(self):
return self._value is <NUM_LIT:0><EOL>
Returns ``True`` if the `Pointer` field points to zero.
f12145:c24:m13
def deserialize_data(self, buffer=bytes(), byte_order=None):
index = Index(<NUM_LIT:0>, <NUM_LIT:0>, self.address, self.base_address, False)<EOL>if self._data:<EOL><INDENT>if byte_order not in ('<STR_LIT>', '<STR_LIT>', Byteorder.little, Byteorder.big):<EOL><INDENT>byte_order = self.data_byte_order<EOL><DEDENT>index = self._data.deserialize(buffer or self._data_stream,<EOL>index,<EOL>nested=False,<EOL>byte_order=byte_order)<EOL><DEDENT>return index<EOL>
De-serializes the :attr:`data` object referenced by the `Pointer` field from the byte *buffer* by mapping the bytes to the :attr:`~Field.value` for each :class:`Field` in the :attr:`data` object in accordance with the decoding *byte order* for the de-serialization and the decoding :attr:`byte_order` of each :class:`Field` in the :attr:`data` object. A specific decoding :attr:`byte_order` of a :class:`Field` in the :attr:`data` object overrules the decoding *byte order* for the de-serialization. Returns the :class:`Index` of the *buffer* after the last de-serialized :class:`Field` in the :attr:`data` object. :param bytes buffer: byte stream. Default is the internal :attr:`bytestream` of the `Pointer` field. :keyword byte_order: decoding byte order for the de-serialization. Default is the :attr:`data_byte_order` of the `Pointer` field. :type byte_order: :class:`Byteorder`, :class:`str`
f12145:c24:m14
def serialize_data(self, byte_order=None):
if self._data is None:<EOL><INDENT>return bytes()<EOL><DEDENT>if byte_order not in ('<STR_LIT>', '<STR_LIT>', Byteorder.little, Byteorder.big):<EOL><INDENT>byte_order = self.data_byte_order<EOL><DEDENT>buffer = bytearray()<EOL>self._data.serialize(buffer,<EOL>Index(<NUM_LIT:0>, <NUM_LIT:0>,<EOL>self.address, self.base_address,<EOL>False),<EOL>byte_order=byte_order)<EOL>return bytes(buffer)<EOL>
Serializes the :attr:`data` object referenced by the `Pointer` field to bytes by mapping the :attr:`~Field.value` for each :class:`Field` in the :attr:`data` object to a number of bytes in accordance with the encoding *byte order* for the serialization and the encoding :attr:`byte_order` of each :class:`Field` in the :attr:`data` object. A specific encoding :attr:`~Field.byte_order` of a :class:`Field` in the :attr:`data` object overrules the encoding *byte order* for the serialization. Returns a number of bytes for the serialized :attr:`data` object referenced by the `Pointer` field. :keyword byte_order: encoding byte order for the serialization. Default is the :attr:`data_byte_order` of the `Pointer` field. :type byte_order: :class:`Byteorder`, :class:`str`
f12145:c24:m15
def index_data(self):
<EOL>index = Index(<NUM_LIT:0>, <NUM_LIT:0>, self.address, self.base_address, False)<EOL>if is_container(self._data):<EOL><INDENT>self._data.index_fields(index, nested=True)<EOL><DEDENT>elif is_pointer(self._data):<EOL><INDENT>self._data.index_field(index)<EOL>self._data.index_data()<EOL><DEDENT>elif is_field(self._data):<EOL><INDENT>self._data.index_field(index)<EOL><DEDENT>
Indexes each :class:`Field` in the :attr:`data` object referenced by the `Pointer` field.
f12145:c24:m16
@nested_option(True)<EOL><INDENT>def read_from(self, provider, null_allowed=False, **options):<DEDENT>
if self._data is None:<EOL><INDENT>pass<EOL><DEDENT>elif is_provider(provider):<EOL><INDENT>if self._value < <NUM_LIT:0>:<EOL><INDENT>pass<EOL><DEDENT>elif null_allowed or self._value > <NUM_LIT:0>:<EOL><INDENT>while True:<EOL><INDENT>self.bytestream = provider.read(self.address, self.data_size)<EOL>index = self.deserialize_data()<EOL>if index.bit != <NUM_LIT:0>:<EOL><INDENT>length = index.byte, index.bit<EOL>raise ContainerLengthError(self, length)<EOL><DEDENT>if not index.update:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if is_mixin(self._data) and get_nested(options):<EOL><INDENT>self._data.read_from(provider, **options)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.bytestream = bytes()<EOL>self.deserialize_data()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ProviderTypeError(self, provider)<EOL><DEDENT>
Reads from the data :class:`Provider` the necessary number of bytes for the :attr:`data` object referenced by the `Pointer` field. A `Pointer` field stores the binary data read from the data :class:`Provider` in its :attr:`bytestream`. :param Provider provider: data :class:`Provider`. :param bool null_allowed: if ``True`` read access of address zero (Null) is allowed. :keyword bool nested: if ``True`` all :class:`Pointer` fields in the :attr:`data` object of the `Pointer` field reads their referenced :attr:`~Pointer.data` object fields as well (chained method call). Each `Pointer` field stores the bytes for its referenced :attr:`data` object in its :attr:`bytestream`.
f12145:c24:m17
def patch(self, item, byte_order=BYTEORDER):
<EOL>self.index_data()<EOL>if is_container(item):<EOL><INDENT>length = item.container_size()<EOL>if length[<NUM_LIT:1>] is not <NUM_LIT:0>:<EOL><INDENT>raise ContainerLengthError(item, length)<EOL><DEDENT>field = item.first_field()<EOL>if field is None:<EOL><INDENT>return None<EOL><DEDENT>index = field.index<EOL>if index.bit is not <NUM_LIT:0>:<EOL><INDENT>raise FieldIndexError(field, index)<EOL><DEDENT>buffer = bytearray(b'<STR_LIT:\x00>' * index.byte)<EOL>item.serialize(buffer, index, byte_order=byte_order)<EOL>content = buffer[index.byte:]<EOL>if len(content) != length[<NUM_LIT:0>]:<EOL><INDENT>raise BufferError(len(content), length[<NUM_LIT:0>])<EOL><DEDENT>return Patch(content,<EOL>index.address,<EOL>byte_order,<EOL>length[<NUM_LIT:0>] * <NUM_LIT:8>,<EOL><NUM_LIT:0>,<EOL>False)<EOL><DEDENT>elif is_field(item):<EOL><INDENT>index = item.index<EOL>alignment = item.alignment<EOL>if index.bit != alignment.bit_offset:<EOL><INDENT>raise FieldGroupOffsetError(<EOL>item, index, Alignment(alignment.byte_size, index.bit))<EOL><DEDENT>buffer = bytearray(b'<STR_LIT:\x00>' * index.byte)<EOL>item.serialize(buffer, index, byte_order=byte_order)<EOL>content = buffer[index.byte:]<EOL>if len(content) != alignment.byte_size:<EOL><INDENT>raise BufferError(len(content), alignment.byte_size)<EOL><DEDENT>patch_size, bit_offset = divmod(item.bit_size, <NUM_LIT:8>)<EOL>if bit_offset is not <NUM_LIT:0>:<EOL><INDENT>inject = True<EOL>patch_size += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>inject = False<EOL><DEDENT>patch_offset, bit_offset = divmod(alignment.bit_offset, <NUM_LIT:8>)<EOL>if bit_offset is not <NUM_LIT:0>:<EOL><INDENT>inject = True<EOL><DEDENT>if byte_order is Byteorder.big:<EOL><INDENT>start = alignment.byte_size - (patch_offset + patch_size)<EOL>stop = alignment.byte_size - patch_offset<EOL><DEDENT>else:<EOL><INDENT>start = patch_offset<EOL>stop = patch_offset + patch_size<EOL><DEDENT>return Patch(content[start:stop],<EOL>index.address + start,<EOL>byte_order,<EOL>item.bit_size,<EOL>bit_offset,<EOL>inject)<EOL><DEDENT>else:<EOL><INDENT>raise MemberTypeError(self, item)<EOL><DEDENT>
Returns a memory :class:`Patch` for the given *item* that shall be patched in the `data source`. :param item: item to patch. :param byte_order: encoding :class:`Byteorder` for the item. :type byte_order: :class:`Byteorder`, :class:`str`
f12145:c24:m18
def write_to(self, provider, item, byte_order=BYTEORDER):
<EOL>patch = self.patch(item, byte_order)<EOL>if patch is None:<EOL><INDENT>pass<EOL><DEDENT>elif is_provider(provider):<EOL><INDENT>if patch.inject:<EOL><INDENT>content = provider.read(patch.address, len(patch.buffer))<EOL>value = int.from_bytes(content, byte_order.value)<EOL>bit_mask = ~((<NUM_LIT:2> ** patch.bit_size - <NUM_LIT:1>) << patch.bit_offset)<EOL>bit_mask &= (<NUM_LIT:2> ** (len(patch.buffer) * <NUM_LIT:8>) - <NUM_LIT:1>)<EOL>value &= bit_mask<EOL>value |= int.from_bytes(patch.buffer, byte_order.value)<EOL>buffer = value.to_bytes(len(patch.buffer), byte_order.value)<EOL>provider.write(buffer, patch.address, len(buffer))<EOL><DEDENT>else:<EOL><INDENT>provider.write(patch.buffer, patch.address, len(patch.buffer))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ProviderTypeError(self, provider)<EOL><DEDENT>
Writes via a data :class:`Provider` the :class:`Field` values of the given *item* to the `data source`. :param Provider provider: data :class:`Provider`. :param item: item to write. :param byte_order: encoding :class:`Byteorder`. :type byte_order: :class:`Byteorder`, :class:`str`
f12145:c24:m19
@byte_order_option()<EOL><INDENT>@nested_option()<EOL>def deserialize(self, buffer=bytes(), index=Index(), **options):<DEDENT>
<EOL>index = super().deserialize(buffer, index, **options)<EOL>if self._data and get_nested(options):<EOL><INDENT>options[Option.byte_order.value] = self.data_byte_order<EOL>self._data.deserialize(self._data_stream,<EOL>Index(<NUM_LIT:0>, <NUM_LIT:0>,<EOL>self.address, self.base_address,<EOL>False),<EOL>**options)<EOL><DEDENT>return index<EOL>
De-serializes the `Pointer` field from the byte *buffer* starting at the begin of the *buffer* or with the given *index* by mapping the bytes to the :attr:`value` of the `Pointer` field in accordance with the decoding *byte order* for the de-serialization and the decoding :attr:`byte_order` of the `Pointer` field. The specific decoding :attr:`byte_order` of the `Pointer` field overrules the decoding *byte order* for the de-serialization. Returns the :class:`Index` of the *buffer* after the `Pointer` field. Optional the de-serialization of the referenced :attr:`data` object of the `Pointer` field can be enabled. :param bytes buffer: byte stream. :param Index index: current read :class:`Index` within the *buffer*. :keyword byte_order: decoding byte order for the de-serialization. :type byte_order: :class:`Byteorder`, :class:`str` :keyword bool nested: if ``True`` a `Pointer` field de-serialize its referenced :attr:`data` object as well (chained method call). Each :class:`Pointer` field uses for the de-serialization of its referenced :attr:`data` object its own :attr:`bytestream`.
f12145:c24:m20
@byte_order_option()<EOL><INDENT>@nested_option()<EOL>def serialize(self, buffer=bytearray(), index=Index(), **options):<DEDENT>
<EOL>index = super().serialize(buffer, index, **options)<EOL>if self._data and get_nested(options):<EOL><INDENT>options[Option.byte_order.value] = self.data_byte_order<EOL>self._data_stream = bytearray()<EOL>self._data.serialize(self._data_stream,<EOL>Index(<NUM_LIT:0>, <NUM_LIT:0>,<EOL>self.address, self.base_address,<EOL>False),<EOL>**options)<EOL>self._data_stream = bytes(self._data_stream)<EOL><DEDENT>return index<EOL>
Serializes the `Pointer` field to the byte *buffer* starting at the begin of the *buffer* or with the given *index* by mapping the :attr:`value` of the `Pointer` field to the byte *buffer* in accordance with the encoding *byte order* for the serialization and the encoding :attr:`byte_order` of the `Pointer` field. The specific encoding :attr:`byte_order` of the `Poiner` field overrules the encoding *byte order* for the serialization. Returns the :class:`Index` of the *buffer* after the `Pointer` field. Optional the serialization of the referenced :attr:`data` object of the `Pointer` field can be enabled. :param bytearray buffer: byte stream. :param Index index: current write :class:`Index` within the *buffer*. :keyword byte_order: encoding byte order for the serialization. :type byte_order: :class:`Byteorder`, :class:`str` :keyword bool nested: if ``True`` a `Pointer` field serializes its referenced :attr:`data` object as well (chained method call). Each :class:`Pointer` field uses for the serialization of its referenced :attr:`data` object its own :attr:`bytestream`.
f12145:c24:m21
def initialize_fields(self, content):
for name, value in content.items():<EOL><INDENT>if name is '<STR_LIT:value>':<EOL><INDENT>self.value = value<EOL><DEDENT>elif name is '<STR_LIT:data>':<EOL><INDENT>if is_mixin(self._data):<EOL><INDENT>self._data.initialize_fields(value)<EOL><DEDENT>elif is_field(self._data):<EOL><INDENT>self._data.value = value<EOL><DEDENT><DEDENT><DEDENT>
Initializes the `Pointer` field itself and the :class:`Field` items in the :attr:`data` object referenced by the `Pointer` field with the *values* in the *content* dictionary. The ``['value']`` key in the *content* dictionary refers to the `Pointer` field itself and the ``['data']`` key refers to the :attr:`data` object referenced by the `Pointer` field. :param dict content: a dictionary contains the :class:`~Field.value` for the `Pointer` field and the :class:`~Field.value` for each :class:`Field` in the :attr:`data` object referenced by the `Pointer` field.
f12145:c24:m22
@nested_option()<EOL><INDENT>def index_fields(self, index=Index(), **options):<DEDENT>
index = self.index_field(index)<EOL>if is_container(self._data):<EOL><INDENT>self._data.index_fields(Index(<NUM_LIT:0>, <NUM_LIT:0>,<EOL>self.address, self.base_address,<EOL>False),<EOL>**options)<EOL><DEDENT>elif is_pointer(self._data) and get_nested(options):<EOL><INDENT>self._data.index_fields(Index(<NUM_LIT:0>, <NUM_LIT:0>,<EOL>self.address, self.base_address,<EOL>False),<EOL>**options)<EOL><DEDENT>elif is_field(self._data):<EOL><INDENT>self._data.index_field(Index(<NUM_LIT:0>, <NUM_LIT:0>,<EOL>self.address, self.base_address,<EOL>False))<EOL><DEDENT>return index<EOL>
Indexes the `Pointer` field and the :attr:`data` object referenced by the `Pointer` field starting with the given *index* and returns the :class:`Index` after the `Pointer` field. :param Index index: :class:`Index` for the `Pointer` field. :keyword bool nested: if ``True`` all :class:`Pointer` fields in the :attr:`data` object referenced by the `Pointer` field indexes their referenced :attr:`~Pointer.data` object fields as well (chained method call).
f12145:c24:m23
@nested_option()<EOL><INDENT>def view_fields(self, *attributes, **options):<DEDENT>
items = OrderedDict()<EOL>if attributes:<EOL><INDENT>field_getter = attrgetter(*attributes)<EOL><DEDENT>else:<EOL><INDENT>field_getter = attrgetter('<STR_LIT:value>')<EOL><DEDENT>if len(attributes) > <NUM_LIT:1>:<EOL><INDENT>for key, value in zip(attributes, field_getter(self)):<EOL><INDENT>items[key] = value<EOL><DEDENT><DEDENT>else:<EOL><INDENT>items['<STR_LIT:value>'] = field_getter(self)<EOL><DEDENT>if is_container(self._data):<EOL><INDENT>items['<STR_LIT:data>'] = self._data.view_fields(*attributes, **options)<EOL><DEDENT>elif is_pointer(self._data) and get_nested(options):<EOL><INDENT>items['<STR_LIT:data>'] = self._data.view_fields(*attributes, **options)<EOL><DEDENT>elif is_field(self._data):<EOL><INDENT>if attributes:<EOL><INDENT>field_getter = attrgetter(*attributes)<EOL><DEDENT>else:<EOL><INDENT>field_getter = attrgetter('<STR_LIT:value>')<EOL><DEDENT>if len(attributes) > <NUM_LIT:1>:<EOL><INDENT>fieldnames = options.get('<STR_LIT>', attributes)<EOL>items['<STR_LIT:data>'] = dict(zip(fieldnames, field_getter(self._data)))<EOL><DEDENT>else:<EOL><INDENT>items['<STR_LIT:data>'] = field_getter(self._data)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>items['<STR_LIT:data>'] = self._data<EOL><DEDENT>return items<EOL>
Returns an :class:`ordered dictionary <collections.OrderedDict>` which contains the selected field *attributes* of the `Pointer` field itself extended with a ``['data']`` key which contains the selected field *attribute* or the dictionaries of the selected field *attributes* for each :class:`Field` *nested* in the :attr:`data` object referenced by the `Pointer` field. The *attributes* of each :class:`Field` for containers *nested* in the :attr:`data` object referenced by the `Pointer` field are viewed as well (chained method call). :param str attributes: selected :class:`Field` attributes. Fallback is the field :attr:`~Field.value`. :keyword tuple fieldnames: sequence of dictionary keys for the selected field *attributes*. Defaults to ``(*attributes)``. :keyword bool nested: if ``True`` all :class:`Pointer` fields in the :attr:`data` object referenced by the `Pointer` field views their referenced :attr:`~Pointer.data` object field attributes as well (chained method call).
f12145:c24:m24
@nested_option()<EOL><INDENT>def field_items(self, path=str(), **options):<DEDENT>
items = list()<EOL>items.append((path if path else '<STR_LIT>', self))<EOL>data_path = '<STR_LIT>'.format(path, '<STR_LIT:data>') if path else '<STR_LIT:data>'<EOL>if is_container(self._data):<EOL><INDENT>for field_item in self._data.field_items(data_path, **options):<EOL><INDENT>items.append(field_item)<EOL><DEDENT><DEDENT>elif is_pointer(self._data) and get_nested(options):<EOL><INDENT>for field_item in self._data.field_items(data_path, **options):<EOL><INDENT>items.append(field_item)<EOL><DEDENT><DEDENT>elif is_field(self._data):<EOL><INDENT>items.append((data_path, self._data))<EOL><DEDENT>return items<EOL>
Returns a **flatten** list of ``('field path', field item)`` tuples for the `Pointer` field itself and for each :class:`Field` *nested* in the :attr:`data` object referenced by the `Pointer` field. :param str path: path of the `Pointer` field. :keyword bool nested: if ``True`` all :class:`Pointer` fields in the :attr:`data` object referenced by the `Pointer` field lists their referenced :attr:`~Pointer.data` object field items as well (chained method call).
f12145:c24:m25
@nested_option(True)<EOL><INDENT>def describe(self, name=str(), **options):<DEDENT>
metadata = super().describe(name, **options)<EOL>metadata['<STR_LIT:class>'] = self.__class__.__name__<EOL>metadata['<STR_LIT:name>'] = name if name else self.__class__.__name__<EOL>metadata['<STR_LIT:type>'] = Pointer.item_type.name<EOL>if is_any(self._data) and get_nested(options):<EOL><INDENT>metadata['<STR_LIT>'] = list()<EOL>metadata['<STR_LIT>'].append(self._data.describe('<STR_LIT:data>', **options))<EOL><DEDENT>return metadata<EOL>
Returns the **metadata** of a `Pointer` as an :class:`ordered dictionary <collections.OrderedDict>`. .. code-block:: python metadata = { 'address': self.index.address, 'alignment': [self.alignment.byte_size, self.alignment.bit_offset], 'class': self.__class__.__name__, 'index': [self.index.byte, self.index.bit], 'max': self.max(), 'min': self.min(), 'name': name if name else self.__class__.__name__, 'order': self.byte_order.value, 'size': self.bit_size, 'type': Pointer.item_type.name, 'value': self.value, 'member': [self.data.describe()] } :param str name: optional name for the `Pointer` field. Fallback is the class name. :keyword bool nested: if ``True`` a :class:`Pointer` field lists its referenced :attr:`data` object fields as well (chained method call). Default is ``True``.
f12145:c24:m26
def append(self, item):
self._data.append(item)<EOL>
Appends the *item* to the end of the :class:`Sequence`. :param item: any :class:`Structure`, :class:`Sequence`, :class:`Array` or :class:`Field` instance.
f12145:c26:m6
def insert(self, index, item):
self._data.insert(index, item)<EOL>
Inserts the *item* before the *index* into the :class:`Sequence`. :param int index: :class:`Sequence` index. :param item: any :class:`Structure`, :class:`Sequence`, :class:`Array` or :class:`Field` instance.
f12145:c26:m7
def pop(self, index=-<NUM_LIT:1>):
return self._data.pop(index)<EOL>
Removes and returns the item at the *index* from the :class:`Sequence`. :param int index: :class:`Sequence` index.
f12145:c26:m8
def clear(self):
self._data.clear()<EOL>
Remove all items from the :class:`Sequence`.
f12145:c26:m9
def remove(self, item):
self._data.remove(item)<EOL>
Removes the first occurrence of an *item* from the :class:`Sequence`. :param item: any :class:`Structure`, :class:`Sequence`, :class:`Array` or :class:`Field` instance.
f12145:c26:m10