diff options
| author | Scott <[email protected]> | 2022-01-10 21:51:30 -0800 |
|---|---|---|
| committer | Scott <[email protected]> | 2022-01-10 21:51:30 -0800 |
| commit | f2c7c1a131d8846c15613125b6ed999724f0ab2f (patch) | |
| tree | a1c870532e9cac8ffe43adde60de1c72861437af /string_utils.py | |
| parent | d1fdbad2a4b96a374fdd23e8c8550484a9e6523a (diff) | |
Add multi replace.
Diffstat (limited to 'string_utils.py')
| -rw-r--r-- | string_utils.py | 19 |
1 files 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() |
