Skip to content

SetMut

Bases: PyoMutableSet[T]


              flowchart TD
              pyochain._set.SetMut[SetMut]
              pyochain.abc._set.PyoMutableSet[PyoMutableSet]
              pyochain.abc._set.PyoSet[PyoSet]
              pyochain.abc._collection.PyoCollection[PyoCollection]
              pyochain.abc._iterable.PyoIterable[PyoIterable]
              pyochain.rs.Pipeable[Pipeable]
              pyochain.rs.Into[Into]
              pyochain.rs.Inspect[Inspect]
              pyochain.rs.Checkable[Checkable]
              pyochain.abc._collection.PyoContainer[PyoContainer]
              pyochain.abc._collection.PyoSized[PyoSized]

                              pyochain.abc._set.PyoMutableSet --> pyochain._set.SetMut
                                pyochain.abc._set.PyoSet --> pyochain.abc._set.PyoMutableSet
                                pyochain.abc._collection.PyoCollection --> pyochain.abc._set.PyoSet
                                pyochain.abc._iterable.PyoIterable --> pyochain.abc._collection.PyoCollection
                                pyochain.rs.Pipeable --> pyochain.abc._iterable.PyoIterable
                                pyochain.rs.Into --> pyochain.rs.Pipeable
                
                pyochain.rs.Inspect --> pyochain.rs.Pipeable
                

                pyochain.rs.Checkable --> pyochain.abc._iterable.PyoIterable
                

                pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
                
                pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
                





              click pyochain._set.SetMut href "" "pyochain._set.SetMut"
              click pyochain.abc._set.PyoMutableSet href "" "pyochain.abc._set.PyoMutableSet"
              click pyochain.abc._set.PyoSet href "" "pyochain.abc._set.PyoSet"
              click pyochain.abc._collection.PyoCollection href "" "pyochain.abc._collection.PyoCollection"
              click pyochain.abc._iterable.PyoIterable href "" "pyochain.abc._iterable.PyoIterable"
              click pyochain.rs.Pipeable href "" "pyochain.rs.Pipeable"
              click pyochain.rs.Into href "" "pyochain.rs.Into"
              click pyochain.rs.Inspect href "" "pyochain.rs.Inspect"
              click pyochain.rs.Checkable href "" "pyochain.rs.Checkable"
              click pyochain.abc._collection.PyoContainer href "" "pyochain.abc._collection.PyoContainer"
              click pyochain.abc._collection.PyoSized href "" "pyochain.abc._collection.PyoSized"
            

A mutable, unordered collection of unique elements.

Unlike Set which is immutable, SetMut allows in-place modification of elements.

Implement the collections::abc::MutableSet interface, so elements can be modified in place, and passed to any function/object expecting a standard mutable set.

Underlying data structure is a set.

Tip

If you have an existing set, consider using SetMut::from_ref to avoid unnecessary copying.

Parameters:

Name Type Description Default
data Iterable[T]

Any Iterable of elements to initialize the set with.

required
Source code in src/pyochain/_set.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
class SetMut[T](PyoMutableSet[T]):  # noqa: PLW1641
    """A mutable, unordered collection of unique elements.

    Unlike [`Set`][Set] which is immutable, `SetMut` allows in-place modification of elements.

    Implement the `collections::abc::MutableSet` interface, so elements can be modified in place, and passed to any function/object expecting a standard mutable `set`.

    Underlying data structure is a `set`.

    Tip:
        If you have an existing `set`, consider using [`SetMut::from_ref`][from_ref] to avoid unnecessary copying.

    Args:
        data (Iterable[T]): Any `Iterable` of elements to initialize the set with.
    """

    __slots__ = ("_inner",)  # pyright: ignore[reportUnannotatedClassAttribute, reportIncompatibleUnannotatedOverride]
    _inner: set[T]

    def __init__(self, data: Iterable[T]) -> None:
        self._inner = set(data)

    @override
    def __iter__(self) -> Iterator[T]:
        return iter(self._inner)

    @override
    def __len__(self) -> int:
        return len(self._inner)

    @override
    def __contains__(self, item: object) -> bool:
        return item in self._inner

    @override
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}({get_repr(self._inner)})"

    @override
    def __eq__(self, other: object) -> bool:
        return _set_eq(self, other)

    @property
    @no_doctest
    def inner(self) -> set[T]:
        """Get the underlying `set` data structure.

        Useful when interoperating with functions that require a standard Python `set`.

        Returns:
            set[T]: The underlying set.
        """
        return self._inner

    @staticmethod
    def from_ref[V](data: set[V]) -> SetMut[V]:
        """Create a `SetMut` from a reference to an existing `set`.

        This method wraps the provided `set` without copying it, allowing for efficient object instanciation.

        This is the recommended way to create a `SetMut` from foreign functions that return `set` objects.

        Warning:
            Since the `SetMut` directly references the original `set`, any modifications made to the `SetMut` will also affect the original `set`, and vice versa.

        Args:
            data (set[V]): The `set` to wrap.

        Returns:
            SetMut[V]: A new `SetMut` instance.

        Example:
            ```python
            >>> from pyochain import SetMut
            >>> original_set = {1, 2, 3}
            >>> set_obj = SetMut.from_ref(original_set)
            >>> set_obj
            SetMut(1, 2, 3)
            >>> original_set.add(4)
            >>> set_obj
            SetMut(1, 2, 3, 4)

            ```
        """
        instance: SetMut[V] = SetMut.__new__(SetMut)  # pyright: ignore[reportUnknownVariableType]
        instance._inner = data
        return instance

    @override
    def add(self, value: T) -> None:
        """Add an element to **self**.

        Args:
            value (T): The element to add.

        Example:
            ```python
            >>> from pyochain import SetMut
            >>> s = SetMut(("a", "b"))
            >>> s.add("c")
            >>> s.iter().sort()
            Vec('a', 'b', 'c')

            ```
        """
        self._inner.add(value)

    @override
    def discard(self, value: T) -> None:
        """Remove an element from **self** if it is a member.

        Unlike [`SetMut::remove`][remove], this method does not raise an exception when an element is missing from the set.

        Args:
            value (T): The element to remove.

        Example:
            ```python
            >>> from pyochain import SetMut
            >>> s = SetMut(("a", "b", "c"))
            >>> s.discard("b")
            >>> s.iter().sort()
            Vec('a', 'c')

            ```
        """
        self._inner.discard(value)

    @override
    def intersection(self, other: AbstractSet[T]) -> SetMut[T]:
        return self.from_ref(self._inner & other)

    @override
    def union(self, other: AbstractSet[T]) -> SetMut[T]:
        return self.from_ref(self._inner | other)

    @override
    def difference(self, other: AbstractSet[T]) -> SetMut[T]:
        return self.from_ref(self._inner - other)

    @override
    def symmetric_difference(self, other: AbstractSet[T]) -> SetMut[T]:
        return self.from_ref(self._inner ^ other)

inner property

Get the underlying set data structure.

Useful when interoperating with functions that require a standard Python set.

Returns:

Type Description
set[T]

set[T]: The underlying set.

add(value)

Add an element to self.

Parameters:

Name Type Description Default
value T

The element to add.

required
Example
>>> from pyochain import SetMut
>>> s = SetMut(("a", "b"))
>>> s.add("c")
>>> s.iter().sort()
Vec('a', 'b', 'c')
Source code in src/pyochain/_set.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
@override
def add(self, value: T) -> None:
    """Add an element to **self**.

    Args:
        value (T): The element to add.

    Example:
        ```python
        >>> from pyochain import SetMut
        >>> s = SetMut(("a", "b"))
        >>> s.add("c")
        >>> s.iter().sort()
        Vec('a', 'b', 'c')

        ```
    """
    self._inner.add(value)

discard(value)

Remove an element from self if it is a member.

Unlike [SetMut::remove][remove], this method does not raise an exception when an element is missing from the set.

Parameters:

Name Type Description Default
value T

The element to remove.

required
Example
>>> from pyochain import SetMut
>>> s = SetMut(("a", "b", "c"))
>>> s.discard("b")
>>> s.iter().sort()
Vec('a', 'c')
Source code in src/pyochain/_set.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
@override
def discard(self, value: T) -> None:
    """Remove an element from **self** if it is a member.

    Unlike [`SetMut::remove`][remove], this method does not raise an exception when an element is missing from the set.

    Args:
        value (T): The element to remove.

    Example:
        ```python
        >>> from pyochain import SetMut
        >>> s = SetMut(("a", "b", "c"))
        >>> s.discard("b")
        >>> s.iter().sort()
        Vec('a', 'c')

        ```
    """
    self._inner.discard(value)

from_ref(data) staticmethod

Create a SetMut from a reference to an existing set.

This method wraps the provided set without copying it, allowing for efficient object instanciation.

This is the recommended way to create a SetMut from foreign functions that return set objects.

Warning

Since the SetMut directly references the original set, any modifications made to the SetMut will also affect the original set, and vice versa.

Parameters:

Name Type Description Default
data set[V]

The set to wrap.

required

Returns:

Type Description
SetMut[V]

SetMut[V]: A new SetMut instance.

Example
>>> from pyochain import SetMut
>>> original_set = {1, 2, 3}
>>> set_obj = SetMut.from_ref(original_set)
>>> set_obj
SetMut(1, 2, 3)
>>> original_set.add(4)
>>> set_obj
SetMut(1, 2, 3, 4)
Source code in src/pyochain/_set.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
@staticmethod
def from_ref[V](data: set[V]) -> SetMut[V]:
    """Create a `SetMut` from a reference to an existing `set`.

    This method wraps the provided `set` without copying it, allowing for efficient object instanciation.

    This is the recommended way to create a `SetMut` from foreign functions that return `set` objects.

    Warning:
        Since the `SetMut` directly references the original `set`, any modifications made to the `SetMut` will also affect the original `set`, and vice versa.

    Args:
        data (set[V]): The `set` to wrap.

    Returns:
        SetMut[V]: A new `SetMut` instance.

    Example:
        ```python
        >>> from pyochain import SetMut
        >>> original_set = {1, 2, 3}
        >>> set_obj = SetMut.from_ref(original_set)
        >>> set_obj
        SetMut(1, 2, 3)
        >>> original_set.add(4)
        >>> set_obj
        SetMut(1, 2, 3, 4)

        ```
    """
    instance: SetMut[V] = SetMut.__new__(SetMut)  # pyright: ignore[reportUnknownVariableType]
    instance._inner = data
    return instance