Skip to content
Snippets Groups Projects
Commit 22c50b53 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added map_through

parent 26779d08
No related branches found
No related tags found
No related merge requests found
# v2.18.4 # v2.18.4
* added `map_through` to `unpack_dict`
__version__ = '2.18.4a1' __version__ = '2.18.4a2'
...@@ -21,7 +21,8 @@ __all__ = ['stringify', 'split_shuffle_and_join', 'one_tuple', 'none_if_false', ...@@ -21,7 +21,8 @@ __all__ = ['stringify', 'split_shuffle_and_join', 'one_tuple', 'none_if_false',
from satella.coding.typing import T, NoArgCallable, Appendable, Number, Predicate, K, V from satella.coding.typing import T, NoArgCallable, Appendable, Number, Predicate, K, V
def unpack_dict(dct: tp.Dict[K, V], *args: K) -> tp.Iterator[V]: def unpack_dict(dct: tp.Dict[K, V], *args: K,
map_through: tp.Callable[[V], V] = lambda y: y) -> tp.Iterator[V]:
""" """
Unpack a dictionary by accessing it's given keys in parallel. Unpack a dictionary by accessing it's given keys in parallel.
...@@ -32,11 +33,13 @@ def unpack_dict(dct: tp.Dict[K, V], *args: K) -> tp.Iterator[V]: ...@@ -32,11 +33,13 @@ def unpack_dict(dct: tp.Dict[K, V], *args: K) -> tp.Iterator[V]:
:param dct: dictionary to unpack :param dct: dictionary to unpack
:param args: keys in this dictionary :param args: keys in this dictionary
:param map_through: a keyword argument, callable that will be called with
each value returned and the result of this callable will be returned
:return: an iterator :return: an iterator
:raises KeyError: a key was not found :raises KeyError: a key was not found
""" """
for key in args: for key in args:
yield dct[key] yield map_through(dct[key])
def none_if_false(y: tp.Any) -> tp.Optional[tp.Any]: def none_if_false(y: tp.Any) -> tp.Optional[tp.Any]:
......
...@@ -3,6 +3,7 @@ import operator ...@@ -3,6 +3,7 @@ import operator
import unittest import unittest
import base64 import base64
from satella.coding.predicates import x
from satella.coding.transforms import stringify, split_shuffle_and_join, one_tuple, \ from satella.coding.transforms import stringify, split_shuffle_and_join, one_tuple, \
merge_series, pad_to_multiple_of_length, clip, b64encode, linear_interpolate, \ merge_series, pad_to_multiple_of_length, clip, b64encode, linear_interpolate, \
hashables_to_int, none_if_false, merge_list, is_subset, unpack_dict hashables_to_int, none_if_false, merge_list, is_subset, unpack_dict
...@@ -14,6 +15,10 @@ class TestTransforms(unittest.TestCase): ...@@ -14,6 +15,10 @@ class TestTransforms(unittest.TestCase):
a, b, c = unpack_dict({1: 2, 2: 3, 4: 5}, 1, 2, 4) a, b, c = unpack_dict({1: 2, 2: 3, 4: 5}, 1, 2, 4)
self.assertTrue(a == 2 and b == 3 and c == 5) self.assertTrue(a == 2 and b == 3 and c == 5)
a, b, c = unpack_dict({1: 2, 2: 3, 4: 5}, 1, 2, 4,
map_through=x*2)
self.assertTrue(a == 4 and b == 6 and c == 10)
def test_is_subset(self): def test_is_subset(self):
self.assertTrue(is_subset({}, {})) self.assertTrue(is_subset({}, {}))
self.assertTrue(is_subset({}, {1: 2})) self.assertTrue(is_subset({}, {1: 2}))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment