Skip to content

Commit 9b9131f

Browse files
committed
Documented loop block
1 parent 4438595 commit 9b9131f

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

lantz/ui/blocks/loop.py

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# -*- coding: utf-8 -*-
2+
"""
3+
lantz.ui.loop
4+
~~~~~~~~~~~~~
5+
6+
A Loop backend and frontend.
7+
8+
:copyright: 2014 by Lantz Authors, see AUTHORS for more details.
9+
:license: BSD, see LICENSE for more details.
10+
"""
211

312
import time
413
import math
@@ -16,6 +25,24 @@ class StopMode(IntEnum):
1625

1726

1827
class Loop(Backend):
28+
"""The Loop backend allows you to execute task periodically.
29+
30+
Usage:
31+
32+
from lantz.ui.blocks import Loop, LoopUi
33+
34+
def measure(counter, iterations, overrun):
35+
print(counter, iterations, overrun)
36+
data = osci.measure()
37+
print(data)
38+
39+
app = Loop()
40+
41+
app.body = measure
42+
43+
start_gui_app(app, LoopUi)
44+
45+
"""
1946

2047
#: Signal emitted before starting a new iteration
2148
#: Parameters: loop counter, iterations, overrun
@@ -25,16 +52,38 @@ class Loop(Backend):
2552
#: The parameter is used to inform if the loop was canceled.
2653
loop_done = QtCore.Signal(bool)
2754

55+
#: The function to be called. It requires three parameters.
56+
#: counter - the iteration number
57+
#: iterations - total number of iterations
58+
#: overrun - a boolean indicating if the time required for the operation
59+
#: is longer than the interval.
60+
#: :type: (int, int, bool) -> None
61+
body = None
62+
2863
def __init__(self, **kwargs):
2964
super().__init__(**kwargs)
30-
self.body = None
3165
self._active = False
3266
self._internal_func = None
3367

3468
def stop(self):
69+
"""Request the scanning to be stop.
70+
Will stop when the current iteration is finished.
71+
"""
3572
self._active = False
3673

3774
def start(self, body, interval=0, iterations=0, timeout=0):
75+
"""Request the scanning to be started.
76+
77+
:param body: function to be called at each iteration.
78+
If None, the class body will be used.
79+
:param interval: interval between starts of the iteration.
80+
If the body takes too long, the iteration will
81+
be as fast as possible and the overrun flag will be True
82+
:param iterations: number of iterations
83+
:param timeout: total time in seconds that the scanning will take.
84+
If overdue, the scanning will be stopped.
85+
If 0, there is no timeout.
86+
"""
3887
self._active = True
3988
body = body or self.body
4089

@@ -48,6 +97,7 @@ def internal(counter, overrun=False, schedule=QtCore.QTimer.singleShot):
4897
body(counter, iterations, overrun)
4998

5099
if iterations and counter + 1 == iterations:
100+
self._active = False
51101
self.loop_done.emit(False)
52102
return
53103
elif not self._active:
@@ -65,12 +115,18 @@ def internal(counter, overrun=False, schedule=QtCore.QTimer.singleShot):
65115

66116

67117
class LoopUi(Frontend):
118+
"""The Loop frontend provides a GUI for the Rich Backend
119+
"""
68120

69121
gui = 'loop.ui'
70122

71123
auto_connect = False
72124

125+
#: Signal emitted when a start is requested.
126+
#: The parameters are None, interval, iterations, duration
73127
request_start = QtCore.Signal(object, object, object, object)
128+
129+
#: Signal emitted when a stop is requested.
74130
request_stop = QtCore.Signal()
75131

76132
def connect_backend(self):

0 commit comments

Comments
 (0)