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
  • index (int or slice, optional) – An index or slice indicating the location of the changes to the trait list. The default is 0.

  • added (list, optional) – The list of values added to the trait list.

  • removed (list, optional) – The list of values removed from the list.

index

An index or slice indicating the location of the changes to the list.

Type

int or slice

added

The list of values added to the list. If nothing was added this is an empty list.

Type

list

removed

The list of values removed from the list. If nothing was removed this is an empty list.

Type

list

class traits.trait_list_object.TraitList(iterable=(), *, item_validator=None, notifiers=None)[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.

Parameters
  • index (int or slice) – The indices being modified by the operation.

  • removed (list) – The items to be removed.

  • added (list) – The items being added to the list.

append(object)[source]

Append object to the end of the list.

Parameters

object (any) – The object to append.

clear()[source]

Remove all items from list.

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

object

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.

reverse()[source]

Reverse the items in the list in place.

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(trait, object, name, value)[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 the list belongs to.

  • name (str) – The name of the trait on the object.

  • value (iterable) – The initial value of the list.

trait

The trait that the list has been assigned to.

Type

CTrait

object

The object the list belongs to.

Type

HasTraits

name

The name of the trait on the object.

Type

str

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.

Parameters
  • trait_list (list) – The list

  • index (int or slice) – Index or slice that was modified

  • removed (list) – Values that were removed

  • added (list) – Values that were added

append(object)[source]

Append object to the end of the list.

Parameters

object (any) – The object to append.

clear()[source]

Remove all items from list.

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

object

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.