Skip to content

Commit 9cf2891

Browse files
authored
Merge pull request #1761 from avanwinkle/timer-kwargs-evaluation
Timer kwargs evaluation
2 parents d93655c + e40b0f1 commit 9cf2891

File tree

1 file changed

+35
-48
lines changed

1 file changed

+35
-48
lines changed

mpf/devices/timer.py

+35-48
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,12 @@ def reset(self, **kwargs):
185185
186186
Args:
187187
----
188-
**kwargs: Not used in this method. Only exists since this method is
189-
often registered as an event handler which may contain
190-
additional keyword arguments.
188+
**kwargs: Optional kwargs that may affect the evaluation of the
189+
timer value placeholder template.
191190
"""
192-
del kwargs
193-
194191
self.debug_log("Resetting timer. New value: %s", self.start_value)
195192

196-
self.jump(self.start_value)
193+
self.jump(self.start_value, **kwargs)
197194

198195
def start(self, **kwargs):
199196
"""Start this timer based on the starting value that's already been configured.
@@ -246,12 +243,10 @@ def restart(self, **kwargs):
246243
247244
Args:
248245
----
249-
**kwargs: Not used in this method. Only exists since this method is
250-
often registered as an event handler which may contain
251-
additional keyword arguments.
246+
**kwargs: Optional kwargs that may affect the evaluation of the
247+
timer value placeholder template.
252248
"""
253-
del kwargs
254-
self.reset()
249+
self.reset(**kwargs)
255250
# If the timer is not running, start it
256251
if not self.running:
257252
self.start()
@@ -300,16 +295,13 @@ def pause(self, timer_value=0, **kwargs):
300295
timer_value: How many seconds you want to pause the timer for. Note
301296
that this pause time is real-world seconds and does not take
302297
into consideration this timer's tick interval.
303-
**kwargs: Not used in this method. Only exists since this method is
304-
often registered as an event handler which may contain
305-
additional keyword arguments.
298+
**kwargs: Optional kwargs that may affect the evaluation of the
299+
timer value placeholder template.
306300
"""
307-
del kwargs
308-
309301
if not timer_value:
310302
pause_ms = 0 # make sure it's not None, etc.
311303
else:
312-
pause_ms = self._get_timer_value(timer_value) * 1000 # delays happen in ms
304+
pause_ms = self._get_timer_value(timer_value, in_ms=True, **kwargs) # delays happen in ms
313305

314306
self.info_log("Pausing Timer for %s ms", pause_ms)
315307

@@ -417,13 +409,10 @@ def add(self, timer_value, **kwargs):
417409
----
418410
timer_value: The number of ticks you want to add to this timer's
419411
current value.
420-
kwargs: Not used in this method. Only exists since this method is
421-
often registered as an event handler which may contain
422-
additional keyword arguments.
412+
**kwargs: Optional kwargs that may affect the evaluation of the
413+
timer value placeholder template.
423414
"""
424-
del kwargs
425-
426-
timer_value = self._get_timer_value(timer_value)
415+
timer_value = self._get_timer_value(timer_value, **kwargs)
427416
ticks_added = timer_value
428417

429418
new_value = self.ticks + ticks_added
@@ -457,13 +446,10 @@ def subtract(self, timer_value, **kwargs):
457446
----
458447
timer_value: The number of ticks you want to subtract from this
459448
timer's current value.
460-
**kwargs: Not used in this method. Only exists since this method is
461-
often registered as an event handler which may contain
462-
additional keyword arguments.
449+
**kwargs: Optional kwargs that may affect the evaluation of the
450+
timer value placeholder template.
463451
"""
464-
del kwargs
465-
466-
ticks_subtracted = self._get_timer_value(timer_value)
452+
ticks_subtracted = self._get_timer_value(timer_value, **kwargs)
467453

468454
self.ticks -= ticks_subtracted
469455

@@ -526,10 +512,19 @@ def _remove_system_timer(self):
526512
self.timer = None
527513

528514
@staticmethod
529-
def _get_timer_value(timer_value):
515+
def _get_timer_value(timer_value, in_ms=False, **kwargs):
516+
"""Return an int value for the number of ticks on the timer."""
517+
if hasattr(timer_value, "evaluate"):
518+
# Convert to int for ticks; config_spec must be float for change_tick_interval
519+
return int(timer_value.evaluate(kwargs) * (1000 if in_ms else 1))
520+
return timer_value
521+
522+
@staticmethod
523+
def _get_timer_tick_secs(timer_value, **kwargs):
524+
"""Return a float value for the number of seconds between each tick."""
530525
if hasattr(timer_value, "evaluate"):
531526
# Convert to int for ticks; config_spec must be float for change_tick_interval
532-
return int(timer_value.evaluate([]))
527+
return timer_value.evaluate(kwargs)
533528
return timer_value
534529

535530
def change_tick_interval(self, change=0.0, **kwargs):
@@ -541,14 +536,10 @@ def change_tick_interval(self, change=0.0, **kwargs):
541536
tick rate. Note this value is multiplied by the current tick
542537
interval: >1 will increase the tick interval (slow the timer) and
543538
<1 will decrease the tick interval (accelerate the timer).
544-
To set an absolute value, use the set_tick_interval() method.
545-
**kwargs: Not used in this method. Only exists since this method is
546-
often registered as an event handler which may contain
547-
additional keyword arguments.
539+
**kwargs: Optional kwargs that may affect the evaluation of the
540+
timer value placeholder template.
548541
"""
549-
del kwargs
550-
551-
self.tick_secs *= change.evaluate([])
542+
self.tick_secs *= change.evaluate(kwargs)
552543
self._create_system_timer()
553544

554545
def set_tick_interval(self, timer_value, **kwargs):
@@ -561,11 +552,10 @@ def set_tick_interval(self, timer_value, **kwargs):
561552
----
562553
timer_value: The new number of seconds between each tick of this
563554
timer. This value should always be positive.
564-
**kwargs: Not used in this method. Only exists since this method is
565-
often registered as an event handler which may contain
566-
additional keyword arguments.
555+
**kwargs: Optional kwargs that may affect the evaluation of the
556+
timer value placeholder template.
567557
"""
568-
self.tick_secs = abs(self._get_timer_value(timer_value.evaluate(kwargs)))
558+
self.tick_secs = abs(self._get_timer_tick_secs(timer_value, **kwargs))
569559
self._create_system_timer()
570560

571561
def jump(self, timer_value, **kwargs):
@@ -577,13 +567,10 @@ def jump(self, timer_value, **kwargs):
577567
Args:
578568
----
579569
timer_value: Integer of the current value you want this timer to be.
580-
**kwargs: Not used in this method. Only exists since this method is
581-
often registered as an event handler which may contain
582-
additional keyword arguments.
570+
**kwargs: Optional kwargs that may affect the evaluation of the
571+
timer value placeholder template.
583572
"""
584-
del kwargs
585-
586-
self.ticks = self._get_timer_value(timer_value)
573+
self.ticks = self._get_timer_value(timer_value, **kwargs)
587574

588575
if self.max_value and self.ticks > self.max_value:
589576
self.ticks = self.max_value

0 commit comments

Comments
 (0)