From 22c50b53b79d9b30e74eb852209405180db34fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <pmaslanka@smok.co> Date: Thu, 14 Oct 2021 22:26:40 +0200 Subject: [PATCH] added map_through --- CHANGELOG.md | 2 ++ satella/__init__.py | 2 +- satella/coding/transforms/__init__.py | 7 +++++-- tests/test_coding/test_transforms.py | 5 +++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1556c2d4..edc031fa 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 f4f211ad..f9ba0d3c 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 76c35cb3..1ff929d3 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 93d7d2b1..4c0804aa 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})) -- GitLab