guidellm.utils.functions
Utility functions for safe operations and value handling.
Provides defensive programming utilities for common operations that may encounter None values, invalid inputs, or edge cases. Includes safe arithmetic operations, attribute access, and timestamp formatting.
all_defined(*values)
Check if all provided values are defined (not None).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values | Any | None | Variable number of values to check for None | () |
Returns:
| Type | Description |
|---|---|
bool | True if all values are not None, False otherwise |
Source code in src/guidellm/utils/functions.py
safe_add(*values, signs=None, default=0.0)
Safely add multiple numbers with None handling and optional signs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values | int | float | None | Variable number of values to add, None values use default | () |
signs | list[int] | None | Optional list of 1 (add) or -1 (subtract) for each value. If None, all values are added. Must match length of values. | None |
default | float | Value to substitute for None values | 0.0 |
Returns:
| Type | Description |
|---|---|
float | Result of adding all values safely (default used when value is None) |
Source code in src/guidellm/utils/functions.py
safe_divide(numerator, denominator, num_default=0.0, den_default=1.0)
Safely divide two numbers with None handling and zero protection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator | int | float | None | Number to divide, or None to use num_default | required |
denominator | int | float | None | Number to divide by, or None to use den_default | required |
num_default | float | Default value for numerator if None | 0.0 |
den_default | float | Default value for denominator if None | 1.0 |
Returns:
| Type | Description |
|---|---|
float | Division result with protection against division by zero |
Source code in src/guidellm/utils/functions.py
safe_format_number(number, precision=1, default='--')
Safely format a number with specified precision and default handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number | int | float | None | Number to format, or None | required |
precision | int | Number of decimal places for formatting floats | 1 |
default | str | Value to return if number is None | '--' |
Returns:
| Type | Description |
|---|---|
str | Formatted number string or default value |
Source code in src/guidellm/utils/functions.py
safe_format_timestamp(timestamp, format_='%H:%M:%S', default='N/A')
Safely format a timestamp with error handling and validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamp | float | int | None | Unix timestamp to format, or None | required |
format_ | str | Strftime format string for timestamp formatting | '%H:%M:%S' |
default | str | Value to return if timestamp is invalid or None | 'N/A' |
Returns:
| Type | Description |
|---|---|
str | Formatted timestamp string or default value |
Source code in src/guidellm/utils/functions.py
safe_getattr(obj, attr, default=None)
Safely get an attribute from an object with None handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj | Any | None | Object to get the attribute from, or None | required |
attr | str | Name of the attribute to retrieve | required |
default | Any | Value to return if object is None or attribute doesn't exist | None |
Returns:
| Type | Description |
|---|---|
Any | Attribute value or default if not found or object is None |
Source code in src/guidellm/utils/functions.py
safe_multiply(*values, default=1.0)
Safely multiply multiple numbers with None handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values | int | float | None | Variable number of values to multiply, None values treated as 1.0 | () |
default | float | Starting value for multiplication | 1.0 |
Returns:
| Type | Description |
|---|---|
float | Product of all non-None values multiplied by default |