cosapp.utils.helpers¶
Various small helper functions.
Functions
|
Utility function for argument type and value validation. |
|
|
|
Test if a value is a Number or a 0d numerical array |
|
Test if a value is numerical based on its type. |
|
Partition a collection into two lists, using filter function predicate. |
- cosapp.utils.helpers.check_arg(arg: Any, argname: str, dtype: Type | Iterable[Type], value_ok: 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.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
- cosapp.utils.helpers.partition(iterable: Iterable[Any], predicate: Callable[[Any], bool])[source]¶
Partition a collection into two lists, using filter function predicate.
Parameters:¶
- iterable: Iterable[Any]
Iterable collection of elements confronted to predicate.
- predicate: Callable[[Any], bool]
Boolean function used to partition the collection.
Returns:¶
- yays, nays: tuple[list, list]
Lists containing the elements for which predicate is True (yays) and False (nays).