Add string handling helper maks to mask strings

This commit is contained in:
Clemens Schwaighofer
2025-07-03 13:57:39 +09:00
parent f900a6eab9
commit 1eb464dd2c
2 changed files with 33 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
# MARK: Project info
[project]
name = "corelibs"
version = "0.4.0"
version = "0.5.0"
description = "Collection of utils for Python scripts"
readme = "ReadMe.md"
requires-python = ">=3.13"

View File

@@ -83,4 +83,36 @@ def format_number(number: float, precision: int = 0) -> str:
"f}"
).format(number)
def mask(
data_set: dict[str, str],
mask_keys: list[str] | None = None,
mask_str: str = "***",
skip: bool = False
) -> dict[str, str]:
"""
mask data for output
Checks if mask_keys list exist in any key in the data set either from the start or at the end
Arguments:
data_set {dict[str, str]} -- _description_
Keyword Arguments:
mask_keys {list[str] | None} -- _description_ (default: {None})
mask_str {str} -- _description_ (default: {"***"})
skip {bool} -- _description_ (default: {False})
Returns:
dict[str, str] -- _description_
"""
if skip is True:
return data_set
if mask_keys is None:
mask_keys = ["password", "secret"]
return {
key: mask_str
if any(key.startswith(mask_key) or key.endswith(mask_key) for mask_key in mask_keys) else value
for key, value in data_set.items()
}
# __END__