IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
utils.py
Go to the documentation of this file.
1from typing import Any, Tuple, Type
2
3from ids_peak_common.exceptions import InvalidParameterException
4
5
6def is_equal(left: int | float, right: int | float, tolerance: float =1e-7) -> bool:
7 """!
8 @brief
9 Compare two numbers for equality, allowing a tolerance for floating point comparison.
10
11 @param left: First number (int or float).
12 @param right: Second number (int or float).
13 @param tolerance: Tolerance for floating point comparison. Ignored for integers.
14 @return True if the numbers are considered equal, False otherwise.
15 """
16 if isinstance(left, int) and isinstance(right, int):
17 return left == right
18 elif isinstance(left, (int, float)) and isinstance(right, (int, float)):
19 return abs(left - right) < tolerance
20 else:
21 return False
22
23
24def validate_parameter_types(*args: Any, accepted_types: Tuple[Type, ...]) -> None:
25 """!
26 @brief
27 Validate that all given arguments are instances of the accepted types and are all of the same type.
28
29 @param args: Parameters to validate.
30 @param accepted_types: Tuple of allowed types (default: (int, float)).
31 @exception TypeError: If any argument is not of an accepted type, or if the arguments have inconsistent types.
32 """
33 if not all(isinstance(v, accepted_types) for v in args):
34 allowed = ", ".join(t.__name__ for t in accepted_types)
35 raise InvalidParameterException(f"All parameters must be instances of: {allowed}")
36
37 types = {type(v) for v in args}
38 if len(types) > 1:
39 raise InvalidParameterException("All parameters must be of the same type")
Thrown when a function receives an invalid parameter.
Definition exceptions.py:13
bool is_equal(int|float left, int|float right, float tolerance=1e-7)
Compare two numbers for equality, allowing a tolerance for floating point comparison.
Definition utils.py:6
None validate_parameter_types(*Any args, Tuple[Type,...] accepted_types)
Validate that all given arguments are instances of the accepted types and are all of the same type.
Definition utils.py:24