@@ -111,12 +111,13 @@ class Delay(WeakAutoUnload):
111
111
def __init__ (self , delay , callback , * args , ** kwargs ):
112
112
"""Initialize the delay.
113
113
114
- :param int delay: The delay in seconds.
114
+ :param float delay: The delay in seconds.
115
115
:param callback: A callable object that should be called after the
116
116
delay expired.
117
117
:param args: Arguments that should be passed to the callback.
118
118
:param kwargs: Keyword arguments that should be passed to the
119
119
callback.
120
+ :raises ValueError: If the given callback is not callable.
120
121
"""
121
122
if not callable (callback ):
122
123
raise ValueError ('Given callback is not callable.' )
@@ -151,18 +152,29 @@ def cancel(self):
151
152
152
153
@property
153
154
def running (self ):
154
- """Return True if the delay running."""
155
+ """Return True if the delay running.
156
+
157
+ :rtype: bool
158
+ """
155
159
return self in _delay_manager
156
160
157
161
@property
158
162
def time_remaining (self ):
163
+ """Return the remaining time (in seconds) until the Delay ends.
164
+
165
+ :rtype: float
166
+ """
159
167
if not self .running :
160
168
# TODO: what should we return here, or should we raise an error?
161
169
return None
162
170
return self .exec_time - time .time ()
163
171
164
172
@property
165
173
def time_elapsed (self ):
174
+ """Return the amount of time (in seconds) since the Delay started.
175
+
176
+ :rtype: float
177
+ """
166
178
if not self .running :
167
179
# TODO: what should we return here, or should we raise an error?
168
180
return None
@@ -188,7 +200,14 @@ class Repeat(AutoUnload):
188
200
"""Class used to create and call repeats."""
189
201
190
202
def __init__ (self , callback , * args , ** kwargs ):
191
- """Store all instance attributes."""
203
+ """Store all instance attributes.
204
+
205
+ :param callback: A callable object that should be called at the
206
+ end of each loop.
207
+ :param args: Arguments that should be passed to the callback.
208
+ :param kwargs: Keyword arguments that should be passed to the
209
+ callback.
210
+ """
192
211
# Store the base attributes
193
212
self .callback = callback
194
213
self .args = args
@@ -209,7 +228,15 @@ def __init__(self, callback, *args, **kwargs):
209
228
self ._loop_time = None
210
229
211
230
def start (self , interval , limit , execute_on_start = False ):
212
- """Start the repeat loop."""
231
+ """Start the repeat loop.
232
+
233
+ :param float interval: The time (in seconds) for each loop.
234
+ :param int limit: The maximum number of times to loop. If 0 is
235
+ passed, there is no limit, and the Repeat will loop indefinitely.
236
+ :param bool execute_on_start: Whether to execute the callback when
237
+ the Repeat is started. Note that this does not affect the 'limit'
238
+ as the number of loops will remain the same.
239
+ """
213
240
# Log the start message
214
241
listeners_tick_logger .log_debug (
215
242
'Repeat.start: <{0}> <{1}>' .format (interval , limit ))
@@ -344,7 +371,11 @@ def resume(self):
344
371
self ._delay = Delay (self ._loop_time , self ._execute )
345
372
346
373
def extend (self , adjustment ):
347
- """Add to the number of loops to be made."""
374
+ """Add to the number of loops to be made.
375
+
376
+ :param int adjustment: The number of loops to be added to the limit.
377
+ :raises ValueError: If given adjustment is not a positive integer.
378
+ """
348
379
# Log the extend message
349
380
listeners_tick_logger .log_debug ('Repeat.extend' )
350
381
@@ -359,7 +390,7 @@ def extend(self, adjustment):
359
390
return
360
391
361
392
# Was a positive integer given?
362
- if adjustment < 1 or not isinstance (adjustment , int ):
393
+ if not isinstance (adjustment , int ) or adjustment < 1 :
363
394
364
395
# Raise an error
365
396
raise ValueError ('Adjusted value must be a positive integer' )
@@ -368,7 +399,12 @@ def extend(self, adjustment):
368
399
self ._adjusted += adjustment
369
400
370
401
def reduce (self , adjustment ):
371
- """Reduce the number of loops to be made."""
402
+ """Reduce the number of loops to be made.
403
+
404
+ :param int adjustment: The number of loops to be removed from
405
+ the limit.
406
+ :raises ValueError: If given adjustment is not a positive integer.
407
+ """
372
408
# Log the reduce message
373
409
listeners_tick_logger .log_debug ('Repeat.reduce' )
374
410
@@ -383,7 +419,7 @@ def reduce(self, adjustment):
383
419
return
384
420
385
421
# Was a positive integer given?
386
- if adjustment < 1 or not isinstance (adjustment , int ):
422
+ if not isinstance (adjustment , int ) or adjustment < 1 :
387
423
388
424
# Raise an error
389
425
raise ValueError ('Adjusted value must be a positive integer' )
@@ -446,7 +482,10 @@ def _execute(self):
446
482
447
483
@property
448
484
def remaining_loops (self ):
449
- """Return the remaining number of loops in the repeat."""
485
+ """Return the remaining number of loops in the repeat.
486
+
487
+ :rtype: int
488
+ """
450
489
# Is there no limit?
451
490
if not self ._limit :
452
491
@@ -458,12 +497,18 @@ def remaining_loops(self):
458
497
459
498
@property
460
499
def loop_count (self ):
461
- """Return the current number of loops made in the repeat."""
500
+ """Return the current number of loops made in the repeat.
501
+
502
+ :rtype: int
503
+ """
462
504
return self ._count
463
505
464
506
@property
465
507
def loop_limit (self ):
466
- """Return the total number of loops to be made."""
508
+ """Return the total number of loops to be made.
509
+
510
+ :rtype: int
511
+ """
467
512
# Is there no limit?
468
513
if not self ._limit :
469
514
@@ -475,33 +520,53 @@ def loop_limit(self):
475
520
476
521
@property
477
522
def time_left (self ):
478
- """Return the remaining time till the end of the repeat."""
523
+ """Return the remaining time till the end of the repeat.
524
+
525
+ :rtype: float
526
+ """
479
527
return (
480
528
self .remaining_loops * self ._interval +
481
529
self .delay_time_remaining
482
530
)
483
531
484
532
@property
485
533
def time_elapsed (self ):
486
- """Return the elapsed time since the repeat started."""
534
+ """Return the elapsed time since the repeat started.
535
+
536
+ :rtype: float
537
+ """
487
538
return self .total_time - self .time_left
488
539
489
540
@property
490
541
def total_time (self ):
491
- """Return the total time it will take to complete the repeat."""
542
+ """Return the total time it will take to complete the repeat.
543
+
544
+ :rtype: float
545
+ """
492
546
return self .loop_limit * self ._interval
493
547
494
548
@property
495
549
def status (self ):
496
- """Return the status of the repeat."""
550
+ """Return the status of the repeat.
551
+
552
+ :rtype: RepeatStatus
553
+ """
497
554
return self ._status
498
555
499
556
@property
500
557
def delay_time_remaining (self ):
558
+ """Return the time remaining in the current loop.
559
+
560
+ :rtype: float
561
+ """
501
562
return self ._delay .time_remaining
502
563
503
564
@property
504
565
def delay_time_elapsed (self ):
566
+ """Return the time elapsed in the current loop.
567
+
568
+ :rtype: float
569
+ """
505
570
return self ._delay .time_elapsed
506
571
507
572
def _unload_instance (self ):
0 commit comments