Dict
Bases: PyoMutableMapping[K, V], PyoReversible[K], KwargsWrapper[K, V]
flowchart TD
pyochain.core._dict.Dict[Dict]
pyochain.abc._mappings.PyoMutableMapping[PyoMutableMapping]
pyochain.abc._mappings.PyoMapping[PyoMapping]
pyochain.abc._collection.PyoCollection[PyoCollection]
pyochain.abc._sequences.PyoReversible[PyoReversible]
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.constructors.KwargsWrapper[KwargsWrapper]
pyochain.abc.constructors.FromKwargs[FromKwargs]
pyochain.abc.constructors.FromIter[FromIter]
pyochain.abc.constructors.Wrapper[Wrapper]
pyochain.abc._mappings.PyoMutableMapping --> pyochain.core._dict.Dict
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
pyochain.abc._sequences.PyoReversible --> pyochain.core._dict.Dict
pyochain.abc._iterable.PyoIterable --> pyochain.abc._sequences.PyoReversible
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.constructors.KwargsWrapper --> pyochain.core._dict.Dict
pyochain.abc.constructors.FromKwargs --> pyochain.abc.constructors.KwargsWrapper
pyochain.abc.constructors.FromIter --> pyochain.abc.constructors.FromKwargs
pyochain.abc.constructors.Wrapper --> pyochain.abc.constructors.KwargsWrapper
click pyochain.core._dict.Dict href "" "pyochain.core._dict.Dict"
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._sequences.PyoReversible href "" "pyochain.abc._sequences.PyoReversible"
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"
click pyochain.abc.constructors.KwargsWrapper href "" "pyochain.abc.constructors.KwargsWrapper"
click pyochain.abc.constructors.FromKwargs href "" "pyochain.abc.constructors.FromKwargs"
click pyochain.abc.constructors.FromIter href "" "pyochain.abc.constructors.FromIter"
click pyochain.abc.constructors.Wrapper href "" "pyochain.abc.constructors.Wrapper"
A Dict is a key-value store similar to Python's built-in dict, but with additional methods inspired by Rust's HashMap.
Implement the MutableMapping interface, so all standard dictionary operations are supported.
Source code in pyochain/core/_dict.pyi
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 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 | |
__new__(iterable=(), /, **kwargs)
__new__(iterable: DictConvertible[K, V]) -> Dict[K, V]
__new__(**kwargs: V) -> Dict[str, V]
__new__(
iterable: DictConvertible[str, V], /, **kwargs: V
) -> Dict[str, V]
Create a new Dict instance.
Accept the same input types as the built-in dict, including Mapping, Iterable of key-value pairs, and objects implementing __getitem__() and keys().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
DictConvertible[K, V]
|
Initial data for the Dict that can converted to a dictionary. |
()
|
**kwargs
|
V
|
Additional key-value pairs to include in the Dict. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new |
See Also
wrap: Create aDictfrom an existing dictionary, no-copy.of: Create aDictfrom keyword arguments.Dict::from_object: Create aDictfrom an object's__dict__attribute, no-copy.
Example
The most straightforward way to create a Dict is from a standard Python dict.
This will copy the data, just like the built-in dict constructor.
from pyochain import Dict
py_dict = {1: "a", 2: "b"}
pyochain_dict = Dict(py_dict)
assert pyochain_dict == Dict({1: "a", 2: "b"})
dict::items, or an Iterator of tuples.
from pyochain import Dict, Iter, Seq
names = Seq("alice", "bob", "charlie", "dave")
ages = (30, 25, 35, 40)
records = names.iter().zip(ages).collect(Dict)
assert records == Dict({"alice": 30, "bob": 25, "charlie": 35, "dave": 40})
assert records.items().iter().collect(Seq) == (
("alice", 30),
("bob", 25),
("charlie", 35),
("dave", 40),
)
Mapping protocol can also be directly converted to a Dict:
from collections.abc import Mapping, Iterator, Iterable
from dataclasses import dataclass
@dataclass
class CustomMapping(Mapping[int, str]):
data: dict[int, str]
def __getitem__(self, key: int) -> str:
return self.data[key]
def __iter__(self) -> Iterator[int]:
return iter(self.data)
def __len__(self) -> int:
return len(self.data)
custom_mapping = CustomMapping({1: "a", 2: "b"})
assert Dict(custom_mapping) == Dict({1: "a", 2: "b"})
__getitem__ and keys:
from pyochain import Dict
class MinimalDictLike:
def __init__(self, data: dict[int, str]) -> None:
self._data = data
def keys(self) -> Iterable[int]:
return iter(self._data)
def __getitem__(self, key: int) -> str:
return self._data[key]
minimal_dict_like = MinimalDictLike({1: "a", 2: "b"})
assert Dict(minimal_dict_like) == Dict({1: "a", 2: "b"})
Source code in pyochain/core/_dict.pyi
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
from_keys(keys, value=None)
classmethod
Create a Dict from an iterable of keys, all mapped to the same value.
This is the equivalent of dict.fromkeys, but returns a Dict instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
Iterable[K1]
|
An iterable of keys to include in the mapping. |
required |
value
|
V1
|
The value that each key will be mapped to. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[K1, V1]
|
Dict[K1, V1]: A new |
Example
from pyochain import Dict
d = Dict.from_keys(["a", "b", "c"], 1)
assert d == Dict(a=1, b=1, c=1)
d2 = Dict.from_keys("abc")
assert d2 == Dict(a=None, b=None, c=None)
Source code in pyochain/core/_dict.pyi
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
from_object(obj)
staticmethod
Create a Dict from an object __dict__ attribute.
We can't know in advance the values types, so we use object.
Syntactic sugar for Dict.from_ref(obj.__dict__).
Warning
This take a direct reference to the object's __dict__, so any modifications to the resulting Dict will also affect the original object's attributes, and vice versa.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
The object whose |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, object]
|
Dict[str, object]: A new |
Example
from pyochain import Dict, Some
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
person = Person("Alice", 30)
pyo_dict = Dict.from_object(person)
assert pyo_dict == Dict(name="Alice", age=30)
assert pyo_dict.insert("name", "Bob") == Some("Alice")
assert person == Person(name="Bob", age=30)
Source code in pyochain/core/_dict.pyi
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 | |
copy()
Create a shallow copy of the Dict.
Returns:
| Type | Description |
|---|---|
Dict[K, V]
|
Dict[K, V]: The copied |
Example
from pyochain import Dict
d1 = Dict(a=1, b=2)
d2 = d1.copy()
assert d2 == Dict(a=1, b=2)
assert d1 is not d2
Source code in pyochain/core/_dict.pyi
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
union(other)
Merge another dict or Dict with this Dict, returning a new one with the combined key-value pairs.
If there are duplicate keys, the values from other will overwrite those in Self.
This is equivalent to | on a standard Python dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
IntoDict[T1, T2]
|
The other mapping to merge with. |
required |
Returns:
| Type | Description |
|---|---|
Dict[K | T1, V | T2]
|
Dict[K | T1, V | T2]: A new mapping containing the merged key-value pairs. |
See Also
Dict::union_mut: Merge another mapping intoSelfin-place.
Example
from pyochain import Dict
d1 = Dict(a=1, b=2)
d2 = Dict(c=2, d=3)
d3 = d1.union(d2)
assert d3 == Dict(a=1, b=2, c=2, d=3)
assert d1 is not d3 and d2 is not d3
Source code in pyochain/core/_dict.pyi
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 | |
union_mut(other)
Merge another dict or Dict into Self in-place.
If there are duplicate keys, the values from other will overwrite those in Self.
This is equivalent to |= on a standard Python dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
SupportsKeysAndGetItem[K, V] | Iterable[tuple[K, V]]
|
The other mapping to merge with. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The modified |
See Also
Dict::union: Merge another mapping withSelfin a newDict.Dict::updateto accept any compatibleIterable.
Example
from pyochain import Dict, Some
d1 = Dict(a=1, b=2)
d2 = Dict(c=2, d=3)
d1.union_mut(d2)
assert d1 == Dict(a=1, b=2, c=2, d=3)
d1.union_mut((("e", 4), ("f", 5)))
assert d1 == Dict(a=1, b=2, c=2, d=3, e=4, f=5)
assert d1.insert("b", 100) == Some(2)
assert d1 == Dict(a=1, b=100, c=2, d=3, e=4, f=5)
assert d2 == Dict(c=2, d=3)
Source code in pyochain/core/_dict.pyi
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 | |