NamedValueSet

class lsst.daf.butler.NamedValueSet(elements: Iterable[K] = ())

Bases: NameMappingSetView[K], NamedValueMutableSet[K]

Custom mutable set class.

A custom mutable set class that requires elements to have a .name attribute, which can then be used as keys in dict-like lookup.

Names and elements can both be used with the in and del operators, remove, and discard. Names (but not elements) can be used with []-based element retrieval (not assignment) and the get method.

Parameters:
elementsiterable

Iterable over elements to include in the set.

Raises:
AttributeError

Raised if one or more elements do not have a .name attribute.

Notes

Iteration order is guaranteed to be the same as insertion order (with the same general behavior as dict ordering). Like dicts, sets with the same elements will compare as equal even if their iterator order is not the same.

Attributes Summary

names

Return set of names associated with the keys, in the same order.

Methods Summary

add(element)

Add an element to the set.

asMapping()

Return a mapping view with names as keys.

clear()

This is slow (creates N new iterators!) but effective.

copy()

Return a new NamedValueSet with the same elements.

discard(element)

Remove an element from the set if it exists.

freeze()

Disable all mutators.

get(key[, default])

Return the element with the given name.

isdisjoint(other)

Return True if two sets have a null intersection.

issubset(other)

issuperset(other)

pop(*args)

Remove and return an element from the set.

remove(element)

Remove an element from the set.

update(elements)

Add multiple new elements to the set.

Attributes Documentation

names

Methods Documentation

add(element: K) None

Add an element to the set.

Raises:
AttributeError

Raised if the element does not have a .name attribute.

asMapping() Mapping[str, K_co]

Return a mapping view with names as keys.

Returns:
dictMapping

A dictionary-like view with values() == self.

clear() None

This is slow (creates N new iterators!) but effective.

copy() NamedValueSet[K]

Return a new NamedValueSet with the same elements.

discard(element: str | K) Any

Remove an element from the set if it exists.

Does nothing if no matching element is present.

Parameters:
elementobject or str

Element to remove or the string name thereof. Assumed to be an element if it has a .name attribute.

freeze() NamedValueAbstractSet[K]

Disable all mutators.

Effectively transforming self into an immutable set.

Returns:
selfNamedValueAbstractSet

While self is modified in-place, it is also returned with a type annotation that reflects its new, frozen state; assigning it to a new variable (and considering any previous references invalidated) should allow for more accurate static type checking.

get(key: str | K_co, default: Any = None) Any

Return the element with the given name.

Returns default if no such element is present.

isdisjoint(other)

Return True if two sets have a null intersection.

issubset(other: Set[K]) bool
issuperset(other: Set[K]) bool
pop(*args: str) K

Remove and return an element from the set.

Parameters:
namestr, optional

Name of the element to remove and return. Must be passed positionally. If not provided, an arbitrary element is removed and returned.

Raises:
KeyError

Raised if name is provided but default is not, and no matching element exists.

remove(element: str | K) Any

Remove an element from the set.

Parameters:
elementobject or str

Element to remove or the string name thereof. Assumed to be an element if it has a .name attribute.

Raises:
KeyError

Raised if an element with the given name does not exist.

update(elements: Iterable[K]) None

Add multiple new elements to the set.

Parameters:
elementsIterable

Elements to add.