dictionary
Convenient partials of functions for manipulating dictionaries.
Parameters that are known at program start are used to initialize the classes so that, at runtime, dictionaries can flow through a preconfigured processing pipe of callable objects.
- class ValuesGetter(key=(), *keys, wrapper=<class 'list'>)[source]
Bases:
ArgRepr
,Generic
Extract a sequence of dictionary values according to the given keys.
Instances behave like sequences of these keys and can be interacted with accortdingly (e.g., one can index, slice, or reverse instances).
- Parameters:
key (Hashable, optional) – Dictionary key or iterable thereof. Defaults to an empy tuple.
*keys (Hashable) – Additional dictionary keys.
wrapper (callable, optional) – A type or some other callable that transforms a tuple of dictionary values into some other type of sequence. The type of the sequence should be annotated. Instances can be type annotated with the type of sequence to be returned. Defaults to
list
.
- Raises:
TypeError – If (any of) key or any of keys are not, in fact, hashable.
Note
Generic type annotation with the (return) type of wrapper is recommended.
Examples
>>> get_values = ValuesGetter('name', 'age') >>> get_values({'name': 'John', 'weight': 84, 'age': 42}) ['John', 42]
- __call__(mapping)[source]
Extract sequence of dictionary values for given keys.
- Parameters:
mapping (Mapping) – Dictionary to extract values from.
- Returns:
The values extracted from the dictionary. The type of sequence is determined by the return type of wrap. Defaults to
list
.- Return type:
Sequence