Public API#

View Module Index

class sire.utils.Console[source]#

This is a singleton class that provides access to printing and logging functions to the console. This uses ‘rich’ for rich console printing

static debug(text: str, variables: List[any] = None, level: int = None, markdown: bool = False, **kwargs)[source]#

Print a debug string to the console. This will only be printed if debugging is enabled. You can also print the values of variables by passing them as a list to ‘variables’

static debugging_enabled(level: int = None)[source]#

Return whether debug output is enabled (optionally for the specified level) - if not, then anything sent to ‘debug’ (for that level) is not printed

static error(text: str, *args, **kwargs)[source]#

Print an error to the console

static info(text: str, *args, **kwargs)[source]#

Print an info section to the console

static panel(text: str, markdown: bool = False, width=None, padding: bool = True, style: str = None, expand=True, *args, **kwargs)[source]#

Print within a panel to the console

static print(text: str, markdown: bool = False, style: str = None, markup: bool = None, *args, **kwargs)[source]#

Print to the console

static print_exception()[source]#

Print the current-handled exception

static redirect_output(outdir: str, auto_bzip: bool = True)[source]#

Redirect all output and error to the directory ‘outdir’

static rule(title: str = None, style=None, **kwargs)[source]#

Write a rule across the screen with optional title

static save(file: str | IO)[source]#

Save the accumulated printing to the console to ‘file’. This can be a file or a filehandle. The buffer is cleared after saving

static set_debugging_enabled(enabled, level=None)[source]#

Switch on or off debugging output

static set_theme(theme)[source]#

Set the theme used for the console - this should be one of the themes in metawards.themes

static supports_emojis()[source]#

Return whether or not you can print emojis to this console

static warning(text: str, *args, **kwargs)[source]#

Print a warning to the console

class sire.utils.NullProfiler(name: str = None, parent=None)[source]#

This is a null profiler that does nothing

class sire.utils.Profiler(name: str = None, parent=None)[source]#

This is a simple profiling class that supports manual instrumenting of the code. It is used for sub-function profiling.

child_total() float[source]#

Return the sum of time spent in the child profilers

is_null() bool[source]#

Return whether this is a null profiler

name() str[source]#

Return the name of this profiler

start(name: str)[source]#

Start profiling the section called ‘name’. This returns the updated profiler, e.g.

p = Profiler()

p = p.start(“long_loop”)

# run some code

p = p.end()



Stop profiling. This records the end time and returns the parent profiler (if we have one)

total() float[source]#

Return the amount of time that was recorded for this profiler in milliseconds (accurate to ~nanoseconds)

class sire.utils.Table(title=None, show_footer=False, show_edge=True)[source]#

This a rich.table, with an additional “to_string()” function

add_column(header, justify='center', style=None, no_wrap=False, footer=None)[source]#

Add a column called ‘header’, with specified justification, style and wrapping


Add the passed row of data to the table


Return this table rendered to a string


Assert that the passed module has indeed been imported. This will raise a ModuleNotFoundError if the module has not been imported, and has instead been stubbed.

sire.utils.have_imported(module) bool[source]#

Return whether or not the passed module has indeed been imported (and thus is not stubbed).

sire.utils.try_import(name, package_registry={}, version=None)[source]#

Try to import the module called ‘name’, returning the loaded module as an argument. If the module is not available, then it looks up the name of the package to install using “package_registry” (or if this is not available, using just the name of the module). This will then be installed using “mamba”, then “conda” (first one that works will return).

For example, use this via

sys = try_import(“sys”) mdtraj = try_import(“mdtraj”)

Note that you can also rename modules, e.g. by using

md = try_import(“mdtraj”)

Note that you should use try_import_from if you want only specific symbols, e.g.

(argv, stdout) = try_import_from(“sys”, [“argv”,”stdout”])

This will return a _ModuleStub if this package cannot be imported. The _ModuleStub will raise a ModuleNotFoundError if any functonality from the module is used.

sire.utils.try_import_from(name, fromlist, package_registry={}, version=None)[source]#

Try to import from the module called ‘name’ the passed symbol (or list of symbols) contained in ‘fromlist’, returning the symbol (or list of symbols).

If the module cannot be loaded, then the package containing the module is looked up in ‘module_to_package’ (or just guessed from the name if it does not exist in ‘module_to_package’. An attempt is made to load the package, using first conda, then pip, then easy_install.

Example usage:

mol = try_import_from(“sire”, “mol”) (argv,stdout = try_import_from(“sys”, [“argv”, “stdout”]) ut = try_import_from(“mdtraj”, “utils”)