Files
codechunk-cli/examples/complex_project/utils.py
7000pctAUTO 4b37b19a7b
Some checks failed
CI / build (push) Has been cancelled
CI / test (push) Has been cancelled
Add example projects
2026-02-01 23:47:38 +00:00

58 lines
1.4 KiB
Python

"""Utility functions."""
from typing import List, TypeVar, Callable
import json
T = TypeVar('T')
def validate_input(data: dict, required_fields: List[str]) -> bool:
"""Validate that all required fields are present."""
for field in required_fields:
if field not in data:
return False
return True
def parse_json_safe(json_string: str, default: T = None) -> T:
"""Safely parse JSON string."""
try:
return json.loads(json_string)
except (json.JSONDecodeError, TypeError):
return default
def retry_operation(func: Callable, max_retries: int = 3, delay: float = 1.0):
"""Retry an operation with exponential backoff."""
import time
for attempt in range(max_retries):
try:
return func()
except Exception as e:
if attempt == max_retries - 1:
raise e
time.sleep(delay * (2 ** attempt))
return None
class Logger:
"""Simple logger utility."""
def __init__(self, name: str):
self.name = name
def info(self, message: str) -> None:
"""Log info message."""
print(f"[INFO] [{self.name}] {message}")
def error(self, message: str) -> None:
"""Log error message."""
print(f"[ERROR] [{self.name}] {message}")
def debug(self, message: str) -> None:
"""Log debug message."""
print(f"[DEBUG] [{self.name}] {message}")