traits.trait_set_object Module

Classes

class traits.trait_set_object.TraitSetEvent(*, removed=None, added=None)[source]

An object reporting in-place changes to a traits sets.

Parameters
  • removed (set, optional) – Old values that were removed from the set.

  • added (set, optional) – New values added to the set.

removed

Old values that were removed from the set.

Type

set

added

New values added to the set.

Type

set

class traits.trait_set_object.TraitSet(value=(), *, item_validator=None, notifiers=None)[source]

A subclass of set that validates and notifies listeners of changes.

Parameters
  • value (iterable, optional) – Iterable providing the items for the set.

  • item_validator (callable, optional) – Called to validate and/or transform items added to the set. The callable should accept a single item 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_set, removed, added)
    

    Where ‘added’ is a set containing new values that have been added. And ‘removed’ is a set containing old values that have been removed.

    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 set. The callable should accept a single item and return the transformed item, raising TraitError for invalid items.

Type

callable

notifiers

A list of callables with the signature:

notifier(trait_set, removed, added)

where ‘added’ is a set containing new values that have been added and ‘removed’ is a set containing old values that have been removed.

Type

list of callable

notify(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_set, removed, added)

Any return values are ignored. Any exceptions raised are not handled. Notifiers are therefore expected not to raise any exceptions under normal use.

Parameters
  • removed (set) – The items that have been removed.

  • added (set) – The new items that have been added to the set.

add(value)[source]

Add an element to a set.

This has no effect if the element is already present.

Parameters

value (any) – The value to add to the set.

clear()[source]

Remove all elements from this set.

discard(value)[source]

Remove an element from the set if it is a member.

If the element is not a member, do nothing.

Parameters

value (any) – An item in the set

difference_update(*args)[source]

Remove all elements of another set from this set.

Parameters

args (iterables) – The other iterables.

intersection_update(*args)[source]

Update the set with the intersection of itself and another set.

Parameters

args (iterables) – The other iterables.

pop()[source]

Remove and return an arbitrary set element.

Raises KeyError if the set is empty.

Returns

item – An element from the set.

Return type

any

Raises

KeyError – If the set is empty.

remove(value)[source]

Remove an element that is a member of the set.

If the element is not a member, raise a KeyError.

Parameters

value (any) – An element in the set

Raises

KeyError – If the value is not found in the set.

symmetric_difference_update(value)[source]

Update the set with the symmetric difference of itself and another.

Parameters

value (iterable) –

update(*args)[source]

Update the set with the union of itself and others.

Parameters

args (iterables) – The other iterables.

class traits.trait_set_object.TraitSetObject(trait, object, name, value)[source]

A specialization of TraitSet with a default validator and notifier for compatibility with Traits versions before 6.0.

Parameters
  • trait (CTrait) – The trait that the set has been assigned to.

  • object (HasTraits) – The object the set belongs to.

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

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

trait

The trait that the set has been assigned to.

Type

CTrait

object

The object the set belongs to.

Type

HasTraits

name

The name of the trait on the object.

Type

str

value

The initial value of the set.

Type

iterable

notifier(trait_set, removed, added)[source]

Converts and consolidates the parameters to a TraitSetEvent and then fires the event.

Parameters
  • trait_set (set) – The complete set

  • removed (set) – Set of values that were removed.

  • added (set) – Set of values that were added.