ops¶
flat_mapping_items¶
-
flat_mapping_items
(mapping, delimiter='.', _parent_key='')¶ yield an iterable of compressed keys and values from a possibly nested mapping.
- Parameters
mapping (
Mapping
) – a possibly nested mappingdelimiter (
str
) – string delimiter to use for compressing keys. Defaults to ‘.’.
- Yields
Iterable of compressed key, value pairs
- Return type
Iterable
[Tuple
[str
,Any
]]
flatten_mapping¶
-
flatten_mapping
(mapping, delimiter='.', keep_type=False)¶ flatten a dictionary or other mapping by compressing keys using
delimiter
.Note
In the be careful with using
keep_type
and a “.” delimiter with ancollectionish.AttyDict
, as attydict keys need to be valid python names and therefore should not contain a dot. This function will raise a value error and tell you off if you try that.- Parameters
mapping (
Mapping
) – a possibly nested mappingdelimiter (
str
) – string delimiter to use for compressing keys. Defaults to ‘.’. Defaults to ‘.’.keep_type (
bool
) – IfTrue
attempt to ensure returned mapping is same type as the input mapping otherwise just return a dict. Defaults to False.
Example
>>> from collectionish.ops import flatten_mapping >>> nested_dict = {'day': 1, ... 'apples': {'eaten': 2, 'left': 4, 'color': {'red':3, 'green': 1}}, ... 'sausages': {'eaten': 1, 'left': 3, 'gone_off':1}} >>> >>> print(flatten_mapping(nested_dict, delimiter='.')) {'day': 1, 'apples.eaten': 2, 'apples.left': 4, 'apples.color.red': 3, 'apples.color.green': 1, 'sausages.eaten': 1, 'sausages.left': 3, 'sausages.gone_off': 1}
rgetattr¶
-
rgetattr
(obj, *keys)¶ A recursive version of
getattr()
.Example
Here’s a simple example using
types.SimpleNamespace
as a standin for anything with attibute access:>>> from types import SimpleNamespace >>> from collectionish.ops import rgetattr >>> >>> thing = SimpleNamespace(a=1, b=SimpleNamespace(ba=1, bb=SimpleNamespace(bba=1, bbb=2)))
with only one key it works like normal
getattr()
:>>> rgetattr(thing, 'a') 1 >>> rgetattr(thing, 'b') namespace(ba=1, bb=namespace(bba=1, bbb=2))
with multiple
rgetattr
gathers the attibute from a nested object:>>> rgetattr(thing, 'b', 'ba') 1 >>> rgetattr(thing, 'b', 'bb', 'bbb') 2
rgetitem¶
-
rgetitem
(obj, *keys)¶ A recursive version of
__getitem__()
.Example
>>> from collectionish.ops import rgetitem >>> >>> thing = {'a': 1, 'b': {'ba': 1, 'bb': 2}}
with only one key it works like normal
__getitem__()
:>>> rgetitem(thing, 'a') 1
with multiple
rgetattr
gets items recursively:>>> rgetitem(thing, 'b', 'bb') 2
works fine with lists and stuff as well:
>>> nested_list = [1, [1, 2, [1, 2, 3]], 2] >>> rgetitem(nested_list, -1) 2 >>> rgetitem(nested_list, 1, 2, 2) 3
rsetattr¶
-
rsetattr
(obj, keys, value)¶ A recursive version of
setattr()
.Example
Again we’ll use
types.SimpleNamespace
as a standin for anything with attibute access:>>> from types import SimpleNamespace >>> from collectionish.ops import rgetattr >>> >>> thing = SimpleNamespace()
with only one key it works like normal
setattr()
:>>> rsetattr(thing, ['a'], 1) >>> thing namespace(a=1) >>> rsetattr(thing, ['b'], SimpleNamespace(ba= 1, bb= 2)) >>> thing namespace(a=1, b=namespace(ba=1, bb=2))
with multiple
rsetattr
can update a nested value:>>> rsetattr(thing, ('b', 'ba'), 2) >>> thing namespace(a=1, b=namespace(ba=2, bb=2))
rsetitem¶
-
rsetitem
(obj, keys, value)¶ A recursive version of
__setitem__()
.Example
>>> from collectionish.ops import rgetitem >>> >>> thing = {'a': 1, 'b': {'ba': 1, 'bb': 2}}
with only one key it works like normal
__setitem__()
:>>> rsetitem(thing, ('a',), 2) >>> thing {'a': 2, 'b': {'ba': 1, 'bb': 2}}
with multiple
rgetattr
gets items recursively:>>> rsetitem(thing, ['b', 'bb'], 4) >>> thing {'a': 2, 'b': {'ba': 1, 'bb': 4}}
lists and stuff work as expected:
>>> nested_list = [1, [1, 2, [1, 2, 3]], 2] >>> rsetitem(nested_list, [-1], 3) >>> nested_list [1, [1, 2, [1, 2, 3]], 3] >>> rsetitem(nested_list, [1, 2, 2], 5) >>> nested_list [1, [1, 2, [1, 2, 5]], 3]
- Return type
None