Dict¶
Bases: ProcessDict[K, V], IterDict[K, V], NestedDict[K, V], JoinsDict[K, V], FilterDict[K, V], GroupsDict[K, V]
Wrapper for Python dictionaries with chainable methods.
Source code in src/pyochain/_dict/_main.py
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 | |
__init__ ¶
__init__(data: T) -> None
Source code in src/pyochain/_core/_main.py
34 35 | |
__repr__ ¶
__repr__() -> str
Source code in src/pyochain/_dict/_main.py
35 36 | |
apply ¶
apply(
func: Callable[Concatenate[dict[K, V], P], dict[KU, VU]],
*args: P.args,
**kwargs: P.kwargs,
) -> Dict[KU, VU]
Apply a function to the underlying dict and return a Dict of the result. Allow to pass user defined functions that transform the dict while retaining the Dict wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[Concatenate[dict[K, V], P], dict[KU, VU]]
|
Function to apply to the underlying dict. |
required |
*args
|
P.args
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the function. |
{}
|
Example:
>>> import pyochain as pc
>>> def invert_dict(d: dict[K, V]) -> dict[V, K]:
... return {v: k for k, v in d.items()}
>>> pc.Dict({'a': 1, 'b': 2}).apply(invert_dict).unwrap()
{1: 'a', 2: 'b'}
Source code in src/pyochain/_core/_main.py
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 | |
diff ¶
diff(other: Mapping[K, V]) -> Dict[K, tuple[V | None, V | None]]
Returns a dict of the differences between this dict and another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Mapping[K, V]
|
The mapping to compare against. |
required |
The keys of the returned dict are the keys that are not shared or have different values. The values are tuples containing the value from self and the value from other.
>>> import pyochain as pc
>>> d1 = {"a": 1, "b": 2, "c": 3}
>>> d2 = {"b": 2, "c": 4, "d": 5}
>>> pc.Dict(d1).diff(d2).sort().unwrap()
{'a': (1, None), 'c': (3, 4), 'd': (None, 5)}
Source code in src/pyochain/_dict/_joins.py
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 | |
diff_keys ¶
diff_keys(*others: Mapping[K, V]) -> Dict[K, V]
Return a new Dict keeping only keys present in self but not in others.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Mapping[K, V]
|
Other mappings to exclude keys from. |
()
|
>>> import pyochain as pc
>>> d1 = {"a": 1, "b": 2, "c": 3}
>>> d2 = {"b": 10, "d": 40}
>>> d3 = {"c": 30}
>>> pc.Dict(d1).diff_keys(d2, d3).unwrap()
{'a': 1}
Source code in src/pyochain/_dict/_filters.py
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 | |
drop ¶
drop(*keys: K) -> Dict[K, V]
Return a new Dict with given keys removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
K
|
Sequence of keys to remove from the dictionary. |
()
|
New dict has d[key] deleted for each supplied key.
>>> import pyochain as pc
>>> pc.Dict({"x": 1, "y": 2}).drop("y").unwrap()
{'x': 1}
>>> pc.Dict({"x": 1, "y": 2}).drop("y", "x").unwrap()
{}
>>> pc.Dict({"x": 1}).drop("y").unwrap() # Ignores missing keys
{'x': 1}
>>> pc.Dict({1: 2, 3: 4}).drop(1).unwrap()
{3: 4}
Source code in src/pyochain/_dict/_process.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
equals_to ¶
equals_to(other: Self | Mapping[Any, Any]) -> bool
Check if two records are equal based on their data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Self | Mapping[Any, Any]
|
Another Dict or mapping to compare against. |
required |
Example:
>>> import pyochain as pc
>>> d1 = pc.Dict({"a": 1, "b": 2})
>>> d2 = pc.Dict({"a": 1, "b": 2})
>>> d3 = pc.Dict({"a": 1, "b": 3})
>>> d1.equals_to(d2)
True
>>> d1.equals_to(d3)
False
Source code in src/pyochain/_dict/_main.py
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 | |
filter_attr ¶
filter_attr(attr: str, dtype: type[U] = object) -> Dict[K, U]
Filter values that have a given attribute.
This does not enforce type checking at runtime for performance considerations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr
|
str
|
Attribute name to check for. |
required |
dtype
|
type[U]
|
Optional expected type of the attribute for type hinting. |
object
|
Example:
>>> import pyochain as pc
>>> pc.Dict({"a": "hello", "b": "world", "c": 2, "d": 5}).filter_attr(
... "capitalize", str
... ).unwrap()
{'a': 'hello', 'b': 'world'}
Source code in src/pyochain/_dict/_filters.py
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 | |
filter_callable ¶
filter_callable() -> Dict[K, Callable[..., Any]]
Filter values that are callable.
>>> import pyochain as pc
>>> def foo():
... pass
>>> data = {1: "one", 2: "two", 3: foo, 4: print}
>>> pc.Dict(data).filter_callable().map_values(lambda x: x.__name__).unwrap()
{3: 'foo', 4: 'print'}
Source code in src/pyochain/_dict/_filters.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
filter_items ¶
filter_items(predicate: Callable[[tuple[K, V]], bool]) -> Dict[K, V]
Filter items by predicate applied to (key, value) tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[tuple[K, V]], bool]
|
Function to determine if a (key, value) pair should be included. |
required |
Example:
>>> import pyochain as pc
>>> def isvalid(item):
... k, v = item
... return k % 2 == 0 and v < 4
>>> d = pc.Dict({1: 2, 2: 3, 3: 4, 4: 5})
>>>
>>> d.filter_items(isvalid).unwrap()
{2: 3}
>>> d.filter_items(lambda kv: not isvalid(kv)).unwrap()
{1: 2, 3: 4, 4: 5}
Source code in src/pyochain/_dict/_filters.py
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 | |
filter_keys ¶
filter_keys(predicate: Callable[[K], bool]) -> Dict[K, V]
Return a new Dict containing keys that satisfy predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[K], bool]
|
Function to determine if a key should be included. |
required |
Example:
>>> import pyochain as pc
>>> d = {1: 2, 2: 3, 3: 4, 4: 5}
>>> pc.Dict(d).filter_keys(lambda x: x % 2 == 0).unwrap()
{2: 3, 4: 5}
Source code in src/pyochain/_dict/_filters.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
filter_kv ¶
filter_kv(predicate: Callable[[K, V], bool]) -> Dict[K, V]
Filter items by predicate applied to unpacked (key, value) tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[K, V], bool]
|
Function to determine if a key-value pair should be included. |
required |
Example:
>>> import pyochain as pc
>>> def isvalid(key, value):
... return key % 2 == 0 and value < 4
>>> d = pc.Dict({1: 2, 2: 3, 3: 4, 4: 5})
>>>
>>> d.filter_kv(isvalid).unwrap()
{2: 3}
>>> d.filter_kv(lambda k, v: not isvalid(k, v)).unwrap()
{1: 2, 3: 4, 4: 5}
Source code in src/pyochain/_dict/_filters.py
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 | |
filter_subclass ¶
filter_subclass(parent: type[R], keep_parent: bool = True) -> Dict[K, type[R]]
Filter values that are subclasses of a given parent class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
type[R]
|
Parent class to check against. |
required |
keep_parent
|
bool
|
Whether to include the parent class itself. Defaults to True. |
True
|
>>> import pyochain as pc
>>> class A:
... pass
>>> class B(A):
... pass
>>> class C:
... pass
>>> def name(cls: type[Any]) -> str:
... return cls.__name__
>>> data = pc.Dict({"first": A, "second": B, "third": C})
>>> data.filter_subclass(A).map_values(name).unwrap()
{'first': 'A', 'second': 'B'}
>>> data.filter_subclass(A, keep_parent=False).map_values(name).unwrap()
{'second': 'B'}
Source code in src/pyochain/_dict/_filters.py
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 | |
filter_type ¶
filter_type(typ: type[R]) -> Dict[K, R]
Filter values by type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
typ
|
type[R]
|
Type to filter values by. |
required |
Example:
>>> import pyochain as pc
>>> data = {"a": "one", "b": "two", "c": 3, "d": 4}
>>> pc.Dict(data).filter_type(str).unwrap()
{'a': 'one', 'b': 'two'}
Source code in src/pyochain/_dict/_filters.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
filter_values ¶
filter_values(predicate: Callable[[V], bool]) -> Dict[K, V]
Return a new Dict containing items whose values satisfy predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[V], bool]
|
Function to determine if a value should be included. |
required |
Example:
>>> import pyochain as pc
>>> d = {1: 2, 2: 3, 3: 4, 4: 5}
>>> pc.Dict(d).filter_values(lambda x: x % 2 == 0).unwrap()
{1: 2, 3: 4}
>>> pc.Dict(d).filter_values(lambda x: not x > 3).unwrap()
{1: 2, 2: 3}
Source code in src/pyochain/_dict/_filters.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
flatten ¶
flatten(sep: str = '.', max_depth: int | None = None) -> Dict[str, Any]
Flatten a nested dictionary, concatenating keys with the specified separator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sep
|
str
|
Separator to use when concatenating keys |
'.'
|
max_depth
|
int | None
|
Maximum depth to flatten. If None, flattens completely. |
None
|
>>> import pyochain as pc
>>> data = {
... "config": {"params": {"retries": 3, "timeout": 30}, "mode": "fast"},
... "version": 1.0,
... }
>>> pc.Dict(data).flatten().unwrap()
{'config.params.retries': 3, 'config.params.timeout': 30, 'config.mode': 'fast', 'version': 1.0}
>>> pc.Dict(data).flatten(sep="_").unwrap()
{'config_params_retries': 3, 'config_params_timeout': 30, 'config_mode': 'fast', 'version': 1.0}
>>> pc.Dict(data).flatten(max_depth=1).unwrap()
{'config.params': {'retries': 3, 'timeout': 30}, 'config.mode': 'fast', 'version': 1.0}
Source code in src/pyochain/_dict/_nested.py
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 | |
for_each ¶
for_each(
func: Callable[Concatenate[K, V, P], Any], *args: P.args, **kwargs: P.kwargs
) -> Dict[K, V]
Apply a function to each key-value pair in the dict for side effects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[Concatenate[K, V, P], Any]
|
Function to apply to each key-value pair. |
required |
*args
|
P.args
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the function. |
{}
|
Returns the original Dict unchanged.
>>> import pyochain as pc
>>> pc.Dict({"a": 1, "b": 2}).for_each(
... lambda k, v: print(f"Key: {k}, Value: {v}")
... ).unwrap()
Key: a, Value: 1
Key: b, Value: 2
{'a': 1, 'b': 2}
Source code in src/pyochain/_dict/_process.py
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 | |
from_
staticmethod
¶
from_(data: Mapping[G, I] | SupportsKeysAndGetItem[G, I]) -> Dict[G, I]
Create a Dict from a mapping or SupportsKeysAndGetItem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[G, I] | SupportsKeysAndGetItem[G, I]
|
A mapping or object supporting keys and item access to convert into a Dict. |
required |
>>> import pyochain as pc
>>> class MyMapping:
... def __init__(self):
... self._data = {1: "a", 2: "b", 3: "c"}
...
... def keys(self):
... return self._data.keys()
...
... def __getitem__(self, key):
... return self._data[key]
>>>
>>> pc.Dict.from_(MyMapping()).unwrap()
{1: 'a', 2: 'b', 3: 'c'}
Source code in src/pyochain/_dict/_main.py
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 | |
from_object
staticmethod
¶
from_object(obj: object) -> Dict[str, Any]
Create a Dict from an object's dict attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
The object whose |
required |
>>> import pyochain as pc
>>> class Person:
... def __init__(self, name: str, age: int):
... self.name = name
... self.age = age
>>> person = Person("Alice", 30)
>>> pc.Dict.from_object(person).unwrap()
{'name': 'Alice', 'age': 30}
Source code in src/pyochain/_dict/_main.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
get_in ¶
get_in(*keys: K, default: Any = None) -> Any
Retrieve a value from a nested dictionary structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
K
|
Sequence of keys representing the nested path to retrieve the value. |
()
|
default
|
Any
|
Default value to return if the keys do not exist. |
None
|
>>> import pyochain as pc
>>> data = {"a": {"b": {"c": 1}}}
>>> pc.Dict(data).get_in("a", "b", "c")
1
>>> pc.Dict(data).get_in("a", "x", default="Not Found")
'Not Found'
Source code in src/pyochain/_dict/_nested.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
group_by_key ¶
group_by_key(func: Callable[[K], G]) -> Dict[G, dict[K, V]]
Group dict items into sub-dictionaries based on a function of the key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[K], G]
|
Function to determine the group for each key. |
required |
>>> import pyochain as pc
>>> d = {"user_1": 10, "user_2": 20, "admin_1": 100}
>>> pc.Dict(d).group_by_key(lambda k: k.split("_")[0]).unwrap()
{'user': {'user_1': 10, 'user_2': 20}, 'admin': {'admin_1': 100}}
Source code in src/pyochain/_dict/_groups.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
group_by_key_agg ¶
group_by_key_agg(
key_func: Callable[[K], G], agg_func: Callable[[Dict[K, V]], R]
) -> Dict[G, R]
Group by key function, then apply aggregation function to each sub-dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_func
|
Callable[[K], G]
|
Function to determine the group for each key. |
required |
agg_func
|
Callable[[Dict[K, V]], R]
|
Function to aggregate each sub-dictionary. |
required |
This avoids materializing intermediate Dict objects if you only need
an aggregated result for each group.
>>> import pyochain as pc
>>>
>>> data = {"user_1": 10, "user_2": 20, "admin_1": 100}
>>> pc.Dict(data).group_by_key_agg(
... key_func=lambda k: k.split("_")[0],
... agg_func=lambda d: d.iter_values().sum(),
... ).unwrap()
{'user': 30, 'admin': 100}
>>>
>>> data_files = {
... "file_a.txt": 100,
... "file_b.log": 20,
... "file_c.txt": 50,
... "file_d.log": 5,
... }
>>>
>>> def get_stats(sub_dict: pc.Dict[str, int]) -> dict[str, Any]:
... return {
... "count": sub_dict.iter_keys().count(),
... "total_size": sub_dict.iter_values().sum(),
... "max_size": sub_dict.iter_values().max(),
... "files": sub_dict.iter_keys().sort().into(list),
... }
>>>
>>> pc.Dict(data_files).group_by_key_agg(
... key_func=lambda k: k.split(".")[-1], agg_func=get_stats
... ).sort().unwrap()
{'log': {'count': 2, 'total_size': 25, 'max_size': 20, 'files': ['file_b.log', 'file_d.log']}, 'txt': {'count': 2, 'total_size': 150, 'max_size': 100, 'files': ['file_a.txt', 'file_c.txt']}}
Source code in src/pyochain/_dict/_groups.py
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 | |
group_by_value ¶
group_by_value(func: Callable[[V], G]) -> Dict[G, dict[K, V]]
Group dict items into sub-dictionaries based on a function of the value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[V], G]
|
Function to determine the group for each value. |
required |
>>> import pyochain as pc
>>> d = {"a": 1, "b": 2, "c": 3, "d": 2}
>>> pc.Dict(d).group_by_value(lambda v: v % 2).unwrap()
{1: {'a': 1, 'c': 3}, 0: {'b': 2, 'd': 2}}
Source code in src/pyochain/_dict/_groups.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
group_by_value_agg ¶
group_by_value_agg(
value_func: Callable[[V], G], agg_func: Callable[[Dict[K, V]], R]
) -> Dict[G, R]
Group by value function, then apply aggregation function to each sub-dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value_func
|
Callable[[V], G]
|
Function to determine the group for each value. |
required |
agg_func
|
Callable[[Dict[K, V]], R]
|
Function to aggregate each sub-dictionary. |
required |
This avoids materializing intermediate Dict objects if you only need
an aggregated result for each group.
>>> import pyochain as pc
>>>
>>> data = {"math": "A", "physics": "B", "english": "A"}
>>> pc.Dict(data).group_by_value_agg(
... value_func=lambda grade: grade,
... agg_func=lambda d: d.iter_keys().count(),
... ).unwrap()
{'A': 2, 'B': 1}
>>>
>>> # --- Exemple 2: Agrégation plus complexe ---
>>> sales_data = {
... "store_1": "Electronics",
... "store_2": "Groceries",
... "store_3": "Electronics",
... "store_4": "Clothing",
... }
>>>
>>> # Obtain the first store for each category (after sorting store names)
>>> pc.Dict(sales_data).group_by_value_agg(
... value_func=lambda category: category,
... agg_func=lambda d: d.iter_keys().sort().first(),
... ).sort().unwrap()
{'Clothing': 'store_4', 'Electronics': 'store_1', 'Groceries': 'store_2'}
Source code in src/pyochain/_dict/_groups.py
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 | |
implode ¶
implode() -> Dict[K, list[V]]
Nest all the values in lists. syntactic sugar for map_values(lambda v: [v])
>>> import pyochain as pc
>>> pc.Dict({1: 2, 3: 4}).implode().unwrap()
{1: [2], 3: [4]}
Source code in src/pyochain/_dict/_main.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
inner_join ¶
inner_join(other: Mapping[K, W]) -> Dict[K, tuple[V, W]]
Performs an inner join with another mapping based on keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Mapping[K, W]
|
The mapping to join with. |
required |
Only keys present in both mappings are kept.
>>> import pyochain as pc
>>> d1 = {"a": 1, "b": 2}
>>> d2 = {"b": 10, "c": 20}
>>> pc.Dict(d1).inner_join(d2).unwrap()
{'b': (2, 10)}
Source code in src/pyochain/_dict/_joins.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
intersect_keys ¶
intersect_keys(*others: Mapping[K, V]) -> Dict[K, V]
Return a new Dict keeping only keys present in self and all others.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Mapping[K, V]
|
Other mappings to intersect keys with. |
()
|
>>> import pyochain as pc
>>> d1 = {"a": 1, "b": 2, "c": 3}
>>> d2 = {"b": 10, "c": 20}
>>> d3 = {"c": 30}
>>> pc.Dict(d1).intersect_keys(d2, d3).unwrap()
{'c': 3}
Source code in src/pyochain/_dict/_filters.py
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 | |
into ¶
into(func: Callable[Concatenate[T, P], R], *args: P.args, **kwargs: P.kwargs) -> R
Pass the unwrapped underlying data into a function.
The result is not wrapped.
>>> import pyochain as pc
>>> pc.Iter.from_(range(5)).into(list)
[0, 1, 2, 3, 4]
Source code in src/pyochain/_core/_main.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
invert ¶
invert() -> Dict[V, list[K]]
Invert the dictionary, grouping keys by common (and hashable) values.
>>> import pyochain as pc
>>> d = {"a": 1, "b": 2, "c": 1}
>>> pc.Dict(d).invert().unwrap()
{1: ['a', 'c'], 2: ['b']}
Source code in src/pyochain/_dict/_main.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
iter_items ¶
iter_items() -> Iter[tuple[K, V]]
Return a Iter of the dict's items.
>>> import pyochain as pc
>>> pc.Dict({1: 2}).iter_items().into(list)
[(1, 2)]
Source code in src/pyochain/_dict/_iter.py
80 81 82 83 84 85 86 87 88 89 90 91 92 | |
iter_keys ¶
iter_keys() -> Iter[K]
Return a Iter of the dict's keys.
>>> import pyochain as pc
>>> pc.Dict({1: 2}).iter_keys().into(list)
[1]
Source code in src/pyochain/_dict/_iter.py
52 53 54 55 56 57 58 59 60 61 62 63 64 | |
iter_values ¶
iter_values() -> Iter[V]
Return an Iter of the dict's values.
>>> import pyochain as pc
>>> pc.Dict({1: 2}).iter_values().into(list)
[2]
Source code in src/pyochain/_dict/_iter.py
66 67 68 69 70 71 72 73 74 75 76 77 78 | |
itr ¶
itr(
func: Callable[Concatenate[Iter[U], P], R], *args: P.args, **kwargs: P.kwargs
) -> Dict[K, R]
Apply a function to each value after wrapping it in an Iter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[Concatenate[Iter[U], P], R]
|
Function to apply to each value after wrapping it in an Iter. |
required |
*args
|
P.args
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the function. |
{}
|
Syntactic sugar for map_values(lambda data: func(Iter(data), *args, **kwargs))
>>> import pyochain as pc
>>> data = {
... "numbers1": [1, 2, 3],
... "numbers2": [4, 5, 6],
... }
>>> pc.Dict(data).itr(lambda v: v.repeat(5).flatten().sum()).unwrap()
{'numbers1': 30, 'numbers2': 75}
Source code in src/pyochain/_dict/_iter.py
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 | |
left_join ¶
left_join(other: Mapping[K, W]) -> Dict[K, tuple[V, W | None]]
Performs a left join with another mapping based on keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Mapping[K, W]
|
The mapping to join with. |
required |
All keys from the left dictionary (self) are kept.
>>> import pyochain as pc
>>> d1 = {"a": 1, "b": 2}
>>> d2 = {"b": 10, "c": 20}
>>> pc.Dict(d1).left_join(d2).unwrap()
{'a': (1, None), 'b': (2, 10)}
Source code in src/pyochain/_dict/_joins.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
map_items ¶
map_items(func: Callable[[tuple[K, V]], tuple[KR, VR]]) -> Dict[KR, VR]
Transform (key, value) pairs using a function that takes a (key, value) tuple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[tuple[K, V]], tuple[KR, VR]]
|
Function to transform each (key, value) pair into a new (key, value) tuple. |
required |
>>> import pyochain as pc
>>> pc.Dict({"Alice": 10, "Bob": 20}).map_items(
... lambda kv: (kv[0].upper(), kv[1] * 2)
... ).unwrap()
{'ALICE': 20, 'BOB': 40}
Source code in src/pyochain/_dict/_main.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
map_keys ¶
map_keys(func: Callable[[K], T]) -> Dict[T, V]
Return a Dict with keys transformed by func.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[K], T]
|
Function to apply to each key in the dictionary. |
required |
>>> import pyochain as pc
>>> pc.Dict({"Alice": [20, 15, 30], "Bob": [10, 35]}).map_keys(
... str.lower
... ).unwrap()
{'alice': [20, 15, 30], 'bob': [10, 35]}
>>>
>>> pc.Dict({1: "a"}).map_keys(str).unwrap()
{'1': 'a'}
Source code in src/pyochain/_dict/_main.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
map_kv ¶
map_kv(func: Callable[[K, V], tuple[KR, VR]]) -> Dict[KR, VR]
Transform (key, value) pairs using a function that takes key and value as separate arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[K, V], tuple[KR, VR]]
|
Function to transform each key and value into a new (key, value) tuple. |
required |
>>> import pyochain as pc
>>> pc.Dict({1: 2}).map_kv(lambda k, v: (k + 1, v * 10)).unwrap()
{2: 20}
Source code in src/pyochain/_dict/_main.py
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 | |
map_values ¶
map_values(func: Callable[[V], T]) -> Dict[K, T]
Return a Dict with values transformed by func.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[V], T]
|
Function to apply to each value in the dictionary. |
required |
>>> import pyochain as pc
>>> pc.Dict({"Alice": [20, 15, 30], "Bob": [10, 35]}).map_values(sum).unwrap()
{'Alice': 65, 'Bob': 45}
>>>
>>> pc.Dict({1: 1}).map_values(lambda v: v + 1).unwrap()
{1: 2}
Source code in src/pyochain/_dict/_main.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
merge ¶
merge(*others: Mapping[K, V]) -> Dict[K, V]
Merge other dicts into this one and return a new Dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Mapping[K, V]
|
One or more mappings to merge into the current dictionary. |
()
|
>>> import pyochain as pc
>>> pc.Dict({1: "one"}).merge({2: "two"}).unwrap()
{1: 'one', 2: 'two'}
>>> # Later dictionaries have precedence
>>> pc.Dict({1: 2, 3: 4}).merge({3: 3, 4: 4}).unwrap()
{1: 2, 3: 3, 4: 4}
Source code in src/pyochain/_dict/_joins.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
merge_with ¶
merge_with(*others: Mapping[K, V], func: Callable[[Iterable[V]], V]) -> Dict[K, V]
Merge dicts using a function to combine values for duplicate keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*others
|
Mapping[K, V]
|
One or more mappings to merge into the current dictionary. |
()
|
func
|
Callable[[Iterable[V]], V]
|
Function to combine values for duplicate keys. |
required |
A key may occur in more than one dict, and all values mapped from the key will be passed to the function as a list, such as func([val1, val2, ...]).
>>> import pyochain as pc
>>> pc.Dict({1: 1, 2: 2}).merge_with({1: 10, 2: 20}, func=sum).unwrap()
{1: 11, 2: 22}
>>> pc.Dict({1: 1, 2: 2}).merge_with({2: 20, 3: 30}, func=max).unwrap()
{1: 1, 2: 20, 3: 30}
Source code in src/pyochain/_dict/_joins.py
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 | |
pipe ¶
pipe(func: Callable[Concatenate[Self, P], R], *args: P.args, **kwargs: P.kwargs) -> R
Pipe the instance in the function and return the result.
Source code in src/pyochain/_core/_main.py
13 14 15 16 17 18 19 20 | |
pluck ¶
pluck(*keys: str) -> Dict[U, Any]
Extract values from nested dictionaries using a sequence of keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
str
|
Sequence of keys to extract values from the nested dictionaries. |
()
|
>>> import pyochain as pc
>>> data = {
... "person1": {"name": "Alice", "age": 30},
... "person2": {"name": "Bob", "age": 25},
... }
>>> pc.Dict(data).pluck("name").unwrap()
{'person1': 'Alice', 'person2': 'Bob'}
Source code in src/pyochain/_dict/_nested.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
println ¶
println(pretty: bool = True) -> Self
Print the underlying data and return self for chaining.
Useful for debugging, simply insert .println() in the chain,
and then removing it will not affect the rest of the chain.
Source code in src/pyochain/_core/_main.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
rename ¶
rename(mapping: Mapping[K, K]) -> Dict[K, V]
Return a new Dict with keys renamed according to the mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Mapping[K, K]
|
A dictionary mapping old keys to new keys. |
required |
Keys not in the mapping are kept as is.
>>> import pyochain as pc
>>> d = {"a": 1, "b": 2, "c": 3}
>>> mapping = {"b": "beta", "c": "gamma"}
>>> pc.Dict(d).rename(mapping).unwrap()
{'a': 1, 'beta': 2, 'gamma': 3}
Source code in src/pyochain/_dict/_process.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
schema ¶
schema(max_depth: int = 1) -> Dict[str, Any]
Return the schema of the dictionary up to a maximum depth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_depth
|
int
|
Maximum depth to inspect. Nested dicts beyond this depth are marked as 'dict'. |
1
|
When the max depth is reached, nested dicts are marked as 'dict'. For lists, only the first element is inspected.
>>> import pyochain as pc
>>> # Depth 2: we see up to level2
>>> data = {
... "level1": {"level2": {"level3": {"key": "value"}}},
... "other_key": 123,
... "list_key": [{"sub_key": "sub_value"}],
... }
>>> pc.Dict(data).schema(max_depth=1).unwrap()
{'level1': 'dict', 'other_key': 'int', 'list_key': 'list'}
>>> pc.Dict(data).schema(max_depth=2).unwrap()
{'level1': {'level2': 'dict'}, 'other_key': 'int', 'list_key': 'dict'}
>>>
>>> # Depth 3: we see up to level3
>>> pc.Dict(data).schema(max_depth=3).unwrap()
{'level1': {'level2': {'level3': 'dict'}}, 'other_key': 'int', 'list_key': {'sub_key': 'str'}}
Source code in src/pyochain/_dict/_nested.py
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 | |
select ¶
select(*exprs: IntoExpr) -> Dict[str, Any]
Select and alias fields from the dict based on expressions and/or strings.
Navigate nested fields using the pyochain.key function.
- Chain
key.key()calls to access nested fields. - Use
key.apply()to transform values. - Use
key.alias()to rename fields in the resulting dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exprs
|
IntoExpr
|
Expressions or strings to select and alias fields from the dictionary. |
()
|
>>> import pyochain as pc
>>> data = {
... "name": "Alice",
... "age": 30,
... "scores": {"eng": [85, 90, 95], "math": [80, 88, 92]},
... }
>>> scores_expr = pc.key("scores") # save an expression for reuse
>>> pc.Dict(data).select(
... pc.key("name").alias("student_name"),
... "age", # shorthand for pc.key("age")
... scores_expr.key("math").alias("math_scores"),
... scores_expr.key("eng")
... .apply(lambda v: pc.Seq(v).mean())
... .alias("average_eng_score"),
... ).unwrap()
{'student_name': 'Alice', 'age': 30, 'math_scores': [80, 88, 92], 'average_eng_score': 90}
Source code in src/pyochain/_dict/_main.py
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 | |
sort ¶
sort(reverse: bool = False) -> Dict[K, V]
Sort the dictionary by its keys and return a new Dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reverse
|
bool
|
Whether to sort in descending order. Defaults to False. |
False
|
>>> import pyochain as pc
>>> pc.Dict({"b": 2, "a": 1}).sort().unwrap()
{'a': 1, 'b': 2}
Source code in src/pyochain/_dict/_process.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
struct ¶
struct(
func: Callable[Concatenate[Dict[K, U], P], R], *args: P.args, **kwargs: P.kwargs
) -> Dict[K, R]
Apply a function to each value after wrapping it in a Dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[Concatenate[Dict[K, U], P], R]
|
Function to apply to each value after wrapping it in a Dict. |
required |
*args
|
P.args
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
P.kwargs
|
Keyword arguments to pass to the function. |
{}
|
Syntactic sugar for map_values(lambda data: func(pc.Dict(data), *args, **kwargs))
>>> import pyochain as pc
>>> data = {
... "person1": {"name": "Alice", "age": 30, "city": "New York"},
... "person2": {"name": "Bob", "age": 25, "city": "Los Angeles"},
... }
>>> pc.Dict(data).struct(lambda d: d.map_keys(str.upper).drop("AGE").unwrap())
... # doctest: +NORMALIZE_WHITESPACE
Dict({
'person1': {'NAME': 'Alice', 'CITY': 'New York'},
'person2': {'NAME': 'Bob', 'CITY': 'Los Angeles'}
})
Source code in src/pyochain/_dict/_nested.py
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 | |
unwrap ¶
unwrap() -> T
Return the underlying data.
This is a terminal operation.
Source code in src/pyochain/_core/_main.py
61 62 63 64 65 66 67 | |
update_in ¶
update_in(*keys: K, func: Callable[[V], V], default: V | None = None) -> Dict[K, V]
Update value in a (potentially) nested dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
K
|
Sequence of keys representing the nested path to update. |
()
|
func
|
Callable[[V], V]
|
Function to apply to the value at the specified path. |
required |
default
|
V | None
|
Default value to use if the path does not exist, by default None |
None
|
Applies the func to the value at the path specified by keys, returning a new Dict with the updated value.
If the path does not exist, it will be created with the default value (if provided) before applying func.
>>> import pyochain as pc
>>> inc = lambda x: x + 1
>>> pc.Dict({"a": 0}).update_in("a", func=inc).unwrap()
{'a': 1}
>>> transaction = {
... "name": "Alice",
... "purchase": {"items": ["Apple", "Orange"], "costs": [0.50, 1.25]},
... "credit card": "5555-1234-1234-1234",
... }
>>> pc.Dict(transaction).update_in("purchase", "costs", func=sum).unwrap()
{'name': 'Alice', 'purchase': {'items': ['Apple', 'Orange'], 'costs': 1.75}, 'credit card': '5555-1234-1234-1234'}
>>> # updating a value when k0 is not in d
>>> pc.Dict({}).update_in(1, 2, 3, func=str, default="bar").unwrap()
{1: {2: {3: 'bar'}}}
>>> pc.Dict({1: "foo"}).update_in(2, 3, 4, func=inc, default=0).unwrap()
{1: 'foo', 2: {3: {4: 1}}}
Source code in src/pyochain/_dict/_process.py
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 | |
with_fields ¶
with_fields(*exprs: IntoExpr) -> Dict[str, Any]
Merge aliased expressions into the root dict (overwrite on collision).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exprs
|
IntoExpr
|
Expressions to merge into the root dictionary. |
()
|
>>> import pyochain as pc
>>> data = {
... "name": "Alice",
... "age": 30,
... "scores": {"eng": [85, 90, 95], "math": [80, 88, 92]},
... }
>>> scores_expr = pc.key("scores") # save an expression for reuse
>>> pc.Dict(data).with_fields(
... scores_expr.key("eng")
... .apply(lambda v: pc.Seq(v).mean())
... .alias("average_eng_score"),
... ).unwrap()
{'name': 'Alice', 'age': 30, 'scores': {'eng': [85, 90, 95], 'math': [80, 88, 92]}, 'average_eng_score': 90}
Source code in src/pyochain/_dict/_main.py
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 | |
with_key ¶
with_key(key: K, value: V) -> Dict[K, V]
Return a new Dict with key set to value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
K
|
Key to set in the dictionary. |
required |
value
|
V
|
Value to associate with the specified key. |
required |
Does not modify the initial dictionary.
>>> import pyochain as pc
>>> pc.Dict({"x": 1}).with_key("x", 2).unwrap()
{'x': 2}
>>> pc.Dict({"x": 1}).with_key("y", 3).unwrap()
{'x': 1, 'y': 3}
>>> pc.Dict({}).with_key("x", 1).unwrap()
{'x': 1}
Source code in src/pyochain/_dict/_process.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
with_nested_key ¶
with_nested_key(*keys: K, value: V) -> Dict[K, V]
Set a nested key path and return a new Dict with new, potentially nested, key value pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
K
|
Sequence of keys representing the nested path. |
()
|
value
|
V
|
Value to set at the specified nested path. |
required |
>>> import pyochain as pc
>>> purchase = {
... "name": "Alice",
... "order": {"items": ["Apple", "Orange"], "costs": [0.50, 1.25]},
... "credit card": "5555-1234-1234-1234",
... }
>>> pc.Dict(purchase).with_nested_key(
... "order", "costs", value=[0.25, 1.00]
... ).unwrap()
{'name': 'Alice', 'order': {'items': ['Apple', 'Orange'], 'costs': [0.25, 1.0]}, 'credit card': '5555-1234-1234-1234'}
Source code in src/pyochain/_dict/_nested.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |