ovo.core.utils.formatting

Module Contents

Classes

Functions

format_duration

Returns: 1 day 2 hours and 5 minutes

datetime_from_utc_to_local

generate_id

Generate a unique ID using 3 or more random letters.

generate_unique_id

Generate unique UID shortened for memory efficiency using UUID prefix.

get_hash_of_bytes

Get SHA1 hash string of the given bytes, for example ‘2aae6c35c94fcfb415dbe95f408b9ce91ee846ed’

get_hashed_path_for_bytes

Get directory and subdirectory based on SHA1 hash, for example ‘2a/ae6c35c94fcfb415dbe95f408b9ce91ee846ed’ ^ Note the slash here

safe_filename

parse_args

Parse list of args into a dict

get_alphanumeric_sort_key

Get a sort key that sorts strings with numbers in an alphanumeric way.

sorted_alphanumeric

Sort a list of strings in an alphanumeric way.

truncated_list

Join a list truncated to a maximum number of items, adding ellipsis if necessary.

truncate_middle

Truncate a string in the middle if it exceeds the maximum length, adding ellipsis.

parse_duration

Parse a duration string like ‘1d 27m 54s’ into total seconds.

tail_filtered

Read a file in reverse and return up to the last N lines that contain any of the keywords.

mix_colors

API

ovo.core.utils.formatting.format_duration(td)

Returns: 1 day 2 hours and 5 minutes

ovo.core.utils.formatting.datetime_from_utc_to_local(utc_datetime)
ovo.core.utils.formatting.generate_id(previous_ids: Collection[str]) str

Generate a unique ID using 3 or more random letters.

Args: previous_ids: List of existing IDs to avoid.

Returns: str: A unique identifier.

ovo.core.utils.formatting.generate_unique_id(model_class, tries=100, min_length=6, max_length=10)

Generate unique UID shortened for memory efficiency using UUID prefix.

This function generates shortened UUIDs and checks uniqueness by querying the database.

Args: model_class: The SQLAlchemy model class that has a get(id=…) class method tries: Maximum number of attempts per length (default: 100) min_length: Minimum ID length to try (default: 6) max_length: Maximum ID length to try (default: 10)

Returns: str: A unique identifier

Raises: ValueError: If no unique ID could be generated within the specified constraints

ovo.core.utils.formatting.get_hash_of_bytes(value: bytes) str

Get SHA1 hash string of the given bytes, for example ‘2aae6c35c94fcfb415dbe95f408b9ce91ee846ed’

ovo.core.utils.formatting.get_hashed_path_for_bytes(value: bytes) str

Get directory and subdirectory based on SHA1 hash, for example ‘2a/ae6c35c94fcfb415dbe95f408b9ce91ee846ed’ ^ Note the slash here

This is done similarly to nextflow workdir to avoid exceeding the directory limit of some filesystems.

ovo.core.utils.formatting.safe_filename(filename)
ovo.core.utils.formatting.parse_args(argv: list[str]) dict

Parse list of args into a dict

For example: [’–foo’, ‘bar’, ‘–bar’, ‘baz’, ‘–flag’] into dict {‘foo’: ‘bar’, ‘bar’: ‘baz’, ‘flag’: True}

ovo.core.utils.formatting.get_alphanumeric_sort_key(value: str) tuple

Get a sort key that sorts strings with numbers in an alphanumeric way.

For example, “item2” will come before “item10”.

Parameters:

value – The string to generate a sort key for.

Returns:

A tuple that can be used as a sort key.

ovo.core.utils.formatting.sorted_alphanumeric(values: Collection[str]) list[str]

Sort a list of strings in an alphanumeric way.

For example, “item2” will come before “item10”.

Parameters:

values – The list of strings to sort.

Returns:

The sorted list of strings.

ovo.core.utils.formatting.truncated_list(items: Collection[Any], max_items: int, sep: str = ', ') str

Join a list truncated to a maximum number of items, adding ellipsis if necessary.

ovo.core.utils.formatting.truncate_middle(text: str, max_length: int) str | None

Truncate a string in the middle if it exceeds the maximum length, adding ellipsis.

Returns:

The truncated string if it exceeds max_length, otherwise the original string.

ovo.core.utils.formatting.parse_duration(duration: str) int | None

Parse a duration string like ‘1d 27m 54s’ into total seconds.

Allowed units: d = days h = hours m = minutes s = seconds

Parameters:

duration – Duration string to parse

Returns:

Total duration in seconds, or None if input is empty or NaN

ovo.core.utils.formatting.tail_filtered(path, keywords, max_lines=10, bufsize=8192)

Read a file in reverse and return up to the last N lines that contain any of the keywords.

class ovo.core.utils.formatting.ColorPicker(seed: int = 27)

Initialization

__call__(value)
ovo.core.utils.formatting.mix_colors(first_hex, second_hex)