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

fix stringify

parent 60cceab4
No related branches found
No related tags found
No related merge requests found
# v2.8.6 # v2.8.6
* added `time_us` * added `time_us`
* updated `stringify` to correctly handle None-cases
__version__ = '2.8.6_a2' __version__ = '2.8.6_a3'
...@@ -5,7 +5,8 @@ __all__ = ['stringify'] ...@@ -5,7 +5,8 @@ __all__ = ['stringify']
def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = str, def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = str,
recursively: bool = False) -> tp.Dict[str, str]: recursively: bool = False,
str_none: bool = False) -> tp.Dict[str, str]:
""" """
Stringify all object: Stringify all object:
...@@ -19,6 +20,7 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s ...@@ -19,6 +20,7 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s
:param obj: a list or a dict :param obj: a list or a dict
:param stringifier: function that accepts any arguments and returns a string representation :param stringifier: function that accepts any arguments and returns a string representation
:param recursively: whether to recursively stringify elements, ie. stringify will be called on all the children :param recursively: whether to recursively stringify elements, ie. stringify will be called on all the children
:param str_none: whether to return None if given a None. If True, "None" will be returned instead
:return: stringified object :return: stringified object
""" """
if isinstance(obj, collections.abc.Mapping): if isinstance(obj, collections.abc.Mapping):
...@@ -27,5 +29,10 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s ...@@ -27,5 +29,10 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s
elif isinstance(obj, collections.abc.Sequence): elif isinstance(obj, collections.abc.Sequence):
make_str = (lambda obj2: stringify(obj2, stringifier, True)) if recursively else stringifier make_str = (lambda obj2: stringify(obj2, stringifier, True)) if recursively else stringifier
return [make_str(v) for v in obj] return [make_str(v) for v in obj]
elif obj is None:
if str_none:
return stringifier(None)
else:
return None
else: else:
return stringifier(obj) return stringifier(obj)
...@@ -16,3 +16,6 @@ class MyTestCase(unittest.TestCase): ...@@ -16,3 +16,6 @@ class MyTestCase(unittest.TestCase):
dct2 = {1: [1, 2, 3]} dct2 = {1: [1, 2, 3]}
self.assertEqual(stringify(dct2, recursively=True), {'1': ['1', '2', '3']}) self.assertEqual(stringify(dct2, recursively=True), {'1': ['1', '2', '3']})
self.assertIsNone(stringify(None))
self.assertEqual(stringify(None, str_none=True), str(None))
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