### ===================================================================================================================
### Change strings to be used in/as filenames to not include illegal characters
### ===================================================================================================================
# Copyright ©VIIA 2024
### ===================================================================================================================
### 1. Import modules
### ===================================================================================================================
# No imports required
### ===================================================================================================================
### 2. Function to convert strings to filename format
### ===================================================================================================================
[docs]def viia_to_filename(string: str) -> str:
"""
This function replaces any characters from a string that can prevent a file from being saved in Windows.
Input:
- string (str): String to be modified into a Windows filename format.
Output:
- Returns string that adheres to Windows filename formats.
"""
# Check for unexpected illegal characters
for ch in '|"*:?\\':
if ch in string:
raise AssertionError(
f"ERROR: Unexpected illegal character '{ch}' is present in string '{string}' to be used in/as "
f"filename.")
# Replace expected illegal characters
filename = string.replace('>', '-g-').replace('<', '-l-').replace('/', '')
return filename
### ===================================================================================================================
### 3. End of script
### ===================================================================================================================