labcore.utils.misc#

misc.py

Various utility functions.

Functions

add_end_number_to_repeated_file(path)

Checks if a file or directory with given path exists, if it does it add (x) where x is an increasing number, until a file or directory with that name does not exist.

commit_changes_in_repo(current_dir)

Commits the changes in the repository at the given directory and returns the commit hash.

get_environment_packages()

Generates a dictionary with the names of the installed packages and their current version.

indent_text(text[, level])

Indent each line of text by level spaces.

map_input_to_signature(func, *args, **kwargs)

Try to re-organize the positional arguments args and key word arguments kwargs such that func can be called with them.

reorder_indices(lst, target)

Determine how to bring a list with unique entries to a different order.

reorder_indices_from_new_positions(lst, **pos)

Determine how to bring a list with unique entries to a different order.

unwrap_optional(val)

Covert a variable of type Optional[T] to T If the variable has value None a ValueError will be raised

Classes

AutoEnum(value)

Enum that with automatically incremented integer values.

LabeledOptions(value)

Enum with a label for each element.

class labcore.utils.misc.AutoEnum(value)[source]#

Bases: Enum

Enum that with automatically incremented integer values.

Allows to pass additional arguments in the class variables to the __init__ method of the instances. See: https://stackoverflow.com/questions/19330460/how-do-i-put-docstrings-on-enums/19330461#19330461

class labcore.utils.misc.LabeledOptions(value)[source]#

Bases: AutoEnum

Enum with a label for each element. We can find the name from the label using fromLabel().

Example:

>>> class Color(LabeledOptions):
...     red = 'Red'
...     blue = 'Blue'

Here, Color.blue has value 2 and Color.fromLabel('Blue') returns Color.blue.

classmethod fromLabel(label: str) LabeledOptions | None[source]#

Find enum element from label.

labcore.utils.misc.add_end_number_to_repeated_file(path: Path) Path[source]#

Checks if a file or directory with given path exists, if it does it add (x) where x is an increasing number, until a file or directory with that name does not exist.

labcore.utils.misc.commit_changes_in_repo(current_dir: Path) str | None[source]#

Commits the changes in the repository at the given directory and returns the commit hash. If the directory is not a git repository, it returns None.

labcore.utils.misc.get_environment_packages()[source]#

Generates a dictionary with the names of the installed packages and their current version. It detects if a package was installed in development mode and places the current commit hash instead of the version.

labcore.utils.misc.indent_text(text: str, level: int = 0) str[source]#

Indent each line of text by level spaces.

labcore.utils.misc.map_input_to_signature(func: Callable | Signature, *args: Any, **kwargs: Any)[source]#

Try to re-organize the positional arguments args and key word arguments kwargs such that func can be called with them.

if func expects arguments that cannot be given, they will be given as None. Surplus arguments are ignored if func does not accept variable positional and/or keyword arguments. Example:

>>> def myfunc(x, y, z=1):
...     print(f"x={x}, y={y}, z={z}")
...
... args, kwargs = map_input_to_signature(myfunc, z=1, x=1, unused=4)
... myfunc(*args, **kwargs)
x=1, y=None, z=1

It is important to note that the position of positional arguments is not preserved, because input key words that match expected positional arguments are inserted as positional arguments at the right position. The order, however, is preserved. Example:

>>> def myfunc(x, y, z):
...     print(f"x={x}, y={y}, z={z}")
...
... args, kwargs = map_input_to_signature(myfunc, 1, 2, x=5)
... myfunc(*args, **kwargs)
x=5, y=1, z=2
labcore.utils.misc.reorder_indices(lst: Sequence[str], target: Sequence[str]) Tuple[int, ...][source]#

Determine how to bring a list with unique entries to a different order.

Supports only lists of strings.

Parameters:
  • lst – input list

  • target – list in the desired order

Returns:

the indices that will reorder the input to obtain the target.

Raises:

ValueError for invalid inputs.

labcore.utils.misc.reorder_indices_from_new_positions(lst: List[str], **pos: int) Tuple[int, ...][source]#

Determine how to bring a list with unique entries to a different order.

Parameters:
  • lst – input list (of strings)

  • pos – new positions in the format element = new_position. non-specified elements will be adjusted automatically.

Returns:

the indices that will reorder the input to obtain the target.

Raises:

ValueError for invalid inputs.

labcore.utils.misc.unwrap_optional(val: T | None) T[source]#

Covert a variable of type Optional[T] to T If the variable has value None a ValueError will be raised