Utilities¶
Common functions that are used in multiple files in the application.
Logging¶
-
class mundo.utils.logging.CustomFormatter(
fmt=None,
datefmt=None,
style='%',
validate=True,
*,
defaults=None
)¶ Custom formatter that supports colored outputs.
- grey = '\x1b[38;20m'¶
- yellow = '\x1b[33;20m'¶
- red = '\x1b[31;20m'¶
- bold_red = '\x1b[31;1m'¶
- green = '\x1b[92m'¶
- reset = '\x1b[0m'¶
- msg_format = '[%(asctime)s %(levelname)-7s]:%(message)-100s (%(funcName)s:%(lineno)s)'¶
- msg_format_gui = '[%(asctime)s %(levelname)-7s]:%(message)s (%(funcName)s:%(lineno)s)'¶
- FORMATS = {10: '\x1b[38;20m[%(asctime)s %(levelname)-7s]:%(message)-100s (%(funcName)s:%(lineno)s)\x1b[0m', 20: '\x1b[92m[%(asctime)s %(levelname)-7s]:%(message)-100s (%(funcName)s:%(lineno)s)\x1b[0m', 30: '\x1b[33;20m[%(asctime)s %(levelname)-7s]:%(message)-100s (%(funcName)s:%(lineno)s)\x1b[0m', 40: '\x1b[31;20m[%(asctime)s %(levelname)-7s]:%(message)-100s (%(funcName)s:%(lineno)s)\x1b[0m', 50: '\x1b[31;1m[%(asctime)s %(levelname)-7s]:%(message)-100s (%(funcName)s:%(lineno)s)\x1b[0m'}¶
- GUI_FORMATS = {10: ('[%(asctime)s %(levelname)-7s]:%(message)s (%(funcName)s:%(lineno)s)', '#555555'), 20: ('[%(asctime)s %(levelname)-7s]:%(message)s (%(funcName)s:%(lineno)s)', '#006637'), 30: ('[%(asctime)s %(levelname)-7s]:%(message)s (%(funcName)s:%(lineno)s)', '#725600'), 40: ('[%(asctime)s %(levelname)-7s]:%(message)s (%(funcName)s:%(lineno)s)', '#B30000')}¶
-
format(
record: logging.LogRecord,
gui_format: bool = False
) str ¶ Formats the logging-messages, e.g. colour. :param record: The message to format :param gui_format: If true, messages are formated for the GUI-logger.
Otherwise they are formatted for the internal logger
- Returns
The formatted log message
-
class mundo.utils.logging.GUILogHandler(
update_signal: PySide2.QtCore.SignalInstance
)¶ A logging handler for emitting messages to a debug log widget.
-
__init__(
update_signal: PySide2.QtCore.SignalInstance
) None ¶ Creates a new log handler for the GUI.
- Parameters
update_signal – The signal to emit when a new message has been added
-
emit(
record: logging.LogRecord
)¶ Method to send custom messages to the prompt
- Parameters
record – Custom message to send to the logging-prompt
-
__init__(
-
mundo.utils.logging.setup_logging(
storage: mundo.api.storage.Storage,
debug: bool = False,
filename: str = 'mundo.log'
) None ¶ Setup global application logging. This should be called only once at application startup.
- Parameters
storage – Storage manager for Mundo
debug – If debug output should be shown in stdout
Tools¶
-
mundo.utils.tools.extends_base_class(
ref_class,
base_class
) bool ¶ Checks if ref_class extends (inherits) from base_class.
- Parameters
ref_class – The class type to check
base_class – The base class type to compare to
- Returns
True if ref_class extends base_class, and ref_class is not equal to base_class. False otherwise
-
mundo.utils.tools.clear_layout(
layout: PySide2.QtWidgets.QBoxLayout
) None ¶ Recursively removes all widgets from a Qt layout widget, including the widgets from any child layouts.
- Parameters
layout – The layout to clear
-
mundo.utils.tools.select_config_file(
storage: mundo.api.storage.Storage,
sub_dir: str = '',
caption: Optional[str] = None,
save=False
) Optional[str] ¶ Allows the user to select a file from the base_dir folder.
- Parameters
storage – The storage instance
sub_dir – The sub directory of the main export directory to show in the file dialog
caption – The caption of the file dialog
- Returns
The filename of the selected file, or None if no file was selected
- mundo.utils.tools.is_testing() bool ¶
Checks if the code is being executed in a testing environment.
- Returns
True if the code is running in a testing environment. False otherwise
-
mundo.utils.tools.first_in_dict(
dictionary: Dict[Any, Any]
) Optional[Tuple[str, Any]] ¶ Returns the key and value of the first item in dictionary.
- Parameters
dictionary – The dictionary to get the first key and value of
- Returns
Tuple containing the key and value of the first item in the dictionary
-
mundo.utils.tools.nth_key_in_dict(
dictionary: Dict[Any, Any],
index: int
) Any ¶ Gets the key given the index it is position in the dictionary.
- Parameters
dictionary – The dictionary to get the key by index.
index – The index of the key in dictionary
- Returns
The key of the given index or None if it does not exist.
-
mundo.utils.tools.run_in_parallel(
data: List[Tuple[Callable, Tuple]]
) Optional[List[Any]] ¶ Executes method with specified arguments for each item in data in separate threads.
- Parameters
data – List of tuples containing a callable method and a tuple of method arguments
- Returns
True if the all methods was executed successfully. False otherwise
-
mundo.utils.tools.mm_to_nm(
mm: float
) float ¶ Converts milimeters to nanometers.
- Parameters
mm – The value in milimeters
- Returns
The value in nanometers
-
mundo.utils.tools.nm_to_mm(
nm: float
) float ¶ Converts milimeters to nanometers.
- Parameters
nm – The value in nanometers
- Returns
The value is milimeters
-
mundo.utils.tools.sort_controllers(
order: List[Optional[str]],
controllers: List[mundo.api.controller.BaseController],
key: Callable[[mundo.api.controller.BaseController], Optional[str]]
) Optional[List[mundo.api.controller.BaseController]] ¶ Sorts a list of controller based on the order of the items in order. The property of the controller that should be used in the comparison with the elements in order are extracted by calling key with the controller.
- Parameters
order – The list containing the order of the items that the resulting list should be ordered by
controllers – The list of controllers that should be sorted
key – A function used to extract the property of the controller that should be used in the comparison, e.g. controller.name, or controller.identifier
- Returns
A list containing the sorted controllers, or None if the list could not be sorted based on the elements in order, or if order or controllers contain None
-
mundo.utils.tools.to_input_decimals(
value: float
) int ¶ Calculates the required amount of decimals needed to represent value in a Qt widget. E.g. to represent 0.001, you need 4 decimals.
- Parameters
value – The value to calculate the number of decimals for
- Returns
The number of decimals that should be used when setting the decimal count using setDecimals
-
mundo.utils.tools.show_error_dialog(
parent: PySide2.QtWidgets.QWidget,
title: str,
error: str
) None ¶ Opens an error dialog with an error message. This is used to highlight critical errors to the user.
- Parameters
title – The error title
error – The error message to show
-
mundo.utils.tools.generate_calibration_plot(
sample_points: Tuple[List[float], List[float], List[float]],
calibration_points: Tuple[Tuple[float, float, float], Tuple[float, float, float], Tuple[float, float, float]],
travel_range: float
) matplotlib.figure.Figure ¶ Method for generating a scatter plot in a 3D space while also drawing out wireframes, the wireframe crosses each point.
- Parameters
sample_points – The sample points to scatter plot
travel_range – the travel_range that is set in the system to limit the placement of the mass spectrometer and the plot range
- Returns
A figure containing the plot, you can plot it by running fig.show()