pyloid 0.20.2__py3-none-any.whl → 0.20.2.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyloid/timer.py CHANGED
@@ -1,307 +1,307 @@
1
- from PySide6.QtCore import QTimer, QObject, Qt
2
-
3
- class PyloidTimer(QObject):
4
- def __init__(self):
5
- """
6
- Constructor for the PyloidTimer class.
7
-
8
- This class is based on PySide6's QTimer and allows for easy creation and management of various types of timers.
9
- """
10
- super().__init__()
11
- self.timers = {}
12
-
13
- def start_periodic_timer(self, interval, callback):
14
- """
15
- Starts a periodic timer.
16
-
17
- This function starts a timer that repeatedly executes the callback function at the given interval.
18
-
19
- Parameters
20
- ----------
21
- interval : int
22
- Interval in milliseconds
23
- callback : function
24
- Callback function to execute
25
-
26
- Returns
27
- -------
28
- int
29
- Timer ID
30
-
31
- Example
32
- -------
33
- ```python
34
- from pyloid.timer import PyloidTimer
35
-
36
- timer_manager = PyloidTimer()
37
-
38
- def print_hello():
39
- print("Hello!")
40
-
41
- # Start a timer that prints "Hello!" every 2 seconds
42
- timer_id = timer_manager.start_periodic_timer(2000, print_hello)
43
- ```
44
- """
45
- return self._create_timer(interval, callback, single_shot=False, auto_remove=False)
46
-
47
- def start_single_shot_timer(self, delay, callback):
48
- """
49
- Starts a single-shot timer.
50
-
51
- This function starts a timer that executes the callback function once after the given delay.
52
-
53
- Parameters
54
- ----------
55
- delay : int
56
- Delay in milliseconds
57
- callback : function
58
- Callback function to execute
59
-
60
- Returns
61
- -------
62
- int
63
- Timer ID
64
-
65
- Example
66
- -------
67
- ```python
68
- from pyloid.timer import PyloidTimer
69
-
70
- timer_manager = PyloidTimer()
71
-
72
- def delayed_message():
73
- print("5 seconds have passed!")
74
-
75
- # Start a single-shot timer that prints a message after 5 seconds
76
- timer_id = timer_manager.start_single_shot_timer(5000, delayed_message)
77
- ```
78
- """
79
- return self._create_timer(delay, callback, single_shot=True, auto_remove=True)
80
-
81
- def _create_timer(self, interval, callback, single_shot=False, auto_remove=False):
82
- timer = QTimer(self)
83
- timer.setInterval(interval)
84
- timer.setSingleShot(single_shot)
85
-
86
- if auto_remove or single_shot:
87
- timer.timeout.connect(lambda: self._timer_finished(callback, id(timer)))
88
- else:
89
- timer.timeout.connect(callback)
90
-
91
- timer.start()
92
-
93
- timer_id = id(timer)
94
- self.timers[timer_id] = timer
95
- return timer_id
96
-
97
- def _timer_finished(self, callback, timer_id):
98
- callback()
99
- self.stop_timer(timer_id)
100
-
101
- def stop_timer(self, timer_id):
102
- """
103
- Stops the timer with the specified ID.
104
-
105
- This function stops the timer corresponding to the given timer ID and removes it from the timer list.
106
-
107
- Parameters
108
- ----------
109
- timer_id : int
110
- ID of the timer to stop
111
-
112
- Example
113
- -------
114
- ```python
115
- from pyloid.timer import PyloidTimer
116
-
117
- timer_manager = PyloidTimer()
118
-
119
- # Stop the timer using its ID
120
- timer_manager.stop_timer(timer_id)
121
- ```
122
- """
123
- if timer_id in self.timers:
124
- self.timers[timer_id].stop()
125
- del self.timers[timer_id]
126
-
127
- def is_timer_active(self, timer_id):
128
- """
129
- Checks if the timer with the specified ID is active.
130
-
131
- This function returns whether the timer corresponding to the given timer ID is currently active.
132
-
133
- Parameters
134
- ----------
135
- timer_id : int
136
- ID of the timer to check
137
-
138
- Returns
139
- -------
140
- bool
141
- True if the timer is active, False otherwise
142
-
143
- Example
144
- -------
145
- ```python
146
- from pyloid.timer import PyloidTimer
147
-
148
- timer_manager = PyloidTimer()
149
-
150
- if timer_manager.is_timer_active(timer_id):
151
- print("The timer is still running.")
152
- else:
153
- print("The timer has stopped.")
154
- ```
155
- """
156
- return timer_id in self.timers and self.timers[timer_id].isActive()
157
-
158
- def get_remaining_time(self, timer_id):
159
- """
160
- Returns the remaining time of the timer with the specified ID.
161
-
162
- This function returns the remaining time of the timer corresponding to the given timer ID in milliseconds.
163
-
164
- Parameters
165
- ----------
166
- timer_id : int
167
- ID of the timer to check
168
-
169
- Returns
170
- -------
171
- int or None
172
- Remaining time in milliseconds, or None if the timer does not exist
173
-
174
- Example
175
- -------
176
- ```python
177
- from pyloid.timer import PyloidTimer
178
-
179
- timer_manager = PyloidTimer()
180
-
181
- remaining_time = timer_manager.get_remaining_time(timer_id)
182
- if remaining_time is not None:
183
- print(f"{remaining_time}ms remaining.")
184
- ```
185
- """
186
- if timer_id in self.timers:
187
- return self.timers[timer_id].remainingTime()
188
- return None
189
-
190
- def set_interval(self, timer_id, interval):
191
- """
192
- Sets the interval of the timer with the specified ID.
193
-
194
- This function sets a new interval for the timer corresponding to the given timer ID.
195
-
196
- Parameters
197
- ----------
198
- timer_id : int
199
- ID of the timer to set
200
- interval : int
201
- New interval in milliseconds
202
-
203
- Example
204
- -------
205
- ```python
206
- from pyloid.timer import PyloidTimer
207
-
208
- timer_manager = PyloidTimer()
209
-
210
- # Change the timer interval to 3 seconds
211
- timer_manager.set_interval(timer_id, 3000)
212
- ```
213
- """
214
- if timer_id in self.timers:
215
- self.timers[timer_id].setInterval(interval)
216
-
217
- def start_precise_periodic_timer(self, interval, callback):
218
- """
219
- Starts a precise periodic timer.
220
-
221
- This function starts a timer that repeatedly executes the callback function at precise intervals.
222
-
223
- Note
224
- ----
225
- Precise timers consume more CPU resources.
226
-
227
- Parameters
228
- ----------
229
- interval : int
230
- Interval in milliseconds
231
- callback : function
232
- Callback function to execute
233
-
234
- Returns
235
- -------
236
- int
237
- Timer ID
238
-
239
- Example
240
- -------
241
- ```python
242
- from pyloid.timer import PyloidTimer
243
-
244
- timer_manager = PyloidTimer()
245
-
246
- def precise_task():
247
- print("Executing precise task")
248
-
249
- # Start a precise periodic timer with a 100ms interval
250
- precise_timer_id = timer_manager.start_precise_periodic_timer(100, precise_task)
251
- ```
252
- """
253
- return self._create_timer_with_type(interval, callback, Qt.TimerType.PreciseTimer)
254
-
255
- def start_coarse_periodic_timer(self, interval, callback):
256
- """
257
- Starts a coarse periodic timer.
258
-
259
- This function starts a timer that repeatedly executes the callback function at coarse intervals.
260
-
261
- Note
262
- ----
263
- Coarse timers consume less CPU resources.
264
-
265
- Parameters
266
- ----------
267
- interval : int
268
- Interval in milliseconds
269
- callback : function
270
- Callback function to execute
271
-
272
- Returns
273
- -------
274
- int
275
- Timer ID
276
-
277
- Example
278
- -------
279
- ```python
280
- from pyloid.timer import PyloidTimer
281
-
282
- timer_manager = PyloidTimer()
283
-
284
- def coarse_task():
285
- print("Executing coarse task")
286
-
287
- # Start a coarse periodic timer with a 500ms interval
288
- coarse_timer_id = timer_manager.start_coarse_periodic_timer(500, coarse_task)
289
- ```
290
- """
291
- return self._create_timer_with_type(interval, callback, Qt.TimerType.CoarseTimer)
292
-
293
- def _create_timer_with_type(self, interval, callback, timer_type, auto_remove=False):
294
- timer = QTimer(self)
295
- timer.setInterval(interval)
296
- timer.setTimerType(timer_type)
297
-
298
- if auto_remove:
299
- timer.timeout.connect(lambda: self._timer_finished(callback, id(timer)))
300
- else:
301
- timer.timeout.connect(callback)
302
-
303
- timer.start()
304
-
305
- timer_id = id(timer)
306
- self.timers[timer_id] = timer
307
- return timer_id
1
+ from PySide6.QtCore import QTimer, QObject, Qt
2
+
3
+ class PyloidTimer(QObject):
4
+ def __init__(self):
5
+ """
6
+ Constructor for the PyloidTimer class.
7
+
8
+ This class is based on PySide6's QTimer and allows for easy creation and management of various types of timers.
9
+ """
10
+ super().__init__()
11
+ self.timers = {}
12
+
13
+ def start_periodic_timer(self, interval, callback):
14
+ """
15
+ Starts a periodic timer.
16
+
17
+ This function starts a timer that repeatedly executes the callback function at the given interval.
18
+
19
+ Parameters
20
+ ----------
21
+ interval : int
22
+ Interval in milliseconds
23
+ callback : function
24
+ Callback function to execute
25
+
26
+ Returns
27
+ -------
28
+ int
29
+ Timer ID
30
+
31
+ Example
32
+ -------
33
+ ```python
34
+ from pyloid.timer import PyloidTimer
35
+
36
+ timer_manager = PyloidTimer()
37
+
38
+ def print_hello():
39
+ print("Hello!")
40
+
41
+ # Start a timer that prints "Hello!" every 2 seconds
42
+ timer_id = timer_manager.start_periodic_timer(2000, print_hello)
43
+ ```
44
+ """
45
+ return self._create_timer(interval, callback, single_shot=False, auto_remove=False)
46
+
47
+ def start_single_shot_timer(self, delay, callback):
48
+ """
49
+ Starts a single-shot timer.
50
+
51
+ This function starts a timer that executes the callback function once after the given delay.
52
+
53
+ Parameters
54
+ ----------
55
+ delay : int
56
+ Delay in milliseconds
57
+ callback : function
58
+ Callback function to execute
59
+
60
+ Returns
61
+ -------
62
+ int
63
+ Timer ID
64
+
65
+ Example
66
+ -------
67
+ ```python
68
+ from pyloid.timer import PyloidTimer
69
+
70
+ timer_manager = PyloidTimer()
71
+
72
+ def delayed_message():
73
+ print("5 seconds have passed!")
74
+
75
+ # Start a single-shot timer that prints a message after 5 seconds
76
+ timer_id = timer_manager.start_single_shot_timer(5000, delayed_message)
77
+ ```
78
+ """
79
+ return self._create_timer(delay, callback, single_shot=True, auto_remove=True)
80
+
81
+ def _create_timer(self, interval, callback, single_shot=False, auto_remove=False):
82
+ timer = QTimer(self)
83
+ timer.setInterval(interval)
84
+ timer.setSingleShot(single_shot)
85
+
86
+ if auto_remove or single_shot:
87
+ timer.timeout.connect(lambda: self._timer_finished(callback, id(timer)))
88
+ else:
89
+ timer.timeout.connect(callback)
90
+
91
+ timer.start()
92
+
93
+ timer_id = id(timer)
94
+ self.timers[timer_id] = timer
95
+ return timer_id
96
+
97
+ def _timer_finished(self, callback, timer_id):
98
+ callback()
99
+ self.stop_timer(timer_id)
100
+
101
+ def stop_timer(self, timer_id):
102
+ """
103
+ Stops the timer with the specified ID.
104
+
105
+ This function stops the timer corresponding to the given timer ID and removes it from the timer list.
106
+
107
+ Parameters
108
+ ----------
109
+ timer_id : int
110
+ ID of the timer to stop
111
+
112
+ Example
113
+ -------
114
+ ```python
115
+ from pyloid.timer import PyloidTimer
116
+
117
+ timer_manager = PyloidTimer()
118
+
119
+ # Stop the timer using its ID
120
+ timer_manager.stop_timer(timer_id)
121
+ ```
122
+ """
123
+ if timer_id in self.timers:
124
+ self.timers[timer_id].stop()
125
+ del self.timers[timer_id]
126
+
127
+ def is_timer_active(self, timer_id):
128
+ """
129
+ Checks if the timer with the specified ID is active.
130
+
131
+ This function returns whether the timer corresponding to the given timer ID is currently active.
132
+
133
+ Parameters
134
+ ----------
135
+ timer_id : int
136
+ ID of the timer to check
137
+
138
+ Returns
139
+ -------
140
+ bool
141
+ True if the timer is active, False otherwise
142
+
143
+ Example
144
+ -------
145
+ ```python
146
+ from pyloid.timer import PyloidTimer
147
+
148
+ timer_manager = PyloidTimer()
149
+
150
+ if timer_manager.is_timer_active(timer_id):
151
+ print("The timer is still running.")
152
+ else:
153
+ print("The timer has stopped.")
154
+ ```
155
+ """
156
+ return timer_id in self.timers and self.timers[timer_id].isActive()
157
+
158
+ def get_remaining_time(self, timer_id):
159
+ """
160
+ Returns the remaining time of the timer with the specified ID.
161
+
162
+ This function returns the remaining time of the timer corresponding to the given timer ID in milliseconds.
163
+
164
+ Parameters
165
+ ----------
166
+ timer_id : int
167
+ ID of the timer to check
168
+
169
+ Returns
170
+ -------
171
+ int or None
172
+ Remaining time in milliseconds, or None if the timer does not exist
173
+
174
+ Example
175
+ -------
176
+ ```python
177
+ from pyloid.timer import PyloidTimer
178
+
179
+ timer_manager = PyloidTimer()
180
+
181
+ remaining_time = timer_manager.get_remaining_time(timer_id)
182
+ if remaining_time is not None:
183
+ print(f"{remaining_time}ms remaining.")
184
+ ```
185
+ """
186
+ if timer_id in self.timers:
187
+ return self.timers[timer_id].remainingTime()
188
+ return None
189
+
190
+ def set_interval(self, timer_id, interval):
191
+ """
192
+ Sets the interval of the timer with the specified ID.
193
+
194
+ This function sets a new interval for the timer corresponding to the given timer ID.
195
+
196
+ Parameters
197
+ ----------
198
+ timer_id : int
199
+ ID of the timer to set
200
+ interval : int
201
+ New interval in milliseconds
202
+
203
+ Example
204
+ -------
205
+ ```python
206
+ from pyloid.timer import PyloidTimer
207
+
208
+ timer_manager = PyloidTimer()
209
+
210
+ # Change the timer interval to 3 seconds
211
+ timer_manager.set_interval(timer_id, 3000)
212
+ ```
213
+ """
214
+ if timer_id in self.timers:
215
+ self.timers[timer_id].setInterval(interval)
216
+
217
+ def start_precise_periodic_timer(self, interval, callback):
218
+ """
219
+ Starts a precise periodic timer.
220
+
221
+ This function starts a timer that repeatedly executes the callback function at precise intervals.
222
+
223
+ Note
224
+ ----
225
+ Precise timers consume more CPU resources.
226
+
227
+ Parameters
228
+ ----------
229
+ interval : int
230
+ Interval in milliseconds
231
+ callback : function
232
+ Callback function to execute
233
+
234
+ Returns
235
+ -------
236
+ int
237
+ Timer ID
238
+
239
+ Example
240
+ -------
241
+ ```python
242
+ from pyloid.timer import PyloidTimer
243
+
244
+ timer_manager = PyloidTimer()
245
+
246
+ def precise_task():
247
+ print("Executing precise task")
248
+
249
+ # Start a precise periodic timer with a 100ms interval
250
+ precise_timer_id = timer_manager.start_precise_periodic_timer(100, precise_task)
251
+ ```
252
+ """
253
+ return self._create_timer_with_type(interval, callback, Qt.TimerType.PreciseTimer)
254
+
255
+ def start_coarse_periodic_timer(self, interval, callback):
256
+ """
257
+ Starts a coarse periodic timer.
258
+
259
+ This function starts a timer that repeatedly executes the callback function at coarse intervals.
260
+
261
+ Note
262
+ ----
263
+ Coarse timers consume less CPU resources.
264
+
265
+ Parameters
266
+ ----------
267
+ interval : int
268
+ Interval in milliseconds
269
+ callback : function
270
+ Callback function to execute
271
+
272
+ Returns
273
+ -------
274
+ int
275
+ Timer ID
276
+
277
+ Example
278
+ -------
279
+ ```python
280
+ from pyloid.timer import PyloidTimer
281
+
282
+ timer_manager = PyloidTimer()
283
+
284
+ def coarse_task():
285
+ print("Executing coarse task")
286
+
287
+ # Start a coarse periodic timer with a 500ms interval
288
+ coarse_timer_id = timer_manager.start_coarse_periodic_timer(500, coarse_task)
289
+ ```
290
+ """
291
+ return self._create_timer_with_type(interval, callback, Qt.TimerType.CoarseTimer)
292
+
293
+ def _create_timer_with_type(self, interval, callback, timer_type, auto_remove=False):
294
+ timer = QTimer(self)
295
+ timer.setInterval(interval)
296
+ timer.setTimerType(timer_type)
297
+
298
+ if auto_remove:
299
+ timer.timeout.connect(lambda: self._timer_finished(callback, id(timer)))
300
+ else:
301
+ timer.timeout.connect(callback)
302
+
303
+ timer.start()
304
+
305
+ timer_id = id(timer)
306
+ self.timers[timer_id] = timer
307
+ return timer_id