ValidatedList

class openff.toolkit.utils.collections.ValidatedList(seq=(), converter=None, validator=None)[source]

A list that runs custom converter and validators when new elements are added.

Multiple converters and validators can be assigned to the list. These are executed in the given order with converters run before validators.

Validators must take the new element as the first argument and raise an exception if validation fails.

validator(new_element) -> None

Converters must also take the new element as the first argument, but they have to return the converted value.

converter(new_element) -> converted_value

Examples

We can define validator and converter functions that are run on each element of the list.

>>> def is_positive_validator(value):
...     if value <= 0:
...         raise TypeError('value must be positive')
...
>>> vl = ValidatedList([1, -1], validator=is_positive_validator)
Traceback (most recent call last):
...
TypeError: value must be positive

Multiple converters that are run before the validators can be specified.

>>> vl = ValidatedList([-1, '2', 3.0], converter=[float, abs],
...                    validator=is_positive_validator)
>>> vl
[1.0, 2.0, 3.0]
__init__(seq=(), converter=None, validator=None)[source]

Initialize the list.

Parameters:
  • seq (Iterable) – A sequence of elements.

  • converter (callable or list[callable]) – Functions that will be used to convert each new element of the list.

  • validator (callable or list[callable]) – Functions that will be used to convert each new element of the list.

Methods

__init__([seq, converter, validator])

Initialize the list.

append(p_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.

index(value[, start, stop])

Return first index of value.

insert(index, p_object)

Insert object before index.

pop([index])

Remove and return item at index (default last).

remove(value, /)

Remove first occurrence of value.

reverse()

Reverse IN PLACE.

sort(*[, key, reverse])

Sort the list in ascending order and return None.

extend(iterable)[source]

Extend list by appending elements from the iterable.

append(p_object)[source]

Append object to the end of the list.

insert(index, p_object)[source]

Insert object before index.

copy()[source]

Return a shallow copy of the list.

clear()

Remove all items from list.

count(value, /)

Return number of occurrences of value.

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

Return first index of value.

Raises ValueError if the value is not present.

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.