bh_utils.conversions module

Some custom conversion functions.

For usage examples, see ./tests/test_conversions.py.

bh_utils.conversions.str_to_bool(str_val: str, exception_msg=None) bool | None

Convert a string to Boolean.

Reference:

If str_val can be converted to Boolean, then return a Boolean value. Otherwise, return None if exception_msg is None; raise an exception otherwise.

The behaviour of this function is different to the standard function distutils.util.strtobool(val).

Parameters:
  • str_val (str) – a string to be converted to Boolean.

  • exception_msg (str) – the exception message to be raised if str_val can’t be converted to Boolean.

Returns:

a Boolean if str_val can be converted. None if can’t be converted and exception_msg is None.

Return type:

Bool or None.

Raises:

Exception – if str_val can not be converted and exception_msg is not None.

bh_utils.conversions.is_integer(value: int | str) bool

Check whether the value of a variable is an integer or can be converted to an integer.

Reference:
Parameters:

value ([int, str]) – value to be checked.

Returns:

True if value is an integer or can be converted to an integer. False otherwise.

Return type:

Bool.