monkey_wrench.generic._common module

monkey_wrench.generic._common.assert_(item: ~monkey_wrench.generic._common.T, message: str, exception: type[Exception] = <class 'ValueError'>, silent: bool = True) T[source]

Assert the truth value of the item, and return the item or raise exception.

Parameters:
  • item – The item to assert. It does not have to be a boolean. For example, given an empty list, assert [] fails as an empty list evaluates to False.

  • message – The exception message, which will be shown when the exception is raised.

  • exception – The exception to raise if both the item and silent evaluate to False. Defaults to ValueError.

  • silent – A boolean indicating whether to return the item silently or raise an exception in the case of assertion failure. Defaults to True, which means the item will be silently returned.

Returns:

Return the item if it evaluates to True. If the item evaluates to False, return it only if silent is True.

Raises:

exception – If the item evaluates to False and silent is False.

monkey_wrench.generic._common.apply_to_single_or_collection(function: Callable[[T], R], single_or_collection: dict[Any, T] | list[T] | set[T] | tuple[T, ...] | T) dict[Any, R] | list[R] | set[R] | tuple[R, ...] | R[source]

Apply the given function to a single item or all elements of a collection (dict/list/set/tuple).

Note

In the case of a dictionary, function will be applied to the values.

Warning

A string, although being a collection, is treated as a single item.

Parameters:
  • function – The function to be applied.

  • single_or_collection – Either a single item or a collection (dict/list/set/tuple).

Returns:

Either a single output, or a collection as output resulting from applying the given function.

Examples

>>> apply_to_single_or_collection(lambda x: x**2, [1, 2, 3])
[1, 4, 9]
>>> apply_to_single_or_collection(lambda x: x**2, (1, 2, 3))
(1, 4, 9)
>>> apply_to_single_or_collection(lambda x: x**2, {"a": 1, "b": 2, "c":3})
{'a': 1, 'b': 4, 'c': 9}
>>> apply_to_single_or_collection(lambda x: x**2, set())
set()
>>> apply_to_single_or_collection(lambda x: x**2, 3)
9
>>> apply_to_single_or_collection(lambda x: x*2, "book!")
'book!book!'
monkey_wrench.generic._common.collection_element_type(collection: dict[Any, T] | list[T] | set[T] | tuple[T, ...]) type[T] | None[source]

Return the type of collection elements, e.g. for set[T] it returns T.

Parameters:

collection – The collection to get the type of any element from.

Returns:

The type of any element from the collection (dict/list/set/tuple). In the case of an empty collection, None is returned.

Raises:

TypeError – If the collection elements are of different types.

Examples

>>> collection_element_type([3, 2, 1])
<class 'int'>
>>> collection_element_type(set()) is None
True
>>> # The following will lead to an exception, since the collection elements are not of the same type.
>>> # element_type_from_collection((3.0, 2.0, "1"))
monkey_wrench.generic._common.type_(single_or_collection: dict[Any, T] | list[T] | set[T] | tuple[T, ...] | T) type[T] | None[source]

Return the type of the given item, or any element from the collection using element_type_from_collection().

Examples

>>> type_([3, 2, 1])
<class 'int'>
>>> type_(3)
<class 'int'>