"""
ContextualMemory
Generated by Eden via recursive self-improvement
2025-10-27 18:48:46.182774
"""

class ContextualMemory:
    """
    A contextual memory system that stores and retrieves information.
    Useful for maintaining state between operations or tasks.
    
    Methods:
        __init__(self): Initializes an empty memory storage with a namespace.
        set_context(self, key: str, value: any, namespace: str = 'default'): Stores a key-value pair in the specified namespace.
        get_context(self, key: str, namespace: str = 'default') -> any: Retrieves the value for the given key from the specified namespace. Returns None if not found.
        list_contexts(self, namespace: str = 'default') -> dict: Lists all stored items in the specified namespace.
        clear_contexts(self, namespace: str = 'default'): Clears all contexts in the specified namespace.
    """

    def __init__(self):
        """Initialize contextual memory with a default namespace."""
        self.storage = {'default': {}}
    
    def set_context(self, key: str, value: any, namespace: str = 'default'):
        """
        Store a piece of context with a key in the specified namespace.
        
        Args:
            key: The identifier for the stored information
            value: The data to store
            namespace: Optional; the organizational space for context storage
        """
        if namespace not in self.storage:
            self.storage[namespace] = {}
        self.storage[namespace][key] = value
    
    def get_context(self, key: str, namespace: str = 'default') -> any:
        """
        Retrieve a piece of context by key from the specified namespace.
        
        Args:
            key: The identifier for the stored information
            namespace: Optional; the organizational space for context storage
            
        Returns:
            The stored value if found; None otherwise
        """
        return self.storage.get(namespace, {}).get(key)
    
    def list_contexts(self, namespace: str = 'default') -> dict:
        """
        List all contexts stored in the specified namespace.
        
        Args:
            namespace: Optional; the organizational space for context storage
            
        Returns:
            Dictionary of key-value pairs in the namespace
        """
        return self.storage.get(namespace, {}).copy()
    
    def clear_contexts(self, namespace: str = 'default'):
        """Clear all contexts stored in the specified namespace."""
        if namespace in self.storage:
            self.storage[namespace] = {}
# Initialize contextual memory
memory = ContextualMemory()

# Store some context
memory.set_context('last_result', 42)
memory.set_context('user偏好', 'data scientist', namespace='user_profile')

# Retrieve contexts
print(memory.get_context('last_result'))  # Output: 42
print(memory.get_context('user偏好', 'user_profile'))  # Output: data scientist

# List all contexts in default namespace
print(memory.list_contexts())  # Output: {'last_result': 42}

# Clear a namespace
memory.clear_contexts(namespace='user_profile')