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:

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.

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.

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:

database_module.py
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:

main_program.py
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:

main_program_fixed.py
from cliasi import cli

def main():
    cli.min_verbose_level = 2  # Only warnings and errors
    from database_module import initialize_database
    initialize_database()
database_module_fixed.py
from cliasi import Cliasi

cli: Cliasi = Cliasi("DB")

def initialize_database():
    cli.infer_settings()
    pass