# msl.qt.loop_until_abort module¶

Repeatedly perform a task until aborted by the user.

class msl.qt.loop_until_abort.LoopUntilAbort(*, loop_delay=0, max_iterations=None, single_shot=False, title=None, bg_color='#DFDFDF', text_color='#20548B', font_family='Helvetica', font_size=14)[source]

Bases: object

Repeatedly perform a task until aborted by the user.

This class provides an interface to show the status of a task (e.g., read a sensor value and write the value to a file) that you want to perform for an unknown period of time (e.g., during lunch or overnight) and you want to stop the task whenever you return. It can be regarded as a way to tell your program to “get as much data as possible until I get back”.

The following example illustrates how to repeatedly write data to a file in a loop:

"""
Example script to repeatedly write data to a file until aborted by the user.
"""
import tempfile

from msl.qt import (
prompt,
LoopUntilAbort
)

class LoopExample(LoopUntilAbort):

def __init__(self):
"""Initialize the LoopUntilAbort class and create a file to write data to.

Use a 250 ms delay between successive calls to the loop method.
"""
super(LoopExample, self).__init__(loop_delay=250)

self.output_path = tempfile.gettempdir() + '/msl-qt-loop-until-abort.txt'
self.f = open(self.output_path, 'w')
self.f.write('Started at {}\n'.format(self.current_time))

def loop(self):
"""Overrides LoopUntilAbort.loop()

This method gets called repeatedly in a loop (every loop_delay ms).
"""
self.f.write('Iteration: {}\n'.format(self.iteration))
self.f.write('Elapsed time: {}\n'.format(self.elapsed_time))
self.set_label_text('The current time is\n' + str(self.current_time))

def cleanup(self):
"""Overrides LoopUntilAbort.cleanup()

This method gets called when the LoopExample window is closing.
"""
self.f.write('Stopped at {}\n'.format(self.current_time))
self.f.close()
msg = 'The data was save to\n{}\n\n... in case you want to look at it'
prompt.information(msg.format(self.output_path))

def main():
loop = LoopExample()
loop.start()

if __name__ == '__main__':
main()


Examples

To run the above example enter the following:

>>> from msl.examples.qt import LoopExample
>>> loop = LoopExample()
>>> loop.start()


Another example which uses single-shot mode:

>>> from msl.examples.qt import LoopExampleSleep
>>> loop = LoopExampleSleep()
>>> loop.start()

Parameters: loop_delay (int, optional) – The delay time, in milliseconds, to wait between successive calls to the loop() method. For example, if loop_delay = 0 then there is no time delay between successive calls to the loop() method; if loop_delay = 1000 then wait 1 second between successive calls to the loop() method. max_iterations (int, optional) – The maximum number of times to call the loop() method. The default value is None, which means to loop until the user aborts the program. single_shot (bool, optional) – Whether to call the loop() method once (and only once). If you specify the value to be True then you must call the loop_once() method in the subclass whenever you want to run the loop() one more time. This is useful if the loop() depends on external factors (e.g., waiting for an oscilloscope to download a trace after a trigger event) and the amount of time that the loop() requires to complete is not known. title (str, optional) – The text to display in the title bar of the QtWidgets.QMainWindow. If None then uses the name of the subclass as the title. bg_color (QtGui.QColor, optional) – The background color of the QtWidgets.QMainWindow. Can be any data type and value that the constructor of a QtGui.QColor accepts. text_color (QtGui.QColor, optional) – The color of the Elapsed time and Iteration text.Can be any data type and value that the constructor of a QtGui.QColor accepts. font_family (QtGui.QFont, optional) – The font family to use for the text. Can be any value that the constructor of a QtGui.QFont accepts. font_size (int, optional) – The font size of the text.
cleanup()[source]

This method gets called when the QtWidgets.QMainWindow is closing.

You can override this method to properly cleanup any tasks. For example, to close a file that is open.

current_time

The current time.

elapsed_time

The elapsed time since the loop() started.

iteration

The number of times that the loop() method has been called.

Type: int
loop()[source]

The task to perform in a loop.

Attention

You MUST override this method.

loop_delay

The time delay, in milliseconds, between successive calls to the loop().

Type: int
loop_once()[source]

Run the loop() method once.

This method should be called if the LoopUntilAbort class was initialized with single_shot = True, in order to run the loop() method one more time.

loop_timer

The reference to the loop()’s timer.

main_window

The reference to the main window.

max_iterations

The maximum number of times that the loop() will be called.

Type: int or None
set_label_text(text)[source]

Update the text of the label that the user has access to.

Parameters: text (str) – The text to display in the user-accessible label.

user_label()
For the reference to the QtWidgets.QLabel object.
set_status_bar_text(text)[source]

Set the text to display in the status bar of the QtWidgets.QMainWindow.

Parameters: text (str) – The text to display in the status bar of the QtWidgets.QMainWindow.
start()[source]

Show the QtWidgets.QMainWindow and start looping.

start_time

The time when the loop() started.

user_label

The reference to the label object that the user can modify the text of.