AI-Colab / core /globals.py
A.Tuin
initial commit
0b2210a
Raw History Blame Contribute Delete
859 Bytes
"""
This module provides centralized getters for global objects, ensuring lazy initialization
and efficient access across the AIColab application.
"""
from typing import Any
_globals: dict[str, Any] = {}
def set_global(key: str, value: Any) -> None:
"""Sets a global object."""
_globals[key] = value
def get_global(key: str) -> Any:
"""Gets a global object."""
return _globals.get(key)
def initialize_global(key: str, initializer: Any) -> Any:
"""
Initializes a global object if it doesn't exist, using the provided initializer.
Returns the initialized or existing global object.
"""
if key not in _globals:
_globals[key] = initializer()
return _globals[key]
# Example usage (will be removed or modified as needed)
# def get_logger():
# return initialize_global("logger", lambda: create_new_logger())