gemd.entity.valid_list module

A list that can validate its contents.

class gemd.entity.valid_list.ValidList(_list: Iterable, content_type: Optional[Union[Iterable[Type], Type]] = None, trigger: Optional[Callable[[T], Optional[T]]] = None)

Bases: list

A list-like class that verifies that its content conforms to specified types.

Parameters
  • _list (Iterable) – The initial values for the elements of the list.

  • content_type (Type or Iterable[Type]) – The allowed type(s) for the content of the list.

  • trigger (function) – A function that gets invoked whenever a new element is added. The function will get passed the value (or each individual value in a separate invocation for list operations) and, if it returns something other than None, it will use that returned value for the assignment.

append(value)

Add an item to the end of the list; equivalent to a[len(a):] = [x].

Validates that value is one of the allowed types.

Parameters

value (Any) – The value to append at the end of the list.

Returns

value is appended at the end of the list, if it is valid.

Return type

None

clear()

Remove all items from list.

copy()

Return a shallow copy of the list.

count(value, /)

Return number of occurrences of value.

extend(list_)

Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.

Validates that value is one of the allowed types.

Parameters

list (list) – The list of values to append at the end of the list.

Returns

list_ is appended at the end of the list, if all its entries are valid.

Return type

None

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

Return first index of value.

Raises ValueError if the value is not present.

insert(i, value)

Insert a value at a given position, if it is one of the allowed types.

a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

Parameters
  • i (int) – The index of the element before which to insert.

  • value (Any) – The value to insert into the list.

Returns

value is inserted into the list at position i, if it is valid.

Return type

None

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.