Skip to content

Commit bdc5cad

Browse files
committed
Updated doc-strings in listeners.tick.
1 parent c87e739 commit bdc5cad

File tree

1 file changed

+80
-15
lines changed
  • addons/source-python/packages/source-python/listeners

1 file changed

+80
-15
lines changed

addons/source-python/packages/source-python/listeners/tick.py

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ class Delay(WeakAutoUnload):
111111
def __init__(self, delay, callback, *args, **kwargs):
112112
"""Initialize the delay.
113113
114-
:param int delay: The delay in seconds.
114+
:param float delay: The delay in seconds.
115115
:param callback: A callable object that should be called after the
116116
delay expired.
117117
:param args: Arguments that should be passed to the callback.
118118
:param kwargs: Keyword arguments that should be passed to the
119119
callback.
120+
:raises ValueError: If the given callback is not callable.
120121
"""
121122
if not callable(callback):
122123
raise ValueError('Given callback is not callable.')
@@ -151,18 +152,29 @@ def cancel(self):
151152

152153
@property
153154
def running(self):
154-
"""Return True if the delay running."""
155+
"""Return True if the delay running.
156+
157+
:rtype: bool
158+
"""
155159
return self in _delay_manager
156160

157161
@property
158162
def time_remaining(self):
163+
"""Return the remaining time (in seconds) until the Delay ends.
164+
165+
:rtype: float
166+
"""
159167
if not self.running:
160168
# TODO: what should we return here, or should we raise an error?
161169
return None
162170
return self.exec_time - time.time()
163171

164172
@property
165173
def time_elapsed(self):
174+
"""Return the amount of time (in seconds) since the Delay started.
175+
176+
:rtype: float
177+
"""
166178
if not self.running:
167179
# TODO: what should we return here, or should we raise an error?
168180
return None
@@ -188,7 +200,14 @@ class Repeat(AutoUnload):
188200
"""Class used to create and call repeats."""
189201

190202
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+
"""
192211
# Store the base attributes
193212
self.callback = callback
194213
self.args = args
@@ -209,7 +228,15 @@ def __init__(self, callback, *args, **kwargs):
209228
self._loop_time = None
210229

211230
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+
"""
213240
# Log the start message
214241
listeners_tick_logger.log_debug(
215242
'Repeat.start: <{0}> <{1}>'.format(interval, limit))
@@ -344,7 +371,11 @@ def resume(self):
344371
self._delay = Delay(self._loop_time, self._execute)
345372

346373
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+
"""
348379
# Log the extend message
349380
listeners_tick_logger.log_debug('Repeat.extend')
350381

@@ -359,7 +390,7 @@ def extend(self, adjustment):
359390
return
360391

361392
# Was a positive integer given?
362-
if adjustment < 1 or not isinstance(adjustment, int):
393+
if not isinstance(adjustment, int) or adjustment < 1:
363394

364395
# Raise an error
365396
raise ValueError('Adjusted value must be a positive integer')
@@ -368,7 +399,12 @@ def extend(self, adjustment):
368399
self._adjusted += adjustment
369400

370401
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+
"""
372408
# Log the reduce message
373409
listeners_tick_logger.log_debug('Repeat.reduce')
374410

@@ -383,7 +419,7 @@ def reduce(self, adjustment):
383419
return
384420

385421
# Was a positive integer given?
386-
if adjustment < 1 or not isinstance(adjustment, int):
422+
if not isinstance(adjustment, int) or adjustment < 1:
387423

388424
# Raise an error
389425
raise ValueError('Adjusted value must be a positive integer')
@@ -446,7 +482,10 @@ def _execute(self):
446482

447483
@property
448484
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+
"""
450489
# Is there no limit?
451490
if not self._limit:
452491

@@ -458,12 +497,18 @@ def remaining_loops(self):
458497

459498
@property
460499
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+
"""
462504
return self._count
463505

464506
@property
465507
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+
"""
467512
# Is there no limit?
468513
if not self._limit:
469514

@@ -475,33 +520,53 @@ def loop_limit(self):
475520

476521
@property
477522
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+
"""
479527
return (
480528
self.remaining_loops * self._interval +
481529
self.delay_time_remaining
482530
)
483531

484532
@property
485533
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+
"""
487538
return self.total_time - self.time_left
488539

489540
@property
490541
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+
"""
492546
return self.loop_limit * self._interval
493547

494548
@property
495549
def status(self):
496-
"""Return the status of the repeat."""
550+
"""Return the status of the repeat.
551+
552+
:rtype: RepeatStatus
553+
"""
497554
return self._status
498555

499556
@property
500557
def delay_time_remaining(self):
558+
"""Return the time remaining in the current loop.
559+
560+
:rtype: float
561+
"""
501562
return self._delay.time_remaining
502563

503564
@property
504565
def delay_time_elapsed(self):
566+
"""Return the time elapsed in the current loop.
567+
568+
:rtype: float
569+
"""
505570
return self._delay.time_elapsed
506571

507572
def _unload_instance(self):

0 commit comments

Comments
 (0)