Utilities
Helper functions and decorators.
Overview
The utils module provides:
- Validators: Input validation functions
- Decorators: Reusable function decorators
Validators
validate_dataset_identifier
Validates and resolves dataset identifiers:
from huwise_utils_py import validate_dataset_identifier
# Resolve a numeric dataset ID
resolved = validate_dataset_identifier(dataset_id="100123")
# Validation errors
validate_dataset_identifier() # ValueError: neither provided
validate_dataset_identifier(dataset_id="100123", dataset_uid="da_abc") # ValueError: both provided
Decorators
retry
Retry decorator with exponential backoff:
from huwise_utils_py.utils.decorators import retry
import httpx
@retry(httpx.HTTPError, tries=3, delay=1, backoff=2)
def fetch_data():
response = httpx.get("https://api.example.com/data")
response.raise_for_status()
return response.json()
# First failure: waits 1s
# Second failure: waits 2s
# Third attempt: either succeeds or raises
Parameters:
exceptions_to_check: Exception type(s) to catchtries: Number of attempts (default: 4)delay: Initial delay in seconds (default: 3)backoff: Multiplier for delay (default: 2)
API Reference
Validators for Huwise Utils.
This module provides validation functions for dataset identifiers and other input validation needs.
validate_dataset_identifier(dataset_id: str | None = None, dataset_uid: str | None = None, config: HuwiseConfig | None = None) -> str
Validate and resolve dataset identifier to UID.
Either dataset_id or dataset_uid must be provided, but not both. If dataset_id is provided, it will be resolved to the corresponding UID.
| PARAMETER | DESCRIPTION |
|---|---|
dataset_id
|
The numeric identifier of the dataset.
TYPE:
|
dataset_uid
|
The unique string identifier (UID) of the dataset.
TYPE:
|
config
|
Optional HuwiseConfig for ID resolution.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The dataset UID. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If both dataset_id and dataset_uid are provided. |
ValueError
|
If neither dataset_id nor dataset_uid is provided. |
IndexError
|
If dataset_id cannot be resolved to a UID. |
Examples:
Resolve a numeric ID to a UID:
Use a UID directly:
Source code in src/huwise_utils_py/utils/validators.py
Decorators for Huwise Utils.
This module provides reusable decorators following DCC coding standards.
retry(exceptions_to_check: type[Exception] | tuple[type[Exception], ...], tries: int = 4, delay: float = 3, backoff: float = 2) -> Any
Retry decorator with exponential backoff.
Retries the decorated function on specified exceptions with configurable attempts and exponential delay.
| PARAMETER | DESCRIPTION |
|---|---|
exceptions_to_check
|
Exception(s) to catch and retry on.
TYPE:
|
tries
|
Number of attempts before giving up.
TYPE:
|
delay
|
Initial delay between retries in seconds.
TYPE:
|
backoff
|
Multiplier for delay after each retry.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
Decorated function with retry logic. |
Example
Source code in src/huwise_utils_py/utils/decorators.py
log_execution_time(func: Any) -> Any
Log the execution time of a function.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
Function to decorate.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
Decorated function that logs execution time. |