cosapp.utils.helpers

Various small helper functions.

Functions

check_arg(arg, argname, dtype[, value_ok, …])

Utility function for argument type and value validation.

get_typename(dtype[, multiformat])

is_number(value)

Test if a value is a Number or a 0d numerical array

is_numerical(value)

Test if a value is numerical based on its type.

cosapp.utils.helpers.check_arg(arg: Any, argname: str, dtype: Union[Type, Iterable[Type]], value_ok: Optional[Callable[[Any], bool]] = None, stack_shift: int = 0)[source]

Utility function for argument type and value validation. Raises a TypeError exception if type(arg) is not in type list given by ‘dtype’. Raises a ValueError exception if value_ok(arg) is False, where ‘value_ok’ is a boolean function defining a validity criterion.

For example: >>> check_arg(-0.12, ‘my_var’, float) does not raise any exception, as -0.12 is a float

>>> check_arg(-0.12, 'my_var', (int, str))
raises TypeError, as first argument is neither an int, not a str
>>> check_arg(-0.12, 'my_var', float, value_ok = lambda x: x > 0)
raises ValueError, as first argument is not strictly positive
cosapp.utils.helpers.get_typename(dtype: Union[Type, Tuple[Type]], multiformat='({})')str[source]
cosapp.utils.helpers.is_number(value: Any)bool[source]

Test if a value is a Number or a 0d numerical array

Currently type considered as numerical are:

  • int; but not bool

  • float; including numpy.inf and numpy.nan

  • complex

  • numpy.ndarray of dtype numpy.number and ndim == 0

Parameters

value (Any) – Value to test

Returns

Is the value numerical type?

Return type

bool

cosapp.utils.helpers.is_numerical(value: Any)bool[source]

Test if a value is numerical based on its type.

Currently type considered as numerical are:

  • int; but not bool

  • float; including numpy.inf and numpy.nan

  • complex

  • numpy.ndarray of dtype numpy.number

  • Collection convertible in a numpy array of dtype derived from numpy.number

Parameters

value (Any) – Value to test

Returns

Is the value numerical type?

Return type

bool