PyoMutableMapping
Bases: ,
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 | |
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
|
|
The key to look up. |
required |
Returns:
| Type | Description |
|---|---|
|
Option[V]: |
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 | |
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
|
|
The key to insert. |
required |
value
|
|
The value associated with the key. |
required |
Returns:
| Type | Description |
|---|---|
|
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 | |
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
|
|
The key to remove. |
required |
Returns:
| Type | Description |
|---|---|
|
Option[V]: The value associated with the removed key, or |
>>> 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 | |
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
|
|
The key to remove. |
required |
Returns:
| Type | Description |
|---|---|
|
Option[tuple[K, V]]: |
>>> 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 | |
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
|
|
The key to insert. |
required |
value
|
|
The value associated with the key. |
required |
Returns:
| Type | Description |
|---|---|
|
Result[V, KeyError]: |
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 | |