diff options
| author | Scott Gasch <[email protected]> | 2021-04-13 13:55:12 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2021-04-13 13:55:12 -0700 |
| commit | dab5654d392f69fb00bed49cf8ffb80f37642ea5 (patch) | |
| tree | bfba22336306b512e6971c2815a1ec4c479f7297 /string_utils.py | |
| parent | 64a9a97fdff29f4bb9eef4e80faaeaa520d59506 (diff) | |
Various sundry changes.
Diffstat (limited to 'string_utils.py')
| -rw-r--r-- | string_utils.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/string_utils.py b/string_utils.py index b586ae1..83575ff 100644 --- a/string_utils.py +++ b/string_utils.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +from itertools import zip_longest import json import random import re @@ -220,6 +221,33 @@ def strip_escape_sequences(in_str: str) -> str: return in_str +def add_thousands_separator(in_str: str, *, separator_char = ',', places = 3) -> str: + if isinstance(in_str, int): + in_str = f'{in_str}' + + if is_number(in_str): + return _add_thousands_separator( + in_str, + separator_char = separator_char, + places = places + ) + raise ValueError(in_str) + + +def _add_thousands_separator(in_str: str, *, separator_char = ',', places = 3) -> str: + decimal_part = "" + if '.' in in_str: + (in_str, decimal_part) = in_str.split('.') + tmp = [iter(in_str[::-1])] * places + ret = separator_char.join( + "".join(x) for x in zip_longest(*tmp, fillvalue=""))[::-1] + if len(decimal_part) > 0: + ret += '.' + ret += decimal_part + return ret + + + # Full url example: # scheme://username:[email protected]:8042/folder/subfolder/file.extension?param=value¶m2=value2#hash def is_url(in_str: Any, allowed_schemes: Optional[List[str]] = None) -> bool: |
