Skip to content

PyoMutableMapping

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

Base trait for eager pyochain mutable mapping collections.

PyoMutableMapping[K, V] is the shared trait for concrete, eager mutable mapping-like collections such as Dict.

It extends PyoMapping[K, V] and collections.abc.MutableMapping[K, V].

This is equivalent to subclassing collections.abc.MutableMapping[K, V] (this trait already does), meaning any concrete subclass must implement the required MutableMapping dunder methods:

  • __getitem__
  • __setitem__
  • __delitem__
  • __iter__
  • __len__

On top of the standard MutableMapping protocol, it provides the additional pyochain API (from PyoMapping, PyoCollection, Pipeable, Checkable, plus any helpers defined here).

Source code in src/pyochain/traits/_iterable.py
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
class PyoMutableMapping[K, V](PyoMapping[K, V], MutableMapping[K, V]):
    """Base trait for eager pyochain mutable mapping collections.

    `PyoMutableMapping[K, V]` is the shared trait for concrete, eager mutable mapping-like
    collections such as `Dict`.

    It extends `PyoMapping[K, V]` and `collections.abc.MutableMapping[K, V]`.

    This is equivalent to subclassing `collections.abc.MutableMapping[K, V]` (this trait
    already does), meaning any concrete subclass must implement the required
    `MutableMapping` dunder methods:

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

    On top of the standard `MutableMapping` protocol, it provides the additional
    pyochain API (from `PyoMapping`, `PyoCollection`, `Pipeable`, `Checkable`, plus any helpers defined here).
    """

    __slots__ = ()

    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
        >>> import pyochain as pc
        >>> data = pc.Dict.new()
        >>> 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
        >>> import pyochain as pc
        >>> d = pc.Dict.new()
        >>> 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.
        ```python
        >>> import pyochain as pc
        >>> data = pc.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.
        ```python
        >>> import pyochain as pc
        >>> data = pc.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
        >>> import pyochain as pc
        >>> data = {"a": 1}
        >>> pc.Dict(data).get_item("a")
        Some(1)
        >>> pc.Dict(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:

>>> import pyochain as pc
>>> data = {"a": 1}
>>> pc.Dict(data).get_item("a")
Some(1)
>>> pc.Dict(data).get_item("x").unwrap_or('Not Found')
'Not Found'

Source code in src/pyochain/traits/_iterable.py
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
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
    >>> import pyochain as pc
    >>> data = {"a": 1}
    >>> pc.Dict(data).get_item("a")
    Some(1)
    >>> pc.Dict(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:

>>> import pyochain as pc
>>> data = pc.Dict.new()
>>> 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/traits/_iterable.py
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
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
    >>> import pyochain as pc
    >>> data = pc.Dict.new()
    >>> 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.

>>> import pyochain as pc
>>> data = pc.Dict({1: "a", 2: "b"})
>>> data.remove(1)
Some('a')
>>> data.remove(3)
NONE
Source code in src/pyochain/traits/_iterable.py
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
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.
    ```python
    >>> import pyochain as pc
    >>> data = pc.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.

>>> import pyochain as pc
>>> data = pc.Dict({1: "a", 2: "b"})
>>> data.remove_entry(1)
Some((1, 'a'))
>>> data.remove_entry(3)
NONE
Source code in src/pyochain/traits/_iterable.py
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
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.
    ```python
    >>> import pyochain as pc
    >>> data = pc.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:

>>> import pyochain as pc
>>> d = pc.Dict.new()
>>> 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/traits/_iterable.py
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
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
    >>> import pyochain as pc
    >>> d = pc.Dict.new()
    >>> 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)