diff --git a/CHANGELOG.md b/CHANGELOG.md index 1556c2d403259addbdce666d82eb88b0d4a13e55..edc031fa08f555bf3b0dcff7531103efdc48ed9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,3 @@ # v2.18.4 + +* added `map_through` to `unpack_dict` diff --git a/satella/__init__.py b/satella/__init__.py index f4f211ad5eb337c4bb5b1e60cb33a0f3674b1c72..f9ba0d3c5b94f2c7863d6a95392d13104eba039b 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.18.4a1' +__version__ = '2.18.4a2' diff --git a/satella/coding/transforms/__init__.py b/satella/coding/transforms/__init__.py index 76c35cb3091de4e5380176399c5cabe87ad4cf77..1ff929d326fe600559b819f69885fdd6d17a462a 100644 --- a/satella/coding/transforms/__init__.py +++ b/satella/coding/transforms/__init__.py @@ -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 -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. @@ -32,11 +33,13 @@ def unpack_dict(dct: tp.Dict[K, V], *args: K) -> tp.Iterator[V]: :param dct: dictionary to unpack :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 :raises KeyError: a key was not found """ for key in args: - yield dct[key] + yield map_through(dct[key]) def none_if_false(y: tp.Any) -> tp.Optional[tp.Any]: diff --git a/tests/test_coding/test_transforms.py b/tests/test_coding/test_transforms.py index 93d7d2b170560612f485e38b134fadb4afb8eda2..4c0804aac4693a69ea5a19f93870063ef4252dd6 100644 --- a/tests/test_coding/test_transforms.py +++ b/tests/test_coding/test_transforms.py @@ -3,6 +3,7 @@ import operator import unittest import base64 +from satella.coding.predicates import x from satella.coding.transforms import stringify, split_shuffle_and_join, one_tuple, \ merge_series, pad_to_multiple_of_length, clip, b64encode, linear_interpolate, \ hashables_to_int, none_if_false, merge_list, is_subset, unpack_dict @@ -14,6 +15,10 @@ class TestTransforms(unittest.TestCase): a, b, c = unpack_dict({1: 2, 2: 3, 4: 5}, 1, 2, 4) 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): self.assertTrue(is_subset({}, {})) self.assertTrue(is_subset({}, {1: 2}))