Add example projects
This commit is contained in:
57
examples/complex_project/utils.py
Normal file
57
examples/complex_project/utils.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
"""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}")
|
||||||
Reference in New Issue
Block a user