PyoMutableMapping
Bases: PyoMapping[K, V], MutableMapping[K, V]
flowchart TD
pyochain.abc._mappings.PyoMutableMapping[PyoMutableMapping]
pyochain.abc._mappings.PyoMapping[PyoMapping]
pyochain.abc._collection.PyoCollection[PyoCollection]
pyochain.abc._iterable.PyoIterable[PyoIterable]
pyochain.abc._collection.PyoContainer[PyoContainer]
pyochain.abc._collection.PyoSized[PyoSized]
pyochain.abc._mixins.Checkable[Checkable]
pyochain.abc._mixins.Fluent[Fluent]
pyochain.abc._mixins.Pipe[Pipe]
pyochain.abc._mixins.Tap[Tap]
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.abc._mixins.Checkable --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Fluent --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Pipe --> pyochain.abc._mixins.Fluent
pyochain.abc._mixins.Tap --> pyochain.abc._mixins.Fluent
pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
pyochain.abc._mixins.Checkable --> pyochain.abc._collection.PyoContainer
pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
pyochain.abc._mixins.Checkable --> pyochain.abc._collection.PyoSized
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.abc._collection.PyoContainer href "" "pyochain.abc._collection.PyoContainer"
click pyochain.abc._collection.PyoSized href "" "pyochain.abc._collection.PyoSized"
click pyochain.abc._mixins.Checkable href "" "pyochain.abc._mixins.Checkable"
click pyochain.abc._mixins.Fluent href "" "pyochain.abc._mixins.Fluent"
click pyochain.abc._mixins.Pipe href "" "pyochain.abc._mixins.Pipe"
click pyochain.abc._mixins.Tap href "" "pyochain.abc._mixins.Tap"
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 pyochain/abc/_mappings.pyi
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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
popitem()
Remove and return a (key, value) pair from the PyoMutableMapping.
Pairs are returned in LIFO order in the default implementation.
It can be useful to destructively iterate over self, as often used in set algorithms.
Raises KeyError if the PyoMutableMapping is empty.
Returns:
| Type | Description |
|---|---|
tuple[K, V]
|
tuple[K, V]: The removed (key, value) pair. |
Example
from pyochain import Dict, Ok, Err
d = Dict(a=1, b=2)
popped = d.popitem()
assert popped == ("b", 2)
popped = d.popitem()
assert popped == ("a", 1)
try:
res = Ok(d.popitem())
except KeyError as e:
res = Err(e)
assert repr(res) == '''Err(KeyError('popitem(): dictionary is empty'))'''
# LIFO order is preserved
d = Dict(a=1, b=2, c=3)
assert d.popitem() == ("c", 3)
assert d.popitem() == ("b", 2)
assert d.popitem() == ("a", 1)
assert d.is_empty()
Source code in pyochain/abc/_mappings.pyi
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 | |
update(m=..., /, **kwargs)
update(m: SupportsKeysAndGetItem[K, V]) -> None
update(
m: SupportsKeysAndGetItem[str, V], /, **kwargs: V
) -> None
update(m: Iterable[tuple[K, V]]) -> None
update(m: Iterable[tuple[str, V]], /, **kwargs: V) -> None
update(**kwargs: V) -> None
In-place update of self, with key-value pairs from another Mapping or Iterable.
Keywords will be applied after the m argument, allowing for additional updates.
If a key already exists in self, its value will be overwritten.
If a given key is present in both m and kwargs, the value from the latter will be the one effectively present in the resulting Dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
object
|
The |
...
|
**kwargs
|
V
|
Additional key-value pairs to update self with. |
{}
|
Example
from pyochain import Dict
d = Dict({1: "a", 2: "b"})
d.update({2: "c", 3: "d"})
assert d == Dict({1: "a", 2: "c", 3: "d"})
d.update([(3, "e"), (4, "f")])
assert d == Dict({1: "a", 2: "c", 3: "e", 4: "f"})
d1 = Dict({"John": 30, "Jane": 25})
d1.update({"John": 26, "Doe": 22}, John=31, Mary=28)
assert d1 == Dict({"John": 31, "Jane": 25, "Doe": 22, "Mary": 28})
Source code in pyochain/abc/_mappings.pyi
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 | |
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, Some
data = Dict(())
assert data.insert(37, "a").is_none()
assert not data.is_empty()
assert data.insert(37, "b") == Some("a")
assert data.insert(37, "c") == Some("b")
assert data[37] == "c"
Source code in pyochain/abc/_mappings.pyi
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
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]: |
Example
from pyochain import Dict, Err
d = Dict(())
assert d.try_insert("a", 1).unwrap() == 1
x = d.try_insert("a", 2)
assert x.is_err()
unwrapped = x.unwrap_err()
assert isinstance(unwrapped, KeyError)
assert repr(unwrapped) == "KeyError('Key a already exists with value 1.')"
assert d == Dict(a=1)
Source code in pyochain/abc/_mappings.pyi
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
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 |
Example
from pyochain import Dict, Some
data = Dict(a=1, b=2)
assert data.remove("a") == Some(1)
assert data.remove("c").is_none()
Source code in pyochain/abc/_mappings.pyi
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
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]]: |
Example
from pyochain import Dict, Some
data = Dict(a=1, b=2)
assert data.remove_entry("a") == Some(("a", 1))
assert data.remove_entry("c").is_none()
Source code in pyochain/abc/_mappings.pyi
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |