.. _instances: Cliasi instances ================== Having multiple cliasi instances allows you to easily communicate different program scopes. Part A of your program has one instance with its own prefix while part B has another instance with a different prefix. .. code-block:: python from cliasi import Cliasi def scope_1(): cli = Cliasi("scope_1") cli.success("Message will be prefixed with [scope_1]") def scope_2(): cli = Cliasi("scope_2") cli.warning("Message will be prefixed with [scope_2]") Instance options """"""""""""""""" Every cliasi instance has the following parameters / methods: * :meth:`~cliasi.cliasi.Cliasi.set_prefix()` - to set the prefix for every message from this instance * :meth:`~cliasi.cliasi.Cliasi.infer_settings()` - to infer settings like the ones below from the global instance, see :ref:`global_inference` * :attr:`~cliasi.cliasi.Cliasi.enable_colors` - whether to use colored output for this instance * :attr:`~cliasi.cliasi.Cliasi.max_dead_space` - maximum number of empty space between aligned messages for this instance. See :ref:`max_dead_space` * :attr:`~cliasi.cliasi.Cliasi.min_verbose_level` - verbosity level for this instance * :attr:`~cliasi.cliasi.Cliasi.messages_stay_in_one_line` - whether messages should stay in one line for this instance .. note:: ``messages_stay_in_one_line`` does not affect progress bars, animations and messages that go over multiple lines due to API limitations. .. _global_inference: Global inference """"""""""""""""" :attr:`~cliasi.cliasi.Cliasi.min_verbose_level` **and** :attr:`~cliasi.cliasi.Cliasi.messages_stay_in_one_line` are inferred from the global (:data:`cliasi.cli`) instance if not set (None). This means that if you set these parameters on the global instance, all other instances will inherit these settings unless you explicitly set them. .. code-block:: python :caption: examples/cliasi_multiple_instances.py from cliasi import Cliasi, cli def function_that_has_no_idea_about_main_program(): # Create a new instance with its own prefix local_cli = Cliasi(prefix="FUNC") local_cli.log("Debug will be shown as min verbosity is inferred by default") local_cli.info("Info from function") cli.min_verbose_level=0 cli.set_prefix("MAIN") cli.log("Shown as min verbosity is DEBUG") function_that_has_no_idea_about_main_program() .. warning:: The actual colors and symbols below may vary depending on your terminal and its settings. .. raw:: html
LOG [MAIN] | Shown as min verbosity is DEBUG
LOG [FUNC] | Debug will be shown as min verbosity is inferred by default
i [FUNC] | Info from function