Version: v1.2.8 | Status: Stable | Last Updated: April 2026
The utils module is a collection of common helpers for file operations, JSON handling, hashing, and execution timing.
ensure_directory(path: Union[str, Path]) -> Path: Creates directories recursively.
safe_json_loads(text: str, default: Any = None) -> Any: Parses JSON without raising exceptions.safe_json_dumps(obj: Any, indent: int = 2) -> str: Serializes object to JSON safely.
hash_content(content: Union[str, bytes], algorithm: str = "sha256") -> str: Hashes string or bytes.hash_file(path: Union[str, Path], algorithm: str = "sha256") -> Optional[str]: Hashes file content.
timing_decorator: Measures wall time; addsexecution_time_mswhen the return value is adict.retry(import fromcodomyrmex.utils): Synchronous decorator withmax_attempts,delay,backoff, andexceptions. Defined on the package so it is not shadowed by a submodule name collision.retry_syncmodule (codomyrmex.utils.retry_sync):RetryConfig, synchronousretry, andasync_retrywith jitter, capped delay, andretryable_exceptions. Use this surface for async code or richer backoff than the packageretry.
get_timestamp: Returns formatted current time.truncate_string: Shortens string with suffix.get_env: Safe environment variable retrieval.flatten_dict: Flattens nested dictionaries.deep_merge: Recursively merges dictionaries.
from codomyrmex.utils import safe_json_loads, retry
@retry(max_attempts=3, delay=0.1, backoff=2.0, exceptions=(ConnectionError,))
def fetch_data():
...
data = safe_json_loads('{"key": "value"}')from codomyrmex.utils.retry_sync import async_retry, retry as retry_jitter
@retry_jitter(max_attempts=3, base_delay=0.5, jitter=True)
def call_upstream():
...
@async_retry(max_attempts=3, base_delay=0.1)
async def call_upstream_async():
...