abaco.utils module#
- abaco.utils.assert_nonempty_keys(dictionary: dict)[source]#
Check that the keys in a dictionary are not empty strings.
- Parameters:
dictionary (dict) – A dictionary (e.g., config file).
- Raises:
AssertionError – If dictionary is not a dict or if any key is empty or blank.
- abaco.utils.assert_nonempty_vals(dictionary: dict)[source]#
Check that the values in a dictionary are not empty strings.
- Parameters:
dictionary (dict) – A dictionary (e.g., config file).
- Raises:
AssertionError – If dictionary is not a dict or if any value is empty or blank.
- abaco.utils.assert_path(filepath: str)[source]#
Check that the given filepath is a string and that it exists.
- Parameters:
filepath (str) – The filepath or folder path to check.
- Raises:
TypeError – If the filepath is not a string.
FileNotFoundError – If the filepath does not exist.
Example
>>> assert_path("..") >>> assert_path("./tests")
- abaco.utils.create_folder(directory_path: str, is_nested: bool = False) bool[source]#
Create a folder if it doesn’t exist.
- Parameters:
- Returns:
True if the folder was created, False if it already existed.
- Return type:
- Raises:
TypeError – If directory_path is not a string.
ValueError – If directory_path is an existing file.
OSError – If there is an error creating the directory.
- abaco.utils.df_joiner(df_dict: dict[DataFrame], on: str, how: str = 'outer') DataFrame[source]#
Join multiple dataframes on a common column.
- Parameters:
df_dict (dict of pandas.DataFrame) – Dictionary of dataframes to join.
on (str, optional) – Column to join on. Defaults to “taxa”.
how (str, optional) – Type of join. Defaults to “outer”.
- Returns:
Joined dataframe.
- Return type:
- abaco.utils.generate_log_filename(folder: str = 'logs', suffix: str = '') str[source]#
Create a log file name and path.
- abaco.utils.get_args(prog_name: str, others: dict = None)[source]#
Initiate argparse.ArgumentParser() and add common arguments.
- Parameters:
- Returns:
Parsed command-line arguments.
- Return type:
- Raises:
TypeError – If prog_name is not a string or others is not a dict.
- abaco.utils.get_basename(fname: None | str = None) str[source]#
Get the basename of a given filename, without file extension.
If no filename is given, returns the basename of the current script.
- abaco.utils.get_logger()[source]#
Initialize and return a logger with a log file named after the current script.
- Returns:
Configured logger object.
- Return type:
- abaco.utils.get_time(incl_time: bool = True, incl_timezone: bool = True) str[source]#
Get current date, time (optional), and timezone (optional) for file naming.
- Parameters:
- Returns:
String including date, timestamp and/or timezone, e.g. ‘yyyyMMdd_hhmm_timezone’.
- Return type:
- Raises:
TypeError – If incl_time or incl_timezone are not bool.
AssertionError – If the output format is not as expected.
- abaco.utils.init_log(filename: str, display: bool = False, logger_id: str | None = None)[source]#
Configure a custom Python logger with file and optional stdout handlers.
- Parameters:
- Returns:
Configured logger object.
- Return type:
- Raises:
TypeError – If filename is not a string or logger_id is not a string or None.
- abaco.utils.normalize_url(host: str, port: int, scheme: str = 'http') str[source]#
Normalize the given URL, ensuring it starts with the specified scheme.
- Parameters:
- Returns:
The normalized URL.
- Return type:
- Raises:
TypeError – If host, port, or scheme are not of the correct type, or if URL cannot be normalized.
Examples
>>> normalize_url("localhost", 7474) 'http://localhost:7474' >>> normalize_url("example.com", 80, "bolt") 'bolt://example.com:80'