tahamajs's picture
download
raw
3.12 kB
import json
import yaml
from pathlib import Path
from typing import Dict, Any, Optional
from dataclasses import dataclass, asdict
from loguru import logger
@dataclass
class Config:
environment: str = "development"
debug: bool = False
max_steps: int = 10
max_trials: int = 3
timeout_seconds: int = 300
evaluation_enabled: bool = True
save_results: bool = True
generate_plots: bool = True
log_level: str = "INFO"
log_to_file: bool = True
log_file: str = "results/logs/ca24.log"
results_dir: str = "results"
plots_dir: str = "results/plots"
data_dir: str = "results/data"
enable_web_search: bool = False
web_search_timeout: int = 30
plot_format: str = "png"
plot_dpi: int = 300
plot_style: str = "seaborn"
def to_dict(self) -> Dict[str, Any]:
return asdict(self)
def save(self, filepath: str) -> None:
config_dict = self.to_dict()
file_path = Path(filepath)
file_path.parent.mkdir(parents=True, exist_ok=True)
if file_path.suffix.lower() == ".json":
with open(file_path, "w", encoding="utf-8") as f:
json.dump(config_dict, f, indent=2, ensure_ascii=False)
elif file_path.suffix.lower() in [".yaml", ".yml"]:
with open(file_path, "w", encoding="utf-8") as f:
yaml.dump(config_dict, f, default_flow_style=False, allow_unicode=True)
else:
raise ValueError(f"Unsupported config file format: {file_path.suffix}")
logger.info(f"Config saved to {filepath}")
@classmethod
def load(cls, filepath: str) -> "Config":
file_path = Path(filepath)
if not file_path.exists():
logger.warning(f"Config file not found: {filepath}, using defaults")
return cls()
if file_path.suffix.lower() == ".json":
with open(file_path, "r", encoding="utf-8") as f:
config_dict = json.load(f)
elif file_path.suffix.lower() in [".yaml", ".yml"]:
with open(file_path, "r", encoding="utf-8") as f:
config_dict = yaml.safe_load(f)
else:
raise ValueError(f"Unsupported config file format: {file_path.suffix}")
logger.info(f"Config loaded from {filepath}")
return cls(**config_dict)
def load_config(config_file: Optional[str] = None) -> Config:
if config_file and Path(config_file).exists():
return Config.load(config_file)
default_configs = [
"config/config.yaml",
"config/config.json",
"config.yaml",
"config.json",
]
for config_path in default_configs:
if Path(config_path).exists():
logger.info(f"Found default config: {config_path}")
return Config.load(config_path)
logger.info("No config file found, using default settings")
return Config()
def create_default_config(config_file: str = "config/config.yaml") -> Config:
config = Config()
config.save(config_file)
logger.info(f"Default configuration created at {config_file}")
return config

Xet Storage Details

Size:
3.12 kB
·
Xet hash:
18f19bad38a7fcc8007f4ee0c6875ad14fb838fdf6a02f577c600abbfc7673e9

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.