from os.path import splitext
from typing import Iterable, TypeVar, List
import numpy
[docs]
def get_first_common_element(first: Iterable[T], second: Iterable[T]) -> T:
"""Get first common element from two lists.
Args:
first (Iterable[T]): First list.
second (Iterable[T]): Second list.
Returns:
T: First common element or None if no common element is found.
"""
return next((item for item in first if item in second), None)
[docs]
def define_separator(filename: str) -> str:
"""Select separator for data values based on filename extension.
Args:
filename (str): Filename for which to get the separator.
Returns:
str: Separator for data values.
"""
if filename.endswith((".tsv", ".tabular")):
# Use tab as separator for TSV and tabular files
separator = "\t"
else:
separator = ","
return separator
[docs]
def get_extension(filename: str) -> str:
"""Get extension of filename.
Args:
filename (str): Filename for which to get the extension.
Returns:
str: Filename extension.
"""
return splitext(filename)[1][1:]
[docs]
def is_sorted(values) -> bool:
"""Check if values are sorted in ascending order.
Args:
values (Any): Values to check
Returns:
bool: True if sorted.
"""
return numpy.all(values[:-1] <= values[1:])
[docs]
def clean_column_names(column_names: List[str]) -> List[str]:
"""Clean column names by removing leading and trailing whitespaces, converting to lowercase.
Args:
column_names (List[str]): List of column names to clean.
Returns:
List[str]: List of cleaned column names.
"""
return [name.strip().lower() for name in column_names]