Browse code

add implementation of 'stop_after' goal

Joseph Weston authored on 21/11/2019 12:33:38
Showing 1 changed files
... ...
@@ -712,6 +712,46 @@ def replay_log(learner, log):
712 712
         getattr(learner, method)(*args)
713 713
 
714 714
 
715
+# --- Useful runner goals
716
+
717
+
718
+def stop_after(*, seconds=0, minutes=0, hours=0):
719
+    """Stop a runner after a specified time.
720
+
721
+    For example, to specify a runner that should stop after
722
+    5 minutes, one could do the following:
723
+
724
+    >>> runner = Runner(learner, goal=stop_after(minutes=5))
725
+
726
+    To stop a runner after 2 hours, 10 minutes and 3 seconds,
727
+    one could do the following:
728
+
729
+    >>> runner = Runner(learner, goal=stop_after(hours=2, minutes=10, seconds=3))
730
+
731
+    Parameters
732
+    ----------
733
+    seconds, minutes, hours : float, default: 0
734
+        If more than one is specified, then they are added together
735
+
736
+    Returns
737
+    -------
738
+    goal : callable
739
+        Can be used as the ``goal`` parameter when constructing
740
+        a `Runner`.
741
+
742
+    Notes
743
+    -----
744
+    The duration specified is only a *lower bound* on the time that the
745
+    runner will run for, because the runner only checks its goal when
746
+    it adds points to its learner
747
+    """
748
+    stop_time = time.time() + seconds + 60 * minutes + 3600 * hours
749
+    return lambda _: time.time() > stop_time
750
+
751
+
752
+# -- Internal executor-related, things
753
+
754
+
715 755
 class SequentialExecutor(concurrent.Executor):
716 756
     """A trivial executor that runs functions synchronously.
717 757