ddp_utils.strings¶
import ddp_utils.strings
Punctuation, quote and ANSI removal, plus recursive character replacement.
Examples
Use this public operation:
import ddp_utils.strings
- ddp_utils.strings.strip_punctuation(text)¶
Remove ASCII punctuation listed in string.punctuation, including underscore.
Parameters
Name
Description
text
Input string; spaces and non-ASCII punctuation are preserved.
Returns
New string, or an empty string for empty input.
Examples
Use this public operation:
result = ddp_utils.strings.strip_punctuation(text=text_value)
- ddp_utils.strings.strip_quotes(s)¶
Strip whitespace and one matching pair of single or double quotes.
Non-None input is converted with str(). Mismatched quotes are retained. A single quote character by itself also becomes an empty string.
Parameters
Name
Description
s
Any value convertible to str, or None.
Returns
Stripped string, or None when the input is None.
Examples
Remove matching outer quotes while preserving unquoted input:
from ddp_utils.strings import strip_quotes assert strip_quotes('"hello"') == "hello" assert strip_quotes("unquoted") == "unquoted" assert strip_quotes(None) is None
- ddp_utils.strings.strip_ansi_codes(text: str) str¶
Remove ANSI sequences matched by the module’s escape-sequence regex.
Useful for comparing colored console output or writing plain-text logs. This is not a full terminal-control parser or a general text sanitizer.
Parameters
Name
Type
Description
text
str
String possibly containing terminal color/style escape sequences.
Returns
Type
Description
str
String with matched escapes removed; other text is unchanged.
Examples
Remove terminal color sequences from a message:
from ddp_utils.strings import strip_ansi_codes message = strip_ansi_codes("[31merror[0m") assert message == "error"
- ddp_utils.strings.replace_chars(d: str | List | Dict | Tuple, addon: Dict[str, str] = None) str | List | Dict | Tuple¶
Recursively replace special characters in strings.
Default replacements are
& > < " ' ’.Parameters
Name
Type
Description
d
str | List | Dict | Tuple
Input value (str / list / dict / tuple). Strings inside will be transformed.
addon
Dict[str, str]
Extra str.translate replacements; matching keys override defaults. Keys must be single characters or integer Unicode ordinals. None and an empty mapping keep defaults. Multi-character keys raise ValueError.
Returns
Type
Description
str | List | Dict | Tuple
New dict/list/tuple containers with recursively transformed values. Dictionary keys are unchanged. Other objects pass through unchanged. Replacement text is not recursively escaped within a single call.
Examples
Use this public operation:
result = ddp_utils.strings.replace_chars(d=d_value)
- ddp_utils.strings.replace_punctuation_and_special_chars(text: str, pattern: str = '') str¶
Remove punctuation and special characters from a string.
Keeps letters/digits/underscore. Everything else is replaced with
pattern.Parameters
Name
Type
Description
text
str
Input string.
pattern
str
re.sub replacement string for each consecutive run of non-word characters, not one replacement per character. Defaults to deletion.
Returns
Type
Description
str
Cleaned string.
Examples
Use this public operation:
result = ddp_utils.strings.replace_punctuation_and_special_chars(text=text_value)