File size: 2,016 Bytes
5400c2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Shared utilities for the entire learning system"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_utils.ipynb.

# %% auto 0
__all__ = ['logger', 'setup_logger', 'load_context_safely', 'save_context_safely']

# %% ../nbs/03_utils.ipynb 4
from typing import Dict, List, Optional, Any, Tuple
import json
from pathlib import Path
import logging
from datetime import datetime

# %% ../nbs/03_utils.ipynb 6
def setup_logger(name: str) -> logging.Logger:
    """Set up module logger with consistent formatting"""
    logger = logging.getLogger(name)
    if not logger.handlers:
        handler = logging.StreamHandler()
        handler.setFormatter(
            logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        )
        logger.addHandler(handler)
        logger.setLevel(logging.INFO)
    return logger

logger = setup_logger(__name__)

# %% ../nbs/03_utils.ipynb 7
def load_context_safely(path: Path) -> Dict:
    """

    Safely load learning context from JSON file.

    

    Args:

        path: Path to context file

        

    Returns:

        dict: Loaded context data

        

    Raises:

        ValueError: If file is invalid or inaccessible

    """
    try:
        with open(path, 'r') as f:
            return json.load(f)
    except json.JSONDecodeError as e:
        raise ValueError(f"Invalid context file format: {str(e)}")
    except Exception as e:
        raise ValueError(f"Error loading context file: {str(e)}")

# %% ../nbs/03_utils.ipynb 8
def save_context_safely(context: Dict, path: Path) -> None:
    """

    Safely save learning context to JSON file.

    

    Args:

        context: Context data to save

        path: Path to save file

        

    Raises:

        ValueError: If save operation fails

    """
    try:
        with open(path, 'w') as f:
            json.dump(context, f, indent=2)
    except Exception as e:
        raise ValueError(f"Error saving context: {str(e)}")