os_helper.string_utils module
String Utilities
This module provides helper functions for handling and manipulating strings, including checks for empty strings, converting strings to ASCII-safe formats, and converting between string representations and numerical time values.
- Author:
Warith HARCHAOUI, https://linkedin.com/in/warith-harchaoui
- os_helper.string_utils.asciistring(input_string, replacement_char='-', lower=True, allow_digits=True)[source]
Convert a given string into a “safe” ASCII string by replacing accented and non-ASCII characters.
Non-ASCII characters that cannot be converted will be replaced with a specified character.
- Parameters:
input_string (str) – The input string to be converted.
replacement_char (str, optional) – The character to replace non-ASCII or unwanted characters with. Defaults to ‘-‘.
lower (bool, optional) – Whether to convert the string to lowercase. Defaults to True.
allow_digits (bool, optional) – Whether to allow digits in the resulting string. Defaults to True.
- Returns:
A “safe” ASCII string with unwanted characters replaced and case adjusted.
- Return type:
Examples
>>> asciistring("MyFile@2024.txt") 'myfile-2024-txt'
>>> asciistring("Café-Con-Leche!", replacement_char="_") 'cafe_con_leche'
>>> asciistring("Special#File$2024", lower=False) 'Special-File-2024'
- os_helper.string_utils.emptystring(s)[source]
Return True if
sis None, not a string, or only whitespace.Convenient for input validation where
"",Noneand" "should all be treated the same way.- Parameters:
s (Optional[str]) – The value to check.
- Returns:
True when
sis None or contains only whitespace; False otherwise.- Return type:
Examples
>>> emptystring("") True >>> emptystring(" ") True >>> emptystring(None) True >>> emptystring("hello") False