pyloid 0.26.3__py3-none-any.whl → 0.26.5__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,426 @@
1
- from PySide6.QtCore import QTimer, QObject, Qt
1
+ from PySide6.QtCore import (
2
+ QTimer,
3
+ QObject,
4
+ Qt,
5
+ )
6
+
2
7
 
3
8
  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
9
+ def __init__(
10
+ self,
11
+ ):
12
+ """
13
+ Constructor for the PyloidTimer class.
14
+
15
+ This class is based on PySide6's QTimer and allows for easy creation and management of various types of timers.
16
+ """
17
+ super().__init__()
18
+ self.timers = {}
19
+
20
+ def start_periodic_timer(
21
+ self,
22
+ interval,
23
+ callback,
24
+ ):
25
+ """
26
+ Starts a periodic timer.
27
+
28
+ This function starts a timer that repeatedly executes the callback function at the given interval.
29
+
30
+ Parameters
31
+ ----------
32
+ interval : int
33
+ Interval in milliseconds
34
+ callback : function
35
+ Callback function to execute
36
+
37
+ Returns
38
+ -------
39
+ int
40
+ Timer ID
41
+
42
+ Example
43
+ -------
44
+ ```python
45
+ from pyloid.timer import (
46
+ PyloidTimer,
47
+ )
48
+
49
+ timer_manager = PyloidTimer()
50
+
51
+
52
+ def print_hello():
53
+ print('Hello!')
54
+
55
+
56
+ # Start a timer that prints "Hello!" every 2 seconds
57
+ timer_id = timer_manager.start_periodic_timer(
58
+ 2000,
59
+ print_hello,
60
+ )
61
+ ```
62
+ """
63
+ return self._create_timer(
64
+ interval,
65
+ callback,
66
+ single_shot=False,
67
+ auto_remove=False,
68
+ )
69
+
70
+ def start_single_shot_timer(
71
+ self,
72
+ delay,
73
+ callback,
74
+ ):
75
+ """
76
+ Starts a single-shot timer.
77
+
78
+ This function starts a timer that executes the callback function once after the given delay.
79
+
80
+ Parameters
81
+ ----------
82
+ delay : int
83
+ Delay in milliseconds
84
+ callback : function
85
+ Callback function to execute
86
+
87
+ Returns
88
+ -------
89
+ int
90
+ Timer ID
91
+
92
+ Example
93
+ -------
94
+ ```python
95
+ from pyloid.timer import (
96
+ PyloidTimer,
97
+ )
98
+
99
+ timer_manager = PyloidTimer()
100
+
101
+
102
+ def delayed_message():
103
+ print('5 seconds have passed!')
104
+
105
+
106
+ # Start a single-shot timer that prints a message after 5 seconds
107
+ timer_id = timer_manager.start_single_shot_timer(
108
+ 5000,
109
+ delayed_message,
110
+ )
111
+ ```
112
+ """
113
+ return self._create_timer(
114
+ delay,
115
+ callback,
116
+ single_shot=True,
117
+ auto_remove=True,
118
+ )
119
+
120
+ def _create_timer(
121
+ self,
122
+ interval,
123
+ callback,
124
+ single_shot=False,
125
+ auto_remove=False,
126
+ ):
127
+ timer = QTimer(self)
128
+ timer.setInterval(interval)
129
+ timer.setSingleShot(single_shot)
130
+
131
+ if auto_remove or single_shot:
132
+ timer.timeout.connect(
133
+ lambda: self._timer_finished(
134
+ callback,
135
+ id(timer),
136
+ )
137
+ )
138
+ else:
139
+ timer.timeout.connect(callback)
140
+
141
+ timer.start()
142
+
143
+ timer_id = id(timer)
144
+ self.timers[timer_id] = timer
145
+ return timer_id
146
+
147
+ def _timer_finished(
148
+ self,
149
+ callback,
150
+ timer_id,
151
+ ):
152
+ callback()
153
+ self.stop_timer(timer_id)
154
+
155
+ def stop_timer(
156
+ self,
157
+ timer_id,
158
+ ):
159
+ """
160
+ Stops the timer with the specified ID.
161
+
162
+ This function stops the timer corresponding to the given timer ID and removes it from the timer list.
163
+
164
+ Parameters
165
+ ----------
166
+ timer_id : int
167
+ ID of the timer to stop
168
+
169
+ Example
170
+ -------
171
+ ```python
172
+ from pyloid.timer import (
173
+ PyloidTimer,
174
+ )
175
+
176
+ timer_manager = PyloidTimer()
177
+
178
+ # Stop the timer using its ID
179
+ timer_manager.stop_timer(timer_id)
180
+ ```
181
+ """
182
+ if timer_id in self.timers:
183
+ self.timers[timer_id].stop()
184
+ del self.timers[timer_id]
185
+
186
+ def is_timer_active(
187
+ self,
188
+ timer_id,
189
+ ):
190
+ """
191
+ Checks if the timer with the specified ID is active.
192
+
193
+ This function returns whether the timer corresponding to the given timer ID is currently active.
194
+
195
+ Parameters
196
+ ----------
197
+ timer_id : int
198
+ ID of the timer to check
199
+
200
+ Returns
201
+ -------
202
+ bool
203
+ True if the timer is active, False otherwise
204
+
205
+ Example
206
+ -------
207
+ ```python
208
+ from pyloid.timer import (
209
+ PyloidTimer,
210
+ )
211
+
212
+ timer_manager = PyloidTimer()
213
+
214
+ if timer_manager.is_timer_active(timer_id):
215
+ print('The timer is still running.')
216
+ else:
217
+ print('The timer has stopped.')
218
+ ```
219
+ """
220
+ return timer_id in self.timers and self.timers[timer_id].isActive()
221
+
222
+ def get_remaining_time(
223
+ self,
224
+ timer_id,
225
+ ):
226
+ """
227
+ Returns the remaining time of the timer with the specified ID.
228
+
229
+ This function returns the remaining time of the timer corresponding to the given timer ID in milliseconds.
230
+
231
+ Parameters
232
+ ----------
233
+ timer_id : int
234
+ ID of the timer to check
235
+
236
+ Returns
237
+ -------
238
+ int or None
239
+ Remaining time in milliseconds, or None if the timer does not exist
240
+
241
+ Example
242
+ -------
243
+ ```python
244
+ from pyloid.timer import (
245
+ PyloidTimer,
246
+ )
247
+
248
+ timer_manager = PyloidTimer()
249
+
250
+ remaining_time = timer_manager.get_remaining_time(timer_id)
251
+ if remaining_time is not None:
252
+ print(f'{remaining_time}ms remaining.')
253
+ ```
254
+ """
255
+ if timer_id in self.timers:
256
+ return self.timers[timer_id].remainingTime()
257
+ return None
258
+
259
+ def set_interval(
260
+ self,
261
+ timer_id,
262
+ interval,
263
+ ):
264
+ """
265
+ Sets the interval of the timer with the specified ID.
266
+
267
+ This function sets a new interval for the timer corresponding to the given timer ID.
268
+
269
+ Parameters
270
+ ----------
271
+ timer_id : int
272
+ ID of the timer to set
273
+ interval : int
274
+ New interval in milliseconds
275
+
276
+ Example
277
+ -------
278
+ ```python
279
+ from pyloid.timer import (
280
+ PyloidTimer,
281
+ )
282
+
283
+ timer_manager = PyloidTimer()
284
+
285
+ # Change the timer interval to 3 seconds
286
+ timer_manager.set_interval(
287
+ timer_id,
288
+ 3000,
289
+ )
290
+ ```
291
+ """
292
+ if timer_id in self.timers:
293
+ self.timers[timer_id].setInterval(interval)
294
+
295
+ def start_precise_periodic_timer(
296
+ self,
297
+ interval,
298
+ callback,
299
+ ):
300
+ """
301
+ Starts a precise periodic timer.
302
+
303
+ This function starts a timer that repeatedly executes the callback function at precise intervals.
304
+
305
+ Note
306
+ ----
307
+ Precise timers consume more CPU resources.
308
+
309
+ Parameters
310
+ ----------
311
+ interval : int
312
+ Interval in milliseconds
313
+ callback : function
314
+ Callback function to execute
315
+
316
+ Returns
317
+ -------
318
+ int
319
+ Timer ID
320
+
321
+ Example
322
+ -------
323
+ ```python
324
+ from pyloid.timer import (
325
+ PyloidTimer,
326
+ )
327
+
328
+ timer_manager = PyloidTimer()
329
+
330
+
331
+ def precise_task():
332
+ print('Executing precise task')
333
+
334
+
335
+ # Start a precise periodic timer with a 100ms interval
336
+ precise_timer_id = timer_manager.start_precise_periodic_timer(
337
+ 100,
338
+ precise_task,
339
+ )
340
+ ```
341
+ """
342
+ return self._create_timer_with_type(
343
+ interval,
344
+ callback,
345
+ Qt.TimerType.PreciseTimer,
346
+ )
347
+
348
+ def start_coarse_periodic_timer(
349
+ self,
350
+ interval,
351
+ callback,
352
+ ):
353
+ """
354
+ Starts a coarse periodic timer.
355
+
356
+ This function starts a timer that repeatedly executes the callback function at coarse intervals.
357
+
358
+ Note
359
+ ----
360
+ Coarse timers consume less CPU resources.
361
+
362
+ Parameters
363
+ ----------
364
+ interval : int
365
+ Interval in milliseconds
366
+ callback : function
367
+ Callback function to execute
368
+
369
+ Returns
370
+ -------
371
+ int
372
+ Timer ID
373
+
374
+ Example
375
+ -------
376
+ ```python
377
+ from pyloid.timer import (
378
+ PyloidTimer,
379
+ )
380
+
381
+ timer_manager = PyloidTimer()
382
+
383
+
384
+ def coarse_task():
385
+ print('Executing coarse task')
386
+
387
+
388
+ # Start a coarse periodic timer with a 500ms interval
389
+ coarse_timer_id = timer_manager.start_coarse_periodic_timer(
390
+ 500,
391
+ coarse_task,
392
+ )
393
+ ```
394
+ """
395
+ return self._create_timer_with_type(
396
+ interval,
397
+ callback,
398
+ Qt.TimerType.CoarseTimer,
399
+ )
400
+
401
+ def _create_timer_with_type(
402
+ self,
403
+ interval,
404
+ callback,
405
+ timer_type,
406
+ auto_remove=False,
407
+ ):
408
+ timer = QTimer(self)
409
+ timer.setInterval(interval)
410
+ timer.setTimerType(timer_type)
411
+
412
+ if auto_remove:
413
+ timer.timeout.connect(
414
+ lambda: self._timer_finished(
415
+ callback,
416
+ id(timer),
417
+ )
418
+ )
419
+ else:
420
+ timer.timeout.connect(callback)
421
+
422
+ timer.start()
423
+
424
+ timer_id = id(timer)
425
+ self.timers[timer_id] = timer
426
+ return timer_id