otupy.types.base.array.Array

class Array(args=[])

Bases: Openc2Type, list

OpenC2 Array

Implements OpenC2 Array:

An ordered list of unnamed fields with positionally-defined semantics. Each field has a position, label, and type. However, position does not matter in this implementation.

Array can be initialized with both a list, an Array, or a scalar. In the last case, an Array with a single value is created. Note that this overrides the typical behaviour of list (for instance, a str is not converted to an list of characters).

Derived classes must provide a fieldtypes dictionary that associate each field name to its class. This is strictly required in order to instantiate the object at deserialization time. However, no check is performed when new items are inserted.

Methods

append

Append object to the end of the list.

clear

Remove all items from list.

copy

Return a shallow copy of the list.

count

Return number of occurrences of value.

extend

Extend list by appending elements from the iterable.

fromdict

!!! WARNING !!! Currently not implemented because there are no examples of usage of this type (only Array/net, which is not clear)

index

Return first index of value.

insert

Insert object before index.

pop

Remove and return item at index (default last).

remove

Remove first occurrence of value.

reverse

Reverse IN PLACE.

sort

Sort the list in ascending order and return None.

todict

Converts to dictionary

validate

Validate the size of the array

Attributes

fieldtypes

Field types

__add__(value, /)

Return self+value.

__init__(args=[])

Array initialization

Arrays are initialized the same way as lists. They only take as input an interable (list, tuple, dict, or another Array/ArrayOf). Initialization with scalars is not allowed. In this case, a tuple must be used (mind to add the trailing comma even with a single value!)

Parameters:

args – The list, tuple, Array, ArrayOf used to initialize. May be empty.

__mul__(value, /)

Return self*value.

append(object, /)

Append object to the end of the list.

clear()

Remove all items from list.

copy()

Return a shallow copy of the list.

count(value, /)

Return number of occurrences of value.

extend(iterable, /)

Extend list by appending elements from the iterable.

fieldtypes = None

Field types

A dictionary which keys are field names and which values are the corresponding classes. Must be provided by any derived class.

fromdict(dic, e)

!!! WARNING !!! Currently not implemented because there are no examples of usage of this type (only Array/net, which is not clear)

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

insert(index, object, /)

Insert object before index.

pop(index=-1, /)

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove(value, /)

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

Reverse IN PLACE.

sort(*, key=None, reverse=False)

Sort the list in ascending order and return None.

The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).

If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.

The reverse flag can be set to sort in descending order.

todict(e)

Converts to dictionary

It is used to convert this object to an intermediary representation during serialization. It takes an Encoder argument that is used to recursively serialize inner data and structures (the Encoder provides standard methods for converting base types to dictionaries)..

Parameters:

e – The Encoder that is being used.

Returns:

A dictionary compliants to the Language Specification’s serialization rules.

validate(num_min=0, num_max=None)

Validate the size of the array

Validation is successfull if the number of elements is in the given range.

Parameters:
  • num_min – Minimun number of elements in the array (Default to: 0).

  • num_max – Maximun number of elements in the array (Default: no limit).

Returns:

True if the size in within the given range, a ValueError Exception otherwise.