From f2c7c1a131d8846c15613125b6ed999724f0ab2f Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 10 Jan 2022 21:51:30 -0800 Subject: [PATCH] Add multi replace. --- string_utils.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/string_utils.py b/string_utils.py index 097dc1b..45607f3 100644 --- a/string_utils.py +++ b/string_utils.py @@ -861,16 +861,16 @@ def words_count(in_str: str) -> int: return len(WORDS_COUNT_RE.findall(in_str)) -def generate_uuid(as_hex: bool = False) -> str: +def generate_uuid(omit_dashes: bool = False) -> str: """ Generated an UUID string (using `uuid.uuid4()`). generate_uuid() # possible output: '97e3a716-6b33-4ab9-9bb1-8128cb24d76b' - generate_uuid(as_hex=True) # possible output: '97e3a7166b334ab99bb18128cb24d76b' + generate_uuid(omit_dashes=True) # possible output: '97e3a7166b334ab99bb18128cb24d76b' """ uid = uuid4() - if as_hex: + if omit_dashes: return uid.hex return str(uid) @@ -1553,6 +1553,19 @@ def path_ancestors_before_descendants_sort_key(volume: str) -> Tuple[str]: return tuple([x for x in volume.split('/') if len(x) > 0]) +def replace_all(in_str: str, replace_set: str, replacement: str) -> str: + """Execute several replace operations in a row. + + >>> s = 'this_is a-test!' + >>> replace_all(s, ' _-!', '') + 'thisisatest' + + """ + for char in replace_set: + in_str = in_str.replace(char, replacement) + return in_str + + if __name__ == '__main__': import doctest doctest.testmod() -- 2.46.0