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.
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:
set_prefix()- to set the prefix for every message from this instanceinfer_settings()- to infer settings like the ones below from the global instance, see Global inferenceenable_colors- whether to use colored output for this instancemax_dead_space- maximum number of empty space between aligned messages for this instance. See max_dead_space parametermin_verbose_level- verbosity level for this instancemessages_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¶
min_verbose_level and
messages_stay_in_one_line
are inferred from the global (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.
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.
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
Common mistakes¶
Please beware that if you have something like this in one of your files:
from cliasi import Cliasi
cli = Cliasi("DB")
def initialize_database():
pass
And maybe get the verbosity level from some config file / as arguments and set the level after importing the module like this:
from cliasi import cli
from database_module import initialize_database
def main():
cli.min_verbose_level = 2 # Only warnings and errors
initialize_database()
The Cliasi instance in database will not infer the verbosity level from the global instance as it is created before the global instance’s verbosity level is set.
To avoid this, either set the verbosity level before importing any modules that create their own Cliasi instances or create Cliasi instances only in functions / after the global instance’s settings have been set.
You can also use the infer_settings() method to manually infer the settings from the global instance.
Below are fixed versions of the above code snippets:
from cliasi import cli
def main():
cli.min_verbose_level = 2 # Only warnings and errors
from database_module import initialize_database
initialize_database()
from cliasi import Cliasi
cli: Cliasi = Cliasi("DB")
def initialize_database():
cli.infer_settings()
pass