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

fixed stringify, v2.8.4

parent 7ab46380
No related branches found
No related tags found
No related merge requests found
__version__ = '2.8.4_a3' __version__ = '2.8.4'
...@@ -11,7 +11,10 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s ...@@ -11,7 +11,10 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s
ie. if a dict, put every item and key (if a dict is given) through stringify. ie. if a dict, put every item and key (if a dict is given) through stringify.
if a list, put every item through stringify if a list, put every item through stringify
else just call stringify on it else just call stringify on it.
Note that if you use recursively, then dicts and lists are allowed to be valid elements of the returned
representation!
: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
...@@ -19,13 +22,10 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s ...@@ -19,13 +22,10 @@ def stringify(obj: tp.Union[tp.Any], stringifier: tp.Callable[[tp.Any], str] = s
:return: stringified object :return: stringified object
""" """
if isinstance(obj, collections.abc.Mapping): if isinstance(obj, collections.abc.Mapping):
if recursively: make_str = (lambda obj2: stringify(obj2, stringifier, True)) if recursively else stringifier
stringifier = lambda obj2: stringify(obj2, stringifier=stringifier, recursively=True) return {make_str(k): make_str(v) for k, v in obj.items()}
return {stringifier(k): stringifier(v) for k, v in obj.items()}
elif isinstance(obj, collections.abc.Sequence): elif isinstance(obj, collections.abc.Sequence):
if recursively: make_str = (lambda obj2: stringify(obj2, stringifier, True)) if recursively else stringifier
stringifier = lambda obj2: stringify(obj2, stringifier=stringifier, recursively=True) return [make_str(v) for v in obj]
return [stringifier(v) for v in obj]
else: else:
return stringifier(obj) return stringifier(obj)
...@@ -13,3 +13,6 @@ class MyTestCase(unittest.TestCase): ...@@ -13,3 +13,6 @@ class MyTestCase(unittest.TestCase):
lst2 = [[1, 2, 3], 3, 4, 5] lst2 = [[1, 2, 3], 3, 4, 5]
self.assertEqual(stringify(lst2), ['[1, 2, 3]', '3', '4', '5']) self.assertEqual(stringify(lst2), ['[1, 2, 3]', '3', '4', '5'])
self.assertEqual(stringify(lst2, recursively=True), [['1', '2', '3'], '3', '4', '5']) self.assertEqual(stringify(lst2, recursively=True), [['1', '2', '3'], '3', '4', '5'])
dct2 = {1: [1, 2, 3]}
self.assertEqual(stringify(dct2, recursively=True), {'1': ['1', '2', '3']})
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