scaffold.ctx_manager ==================== .. py:module:: scaffold.ctx_manager Attributes ---------- .. autoapisummary:: scaffold.ctx_manager.DEFAULT_LOGGING scaffold.ctx_manager.DISABLED_LOGGING scaffold.ctx_manager.NONE_LOGGING scaffold.ctx_manager.STDOUT_AND_LOGFILE_LOGGING scaffold.ctx_manager.STDOUT_LOGGING scaffold.ctx_manager.logger Classes ------- .. autoapisummary:: scaffold.ctx_manager.LoggingContext scaffold.ctx_manager.TimerContext scaffold.ctx_manager.WandBContext Functions --------- .. autoapisummary:: scaffold.ctx_manager.combined_context Module Contents --------------- .. py:class:: LoggingContext(log_config: omegaconf.DictConfig = DEFAULT_CONFIGURATION, verbose: Union[bool, str, Sequence[str]] = False) Bases: :py:obj:`contextlib.AbstractContextManager` Context manager for configuring the logging system. It wraps the hydra's logging configuration and allows to specify verbosity level per module, see hydra documentation for more details: https://hydra.cc/docs/tutorials/basic/running_your_app/logging/ Example usage: .. code-block:: python cfg = LoggingContext.DEFAULT_CONFIGURATION with LoggingContext(cfg) as log_context: logging.info("This message should be logged with the specified configuration.") logging.info("This error should be logged with default configuration.") Initialize the configuration of the logging context. .. py:method:: __enter__() Configure the logging system with the specified settings on entering the context. .. py:method:: __exit__(exc_type, exc_val, exc_tb) Exit the context (and ideally restore previous logging configuration). .. py:attribute:: DEFAULT_CONFIGURATION :type: omegaconf.DictConfig .. py:attribute:: SILENT_CONFIGURATION :type: omegaconf.DictConfig .. py:attribute:: log_config .. py:attribute:: verbose :value: False .. py:class:: TimerContext(module_name: str = None) Bases: :py:obj:`contextlib.AbstractContextManager` An abstract base class for context managers. Initialize the timer context manager. :param module_name: Optional name of the module to be displayed in the log message. :type module_name: str .. py:method:: __enter__() Enter the timer context and start the timer .. py:method:: __exit__(exc_type, exc_val, exc_tb) Exit the timer context and log the time taken for execution .. py:attribute:: module_name :value: None .. py:attribute:: start_time :value: None .. py:class:: WandBContext(user: Optional[str] = None, base_url: str = None, entity: str = 'mxm', run_id: Optional[str] = None, resume: bool = False, run_config: Optional[omegaconf.DictConfig] = None, **kwargs) Bases: :py:obj:`contextlib.AbstractContextManager` Context manager for configuring wandb, starting and exiting a wandb run. Example usage: .. code-block:: python with WandBContext(project="chameleon", entity="mg515") as wandb_ctx: # Do something with the WandB context wandb.log({"metric": 0.5}) WandB Context that connects to our WandB instance, creates a WandB run and logs config to WandB instance. :param user: Optional cloud username to retrieve wandb login secrets. See Also: :func:`scaffold.wandb.helpers.wandb_environment_setup()` :param base_url: Base URL of WandB instance. :param entity: Name of the WandB entity. :param run_id: Flyte or WandB run ID. :param resume: Whether to resume previous run specified by run_id. :param run_config: Config passed to wandb init. :param \*\*kwargs: Additional kwargs passed to wandb init. See https://docs.wandb.ai/ref/python/init. .. py:method:: __enter__() Enters the context manager and starts a new WandB run. .. py:method:: __exit__(exc_type: Optional[type], exc_val: Optional[Exception], exc_tb: Optional[Any]) -> Optional[bool] Raise any exception triggered within the runtime context. .. py:attribute:: entity :value: 'mxm' .. py:attribute:: kwargs .. py:attribute:: run :value: None .. py:attribute:: run_config .. py:attribute:: run_id .. py:function:: combined_context(*contexts: List[contextlib.AbstractContextManager]) Combine multiple context managers into a stack, since contexts should be exited in reverse order in which they are entered. If an exception occurred, this order matters, as any context manager could suppress the exception, at which point the remaining managers will not even get notified Example use case: .. code-block:: python with combined_context(LoggingContext(), WandBContext()) as (logger, wandb): ... .. py:data:: DEFAULT_LOGGING :type: dict .. py:data:: DISABLED_LOGGING :type: dict .. py:data:: NONE_LOGGING :type: dict .. py:data:: STDOUT_AND_LOGFILE_LOGGING :type: dict .. py:data:: STDOUT_LOGGING :type: dict .. py:data:: logger