From db1a1aa4dce30639227cf0aedb5e539cf9ab0b4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Fri, 19 Jun 2020 13:34:25 +0200 Subject: [PATCH] added stringify_dict --- CHANGELOG.md | 1 + docs/coding/transforms.rst | 8 ++++++++ docs/index.rst | 1 + satella/__init__.py | 2 +- satella/coding/transforms/__init__.py | 7 +++++++ tests/test_coding/test_transforms.py | 8 ++++++++ 6 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 docs/coding/transforms.rst create mode 100644 satella/coding/transforms/__init__.py create mode 100644 tests/test_coding/test_transforms.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 659be69d..340e41c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,3 @@ # v2.8.4 +* added `stringify_dict` diff --git a/docs/coding/transforms.rst b/docs/coding/transforms.rst new file mode 100644 index 00000000..6350c394 --- /dev/null +++ b/docs/coding/transforms.rst @@ -0,0 +1,8 @@ +Rudimentary data transforms. + +stringify_dict +============== + +Make both keys and values of this dict a string, by passing them through stringify function. + +.. autofunction:: satella.coding.transforms.stringify_dict diff --git a/docs/index.rst b/docs/index.rst index 28eff3ed..7e5d5ad8 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -16,6 +16,7 @@ Visit the project's page at GitHub_! coding/structures coding/concurrent coding/sequences + coding/transforms instrumentation/traceback instrumentation/memory instrumentation/metrics diff --git a/satella/__init__.py b/satella/__init__.py index fc33c922..ef59cc95 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.8.4_a2' +__version__ = '2.8.4_a3' diff --git a/satella/coding/transforms/__init__.py b/satella/coding/transforms/__init__.py new file mode 100644 index 00000000..88325817 --- /dev/null +++ b/satella/coding/transforms/__init__.py @@ -0,0 +1,7 @@ +import typing as tp + +__all__ = ['stringify_dict'] + + +def stringify_dict(dct: dict, stringify: tp.Callable[[tp.Any], str]) -> tp.Dict[str, str]: + return {stringify(k): stringify(v) for k, v in dct.items()} diff --git a/tests/test_coding/test_transforms.py b/tests/test_coding/test_transforms.py new file mode 100644 index 00000000..11b098cb --- /dev/null +++ b/tests/test_coding/test_transforms.py @@ -0,0 +1,8 @@ +import unittest +from satella.coding.transforms import stringify_dict + + +class MyTestCase(unittest.TestCase): + def test_stringify_dict(self): + dct = {1: 2, 3: 4, 5: 6} + self.assertEqual(stringify_dict(dct), {'1': '2', '3': '4', '5': '6'}) -- GitLab