Avoid directory writes when files are already there.
[python_utils.git] / string_utils.py
index b586ae1a7e82d62e92ba567b20e5a440254fe8b3..83575ff47ce878a93f5237565e066abac57a0b1a 100644 (file)
@@ -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&param2=value2#hash
 def is_url(in_str: Any, allowed_schemes: Optional[List[str]] = None) -> bool: