traits.trait_list_object
Module¶
Classes¶
-
class
traits.trait_list_object.
TraitListEvent
(*, index=0, removed=None, added=None)[source]¶ An object reporting in-place changes to a trait list.
- Parameters
-
class
traits.trait_list_object.
TraitList
(*args, **kwargs)[source]¶ A subclass of list that validates and notifies listeners of changes.
- Parameters
value (iterable) – Iterable providing the items for the list
item_validator (callable, optional) – Called to validate and/or transform items added to the list. The callable should accept a single item from the list and return the transformed item, raising TraitError for invalid items. If not given, no item validation is performed.
notifiers (list of callable, optional) –
A list of callables with the signature:
notifier(trait_list, index, removed, added)
If this argument is not given, the list of notifiers is initially empty.
-
item_validator
¶ Called to validate and/or transform items added to the list. The callable should accept a single item from the list and return the transformed item, raising TraitError for invalid items.
- Type
callable
-
notifiers
¶ A list of callables with the signature:
notifier(trait_list, index, removed, added)
- Type
list of callable
-
notify
(index, removed, added)[source]¶ Call all notifiers.
This simply calls all notifiers provided by the class, if any. The notifiers are expected to have the signature:
notifier(trait_list, index, removed, added)
Any return values are ignored.
-
append
(object)[source]¶ Append object to the end of the list.
- Parameters
object (any) – The object to append.
-
extend
(iterable)[source]¶ Extend list by appending elements from the iterable.
- Parameters
iterable (iterable) – The elements to append.
-
insert
(index, object)[source]¶ Insert object before index.
- Parameters
index (integer) – The position at which to insert.
object (object) – The object to insert.
-
pop
(index=- 1)[source]¶ Remove and return item at index (default last).
- Parameters
index (int, optional) – Index at which to remove item. If not given, the last item of the list is removed.
- Returns
item – The removed item.
- Return type
- Raises
IndexError – If list is empty or index is out of range.
-
remove
(value)[source]¶ Remove first occurrence of value.
Notes
The value is not validated or converted before removal.
- Parameters
value (object) – Value to be removed.
- Raises
ValueError – If the value is not present.
-
sort
(*, key=None, reverse=False)[source]¶ 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.
- Parameters
key (callable) – Custom function that accepts a single item from the list and returns the key to be used in comparisons.
reverse (bool) – If true, the resulting list will be sorted in descending order.
-
class
traits.trait_list_object.
TraitListObject
(*args, **kwargs)[source]¶ A specialization of TraitList with a default validator and notifier which provide bug-for-bug compatibility with the TraitListObject from Traits versions before 6.0.
- Parameters
trait (CTrait) – The trait that the list has been assigned to.
object (HasTraits) – The object this list belongs to. Can also be None in cases where the list has been disconnected from its HasTraits parent.
name (str) – The name of the trait on the object.
value (iterable) – The initial value of the list.
-
object
¶ A callable that when called with no arguments returns the HasTraits object that this list belongs to, or None if there is no such object.
- Type
callable
-
value
¶ The initial value of the list.
- Type
iterable
-
notifier
(trait_list, index, removed, added)[source]¶ Converts and consolidates the parameters to a TraitListEvent and then fires the event.
-
append
(object)[source]¶ Append object to the end of the list.
- Parameters
object (any) – The object to append.
-
extend
(iterable)[source]¶ Extend list by appending elements from the iterable.
- Parameters
iterable (iterable) – The elements to append.
-
insert
(index, object)[source]¶ Insert object before index.
- Parameters
index (integer) – The position at which to insert.
object (object) – The object to insert.
-
pop
(index=- 1)[source]¶ Remove and return item at index (default last).
- Parameters
index (int, optional) – Index at which to remove item. If not given, the last item of the list is removed.
- Returns
item – The removed item.
- Return type
- Raises
IndexError – If list is empty or index is out of range.
-
remove
(value)[source]¶ Remove first occurrence of value.
Notes
The value is not validated or converted before removal.
- Parameters
value (object) – Value to be removed.
- Raises
ValueError – If the value is not present.