abraham9486937737
Deploy MySpace Ooty Analytics to Hugging Face - with KPI styling updates
04b129a
"""
Utility functions for the project
"""
import os
from pathlib import Path
from datetime import datetime
from typing import Union, Optional
import json
def get_project_root() -> Path:
"""Get the project root directory"""
return Path(__file__).parent.parent
def ensure_dir_exists(directory: Union[str, Path]) -> Path:
"""Create directory if it doesn't exist"""
path = Path(directory)
path.mkdir(parents=True, exist_ok=True)
return path
def get_timestamp() -> str:
"""Get current timestamp as string"""
return datetime.now().strftime("%Y%m%d_%H%M%S")
def save_results(data: dict, filename: str, directory: Optional[Union[str, Path]] = None) -> Path:
"""
Save results as JSON file
Args:
data: Dictionary to save
filename: Output filename
directory: Output directory (default: outputs/)
Returns:
Path to saved file
"""
if directory is None:
directory = get_project_root() / "data" / "outputs"
ensure_dir_exists(directory)
filepath = Path(directory) / filename
with open(filepath, 'w') as f:
json.dump(data, f, indent=4)
return filepath
def format_number(value: float, decimals: int = 2) -> str:
"""Format number with specified decimals"""
return f"{value:.{decimals}f}"
def generate_file_path(prefix: str = "", suffix: str = "",
extension: str = "csv", directory: Optional[str] = None) -> Path:
"""Generate a timestamped file path"""
if directory is None:
directory = get_project_root() / "data" / "outputs"
ensure_dir_exists(directory)
timestamp = get_timestamp()
filename = f"{prefix}_{timestamp}_{suffix}.{extension}".strip("_")
return Path(directory) / filename
def log_message(message: str, level: str = "INFO") -> str:
"""Create a formatted log message"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return f"[{timestamp}] [{level}] {message}"