Skip to content

PyoMutableMapping

Bases: PyoMapping[K, V], MutableMapping[K, V], ABC


              flowchart TD
              pyochain.abc._mappings.PyoMutableMapping[PyoMutableMapping]
              pyochain.abc._mappings.PyoMapping[PyoMapping]
              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._mappings.PyoMapping --> pyochain.abc._mappings.PyoMutableMapping
                                pyochain.abc._collection.PyoCollection --> pyochain.abc._mappings.PyoMapping
                                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.abc._mappings.PyoMutableMapping href "" "pyochain.abc._mappings.PyoMutableMapping"
              click pyochain.abc._mappings.PyoMapping href "" "pyochain.abc._mappings.PyoMapping"
              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"
            

Extends PyoMapping[K, V] and collections.abc.MutableMapping[K, V].

Serves as a base class for pyochain mutable mappings, such as Dict.

Any concrete subclass must implement the required MutableMapping dunder methods:

  • __getitem__
  • __setitem__
  • __delitem__
  • __iter__
  • __len__
Source code in src/pyochain/abc/_mappings.py
106
107
108
109
110
111
112
113
114
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
258
259
260
class PyoMutableMapping[K, V](PyoMapping[K, V], MutableMapping[K, V], ABC):
    """Extends `PyoMapping[K, V]` and `collections.abc.MutableMapping[K, V]`.

    Serves as a base class for pyochain mutable mappings, such as `Dict`.

    Any concrete subclass must implement the required `MutableMapping` dunder methods:

    - `__getitem__`
    - `__setitem__`
    - `__delitem__`
    - `__iter__`
    - `__len__`

    """

    # pyrefly: ignore [implicit-any-attribute]
    __slots__ = ()  # pyright: ignore[reportUnannotatedClassAttribute]

    def insert(self, key: K, value: V) -> Option[V]:
        """Insert a key-value pair into the `MutableMapping`.

        If the `MutableMapping` did not have this **key** present, `NONE` is returned.

        If the `MutableMapping` did have this **key** present, the **value** is updated, and the old value is returned.

        The **key** is not updated.

        Args:
            key (K): The key to insert.
            value (V): The value associated with the key.

        Returns:
            Option[V]: The previous value associated with the key, or None if the key was not present.

        Example:
            ```python
            >>> from pyochain import Dict
            >>> data = Dict(())
            >>> data.insert(37, "a")
            NONE
            >>> data.is_empty()
            False

            >>> data.insert(37, "b")
            Some('a')
            >>> data.insert(37, "c")
            Some('b')
            >>> data[37]
            'c'

            ```
        """
        previous = self.get(key, None)
        self[key] = value
        return option(previous)

    def try_insert(self, key: K, value: V) -> Result[V, KeyError]:
        """Tries to insert a key-value pair into the `MutableMapping`, and returns a `Result[V, KeyError]` containing the value in the entry (if successful).

        If the `MutableMapping` already had this **key** present, nothing is updated, and an error containing the occupied entry and the value is returned.

        Args:
            key (K): The key to insert.
            value (V): The value associated with the key.

        Returns:
            Result[V, KeyError]: `Ok` containing the value if the **key** was not present, or `Err` containing a `KeyError` if the **key** already existed.

        Example:
            ```python
            >>> from pyochain import Dict
            >>> d = Dict(())
            >>> d.try_insert(37, "a").unwrap()
            'a'
            >>> d.try_insert(37, "b")
            Err(KeyError('Key 37 already exists with value a.'))

            ```
        """
        if key in self:
            return Err(KeyError(f"Key {key} already exists with value {self[key]}."))
        self[key] = value
        return Ok(value)

    def remove(self, key: K) -> Option[V]:
        """Remove a **key** from the `MutableMapping` and return its value if it existed.

        Equivalent to `dict.pop(key, None)`, with an `Option` return type.

        Args:
            key (K): The key to remove.

        Returns:
            Option[V]: The value associated with the removed **key**, or `None` if the **key** was not present.

        Example:
            ```python
            >>> from pyochain import Dict
            >>> data = Dict({1: "a", 2: "b"})
            >>> data.remove(1)
            Some('a')
            >>> data.remove(3)
            NONE

            ```
        """
        return option(self.pop(key, None))

    def remove_entry(self, key: K) -> Option[tuple[K, V]]:
        """Remove a key from the `MutableMapping` and return the item if it existed.

        Return an `Option[tuple[K, V]]` containing the (key, value) pair if the key was present.

        Args:
            key (K): The key to remove.

        Returns:
            Option[tuple[K, V]]: `Some((key, value))` pair associated with the removed key, or `None` if the **key** was not present.

        Example:
            ```python
            >>> from pyochain import Dict
            >>> data = Dict({1: "a", 2: "b"})
            >>> data.remove_entry(1)
            Some((1, 'a'))
            >>> data.remove_entry(3)
            NONE

            ```
        """
        return Some((key, self.pop(key))) if key in self else NONE

    def get_item(self, key: K) -> Option[V]:
        """Retrieve a value from the `MutableMapping`.

        Returns `Some(value)` if the **key** exists, or `None` if it does not.

        Args:
            key (K): The key to look up.

        Returns:
            Option[V]: `Some(value)` that is associated with the **key**, or `None` if not found.

        Example:
            ```python
            >>> from pyochain import Dict
            >>> data = Dict.from_ref({"a": 1})
            >>> data.get_item("a")
            Some(1)
            >>> data.get_item("x").unwrap_or("Not Found")
            'Not Found'

            ```
        """
        return option(self.get(key, None))

get_item(key)

Retrieve a value from the MutableMapping.

Returns Some(value) if the key exists, or None if it does not.

Parameters:

Name Type Description Default
key K

The key to look up.

required

Returns:

Type Description
Option[V]

Option[V]: Some(value) that is associated with the key, or None if not found.

Example
>>> from pyochain import Dict
>>> data = Dict.from_ref({"a": 1})
>>> data.get_item("a")
Some(1)
>>> data.get_item("x").unwrap_or("Not Found")
'Not Found'
Source code in src/pyochain/abc/_mappings.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def get_item(self, key: K) -> Option[V]:
    """Retrieve a value from the `MutableMapping`.

    Returns `Some(value)` if the **key** exists, or `None` if it does not.

    Args:
        key (K): The key to look up.

    Returns:
        Option[V]: `Some(value)` that is associated with the **key**, or `None` if not found.

    Example:
        ```python
        >>> from pyochain import Dict
        >>> data = Dict.from_ref({"a": 1})
        >>> data.get_item("a")
        Some(1)
        >>> data.get_item("x").unwrap_or("Not Found")
        'Not Found'

        ```
    """
    return option(self.get(key, None))

insert(key, value)

Insert a key-value pair into the MutableMapping.

If the MutableMapping did not have this key present, NONE is returned.

If the MutableMapping did have this key present, the value is updated, and the old value is returned.

The key is not updated.

Parameters:

Name Type Description Default
key K

The key to insert.

required
value V

The value associated with the key.

required

Returns:

Type Description
Option[V]

Option[V]: The previous value associated with the key, or None if the key was not present.

Example
>>> from pyochain import Dict
>>> data = Dict(())
>>> data.insert(37, "a")
NONE
>>> data.is_empty()
False

>>> data.insert(37, "b")
Some('a')
>>> data.insert(37, "c")
Some('b')
>>> data[37]
'c'
Source code in src/pyochain/abc/_mappings.py
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
def insert(self, key: K, value: V) -> Option[V]:
    """Insert a key-value pair into the `MutableMapping`.

    If the `MutableMapping` did not have this **key** present, `NONE` is returned.

    If the `MutableMapping` did have this **key** present, the **value** is updated, and the old value is returned.

    The **key** is not updated.

    Args:
        key (K): The key to insert.
        value (V): The value associated with the key.

    Returns:
        Option[V]: The previous value associated with the key, or None if the key was not present.

    Example:
        ```python
        >>> from pyochain import Dict
        >>> data = Dict(())
        >>> data.insert(37, "a")
        NONE
        >>> data.is_empty()
        False

        >>> data.insert(37, "b")
        Some('a')
        >>> data.insert(37, "c")
        Some('b')
        >>> data[37]
        'c'

        ```
    """
    previous = self.get(key, None)
    self[key] = value
    return option(previous)

remove(key)

Remove a key from the MutableMapping and return its value if it existed.

Equivalent to dict.pop(key, None), with an Option return type.

Parameters:

Name Type Description Default
key K

The key to remove.

required

Returns:

Type Description
Option[V]

Option[V]: The value associated with the removed key, or None if the key was not present.

Example
>>> from pyochain import Dict
>>> data = Dict({1: "a", 2: "b"})
>>> data.remove(1)
Some('a')
>>> data.remove(3)
NONE
Source code in src/pyochain/abc/_mappings.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def remove(self, key: K) -> Option[V]:
    """Remove a **key** from the `MutableMapping` and return its value if it existed.

    Equivalent to `dict.pop(key, None)`, with an `Option` return type.

    Args:
        key (K): The key to remove.

    Returns:
        Option[V]: The value associated with the removed **key**, or `None` if the **key** was not present.

    Example:
        ```python
        >>> from pyochain import Dict
        >>> data = Dict({1: "a", 2: "b"})
        >>> data.remove(1)
        Some('a')
        >>> data.remove(3)
        NONE

        ```
    """
    return option(self.pop(key, None))

remove_entry(key)

Remove a key from the MutableMapping and return the item if it existed.

Return an Option[tuple[K, V]] containing the (key, value) pair if the key was present.

Parameters:

Name Type Description Default
key K

The key to remove.

required

Returns:

Type Description
Option[tuple[K, V]]

Option[tuple[K, V]]: Some((key, value)) pair associated with the removed key, or None if the key was not present.

Example
>>> from pyochain import Dict
>>> data = Dict({1: "a", 2: "b"})
>>> data.remove_entry(1)
Some((1, 'a'))
>>> data.remove_entry(3)
NONE
Source code in src/pyochain/abc/_mappings.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
def remove_entry(self, key: K) -> Option[tuple[K, V]]:
    """Remove a key from the `MutableMapping` and return the item if it existed.

    Return an `Option[tuple[K, V]]` containing the (key, value) pair if the key was present.

    Args:
        key (K): The key to remove.

    Returns:
        Option[tuple[K, V]]: `Some((key, value))` pair associated with the removed key, or `None` if the **key** was not present.

    Example:
        ```python
        >>> from pyochain import Dict
        >>> data = Dict({1: "a", 2: "b"})
        >>> data.remove_entry(1)
        Some((1, 'a'))
        >>> data.remove_entry(3)
        NONE

        ```
    """
    return Some((key, self.pop(key))) if key in self else NONE

try_insert(key, value)

Tries to insert a key-value pair into the MutableMapping, and returns a Result[V, KeyError] containing the value in the entry (if successful).

If the MutableMapping already had this key present, nothing is updated, and an error containing the occupied entry and the value is returned.

Parameters:

Name Type Description Default
key K

The key to insert.

required
value V

The value associated with the key.

required

Returns:

Type Description
Result[V, KeyError]

Result[V, KeyError]: Ok containing the value if the key was not present, or Err containing a KeyError if the key already existed.

Example
>>> from pyochain import Dict
>>> d = Dict(())
>>> d.try_insert(37, "a").unwrap()
'a'
>>> d.try_insert(37, "b")
Err(KeyError('Key 37 already exists with value a.'))
Source code in src/pyochain/abc/_mappings.py
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
def try_insert(self, key: K, value: V) -> Result[V, KeyError]:
    """Tries to insert a key-value pair into the `MutableMapping`, and returns a `Result[V, KeyError]` containing the value in the entry (if successful).

    If the `MutableMapping` already had this **key** present, nothing is updated, and an error containing the occupied entry and the value is returned.

    Args:
        key (K): The key to insert.
        value (V): The value associated with the key.

    Returns:
        Result[V, KeyError]: `Ok` containing the value if the **key** was not present, or `Err` containing a `KeyError` if the **key** already existed.

    Example:
        ```python
        >>> from pyochain import Dict
        >>> d = Dict(())
        >>> d.try_insert(37, "a").unwrap()
        'a'
        >>> d.try_insert(37, "b")
        Err(KeyError('Key 37 already exists with value a.'))

        ```
    """
    if key in self:
        return Err(KeyError(f"Key {key} already exists with value {self[key]}."))
    self[key] = value
    return Ok(value)