4.6.1. basf2.utils - Helper functions for printing basf2 objects

This modules contains some utility functions used by basf2, mainly for printing things.

basf2.utils.get_terminal_width()[source]

Returns width of terminal in characters, or 80 if unknown.

basf2.utils.is_mod_function(mod, func)[source]

Return true if func is a function and defined in the module mod

basf2.utils.list_functions(mod)[source]

Returns list of function names defined in the given Python module.

basf2.utils.pretty_print_description_list(rows)[source]

Given a list of 2-tuples, print a nicely formatted description list. Rows with only one entry are interpreted as sub-headings

basf2.utils.pretty_print_module(module, module_name, replacements=None)[source]

Pretty print the contents of a python module. It will print all the functions defined in the given module to the console

Parameters
  • module – instance of the module or name with which it can be found in sys.modules

  • module_name – readable module name

  • replacements (dict) – dictionary containing text replacements: Every occurrence of any key in the function signature will be replaced by its value

basf2.utils.pretty_print_table(table, column_widths, first_row_is_heading=True, transform=None, min_flexible_width=10, *, hline_formatter=None)[source]

Pretty print a given table, by using available terminal size and word wrapping fields as needed.

Parameters
  • table – A 2d list of table fields. Each row must have the same length.

  • column_width

    list of column widths, needs to be of same length as rows in ‘table’. Available fields are

    -n

    as needed, up to n characters, word wrap if longer

    0

    as long as needed, no wrapping

    n

    n characters (fixed)

    *

    use all available space, good for description fields. If more than one column has a * they all get equal width

    +

    use all available space but at least the actual width. Only useful to make the table span the full width of the terminal

    The column width can also start with >, < or ^ in which case it will be right, left or center aligned.

  • first_row_is_heading – header specifies if we should take the first row as table header and offset it a bit

  • transform

    either None or a callback function which takes three arguments

    1. the elements of the row as a list

    2. second the width of each column (without separator)

    3. the preformatted text line.

    It should return a string representing the final line to be printed.

  • min_flexible_width – the minimum amount of characters for every column marked with *

  • hline_formatter

    A callable function to format horizontal lines (above and below the table header). Should be a callback with one parameter for the total width of the table in characters and return a string that is the horizontal line. If None is returned no line is printed.

    If argument is not given or given as None the default of printing ‘-’ signs are printed over the whole table width is used.

Changed in version after: release 5 Added support for column alignment

basf2.utils.print_all_modules(moduleList, package='')[source]

Loop over the list of available modules, register them and print their information

basf2.utils.print_params(module, print_values=True, shared_lib_path=None)[source]

This function prints parameter information

Parameters
  • module – Print the parameter information of this module

  • print_values – Set it to True to print the current values of the parameters

  • shared_lib_path – The path of the shared library from which the module was loaded

basf2.utils.print_path(path, defaults=False, description=False, indentation=0, title=True)[source]

This function prints the modules in the given path and the module parameters. Parameters that are not set by the user are suppressed by default.

Parameters
  • defaults – Set it to True to print also the parameters with default values

  • description – Set to True to print the descriptions of modules and parameters

  • indentation – an internal parameter to indent the whole output (needed for outputting sub-paths)

  • title – show the title string or not (defaults to True)