test/moxie/utils/helpers.py
2026-03-24 04:07:54 +00:00

43 lines
981 B
Python
Executable File

"""
Helper Utilities
Common utility functions for MOXIE.
"""
import hashlib
from typing import Any, Dict
from datetime import datetime
def generate_id() -> str:
"""Generate a unique ID."""
import uuid
return str(uuid.uuid4())
def hash_content(content: bytes) -> str:
"""Generate a hash for content."""
return hashlib.sha256(content).hexdigest()
def timestamp_now() -> str:
"""Get current timestamp in ISO format."""
return datetime.now().isoformat()
def truncate_text(text: str, max_length: int = 100) -> str:
"""Truncate text with ellipsis."""
if len(text) <= max_length:
return text
return text[:max_length - 3] + "..."
def safe_json(obj: Any) -> Dict:
"""Safely convert object to JSON-serializable dict."""
if hasattr(obj, 'model_dump'):
return obj.model_dump()
elif hasattr(obj, 'dict'):
return obj.dict()
elif isinstance(obj, dict):
return obj
else:
return str(obj)