deepcave.utils.util

# Util

This module provides utilities for string generation and shortening. It also provides a function to get the difference between now and a given timestamp.

Functions

config_to_tuple(config[, round_places])

Convert a configuration dictionary to a tuple and round floats.

custom_round(number[, min_decimals, ...])

Round a number to the nearest decimal.

get_latest_change(st_mtime)

Get the difference between now and a given timestamp.

get_random_string(length)

Get a random string with a specific length.

print_progress_bar(total, iteration[, ...])

Print a simple progress bar.

short_string(value[, length, mode])

Shorten the given string.

deepcave.utils.util.config_to_tuple(config, round_places=10)[source]

Convert a configuration dictionary to a tuple and round floats.

Parameters:
  • config (dict) – The configuration dictionary.

  • round_places (int) – The number of places to round floats. Default is 10.

Return type:

tuple

deepcave.utils.util.custom_round(number, min_decimals=3, max_decimals=10)[source]

Round a number to the nearest decimal.

Parameters:
  • number (float) – The number to round.

  • min_decimals (int) – The minimum number of decimals. Default is 2.

  • max_decimals (int) – The maximum number of decimals. Default is 10.

Return type:

float

deepcave.utils.util.get_latest_change(st_mtime)[source]

Get the difference between now and a given timestamp.

Parameters:

st_mtime (int) – A timestamp to calculate the difference from.

Returns:

A string containing the passed time.

Return type:

str

deepcave.utils.util.get_random_string(length)[source]

Get a random string with a specific length.

Parameters:

length (int) – The length of the string.

Returns:

The random string with the given length.

Return type:

str

Raises:

ValueError – If the length is smaller 0.

deepcave.utils.util.print_progress_bar(total, iteration, prefix='', suffix='', decimals=1, length=100, fill='█', print_end='')[source]

Print a simple progress bar.

Parameters:
  • total (int) – The total number of iterations.

  • iteration (int) – The current iteration (usually, index+1).

  • prefix (str) – String to print before the progress bar.

  • suffix (str) – String to print after the progress bar.

  • decimals (int) – Number of decimals in the percentage.

  • length (int) – The length of the progress bar.

  • fill (str) – The character to fill the progress bar with.

  • print_end (str) – The character to print at the end of the line.

Return type:

None

deepcave.utils.util.short_string(value, length=30, *, mode='prefix')[source]

Shorten the given string.

Cut either at prefix or at suffix if necessary.

Parameters:
  • value (Any) – The value or string to shorten.

  • length (int, optional) – The length of the returned string. Default is 30.

  • mode (str, optional) – Define how to shorten the string. Default is “prefix”.

Returns:

The shortened string.

Return type:

str

Raises:

ValueError – If the given mode is unknown.