"""
ContextManager
Generated by Eden via recursive self-improvement
2025-10-27 19:49:16.167062
"""

class ContextManager:
    """A class to manage multiple execution contexts, each with its own variables and settings.
    
    Features:
    - Create new contexts
    - Switch between contexts
    - Store context-specific variables
    - Retrieve context data
    
    Example usage:
    >>> cm = ContextManager()
    >>> cm.create_context("default")
    >>> cm.switch_context("default")
    >>> cm.get_variable("x", 42)
    >>> cm.get_variable("x")
    42
    """

    def __init__(self):
        """Initialize the ContextManager with default settings."""
        self._contexts = {}
        self._current_context = None
        
    def create_context(self, context_name):
        """Create a new context with the given name.
        
        Args:
            context_name (str): Name of the new context
        """
        if context_name not in self._contexts:
            self._contexts[context_name] = {}
            if self._current_context is None:
                self._current_context = context_name
            return True
        return False
    
    def switch_context(self, context_name):
        """Switch to the specified context.
        
        Args:
            context_name (str): Name of the context to switch to
        """
        if context_name in self._contexts:
            self._current_context = context_name
            return True
        return False
    
    def get_variable(self, var_name, default=None):
        """Retrieve a variable from the current context.
        
        Args:
            var_name (str): Name of the variable to retrieve
            default: Default value if variable doesn't exist
        
        Returns:
            Value of the variable or default if not found
        """
        if self._current_context is not None:
            return self._contexts[self._current_context].get(var_name, default)
        return default
    
    def set_variable(self, var_name, value):
        """Set a variable in the current context.
        
        Args:
            var_name (str): Name of the variable to set
            value: Value to assign to the variable
        """
        if self._current_context is not None:
            self._contexts[self._current_context][var_name] = value
            
    def save_context(self, file_path):
        """Save the current context to a file.
        
        Args:
            file_path (str): Path where to save the context data
        """
        import json
        if self._current_context is not None:
            with open(file_path, 'w') as f:
                json.dump(self._contexts[self._current_context], f)
                
    def __repr__(self):
        """Return a string representation of the current state."""
        return f"ContextManager(current_context='{self._current_context}', available_contexts={list(self._contexts.keys())})"
cm = ContextManager()
cm.create_context("default")
cm.switch_context("default")
cm.set_variable("x", 42)
print(cm.get_variable("x"))  # Output: 42
cm.save_context("context_state.json")