"""Collection of utilities realted to data and dataloaders."""
# standard library packages
from os.path import exists
from typing import Tuple, List, Union
[docs]def check_and_standardize_path(path: str, folder: bool = True):
"""Check if path exists and remove trailing slash if present.
:param path: path to folder or file
:type path: str
:param folder: True if path points to folder; False if path points to file
:type folder: bool
:return: standardized path to file or folder
:rtype: str
"""
if exists(path):
if folder and path[-1] == "/":
return path[:-1]
else:
return path
else:
raise ValueError(f"Could not find {path}")
[docs]def check_list_or_str(arg_value: Union[List[str], str], arg_name: str):
"""Check if argument is of type list or string.
If the input is a list, an additional check is performed to ensure that
the list has at list one entry and that all entries are strings.
:param arg_value: object to perform the check on
:type arg_value: Union[List[str], str]
:param arg_name: additional argument name to provide informative error message
:param arg_name: str
"""
message = f"Argument {arg_name} must be a string or a list of strings."
if isinstance(arg_value, list):
if len(arg_value) < 1:
raise ValueError(f"The list {arg_name} must not be empty.")
if not all([isinstance(arg, str) for arg in arg_value]):
raise ValueError(message)
else:
if not isinstance(arg_value, str):
raise ValueError(message)