Public API¶
- 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 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 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
- 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.
- 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()
print(p)
- class sire.utils.Table(title=None, show_footer=False, show_edge=True)[source]¶
This a rich.table, with an additional “to_string()” function
- sire.utils.assert_imported(module)[source]¶
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 “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”)