lkj.iterables#
Tools with iterables (dicts, lists, tuples, sets, etc.).
Functions
|
Compares two iterables and returns a named tuple with: |
|
Get a dictionary from a list of dictionaries by a field value. |
|
List list.index but for any iterable. |
Classes
|
- class lkj.iterables.SetsComparisonResult(common, left_only, right_only)[source]#
Bases:
NamedTuple
- lkj.iterables.compare_sets(left, right)[source]#
Compares two iterables and returns a named tuple with:
Elements in both iterables.
Elements only in the left iterable.
Elements only in the right iterable.
Note
When applied to dicts, the comparison is done on the keys. If you want a comparison on the values, use
compare_iterables(left.values(), right.values()).- Parameters:
- Returns:
A namedtuple with fields
common,left_only, andright_only.- Return type:
Examples
>>> left = ['a', 'b', 'c'] >>> right = ['b', 'c', 'd'] >>> result = compare_sets(left, right) >>> assert result.common == {'b', 'c'} # asserting because order is not guaranteed >>> result.left_only {'a'} >>> result.right_only {'d'}
- lkj.iterables.get_by_value(list_of_dicts, value, field)[source]#
Get a dictionary from a list of dictionaries by a field value.
>>> data = [{'id': 1, 'value': 'A'}, {'id': 2, 'value': 'B'}] >>> get_by_value(data, 2, 'id') {'id': 2, 'value': 'B'}
This function just WANTS to be
functools.partial-ized!!>>> from functools import partial >>> get_by_id = partial(get_by_value, field='id') >>> get_by_id(data, 1) {'id': 1, 'value': 'A'} >>> get_value_of_B = partial(get_by_value, value='B', field='value') >>> get_value_of_B(data) {'id': 2, 'value': 'B'}