forked from renatahodovan/picire
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add limits to reduction (renatahodovan#43)
- Loading branch information
1 parent
a80b86b
commit 842e7cf
Showing
9 changed files
with
245 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright (c) 2023 Renata Hodovan, Akos Kiss. | ||
# | ||
# Licensed under the BSD 3-Clause License | ||
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>. | ||
# This file may not be copied, modified, or distributed except | ||
# according to those terms. | ||
|
||
|
||
class ReductionException(Exception): | ||
""" | ||
Base class of reduction-related exceptions. In addition to signal the | ||
premature termination of a reduction process, exception instances contain | ||
the intermediate result of the reduction. | ||
:ivar result: A representation of the smallest, potentially non-minimal, but | ||
failing test case found during reduction. | ||
""" | ||
|
||
def __init__(self, *args, result=None): | ||
super().__init__(*args) | ||
self.result = result | ||
|
||
|
||
class ReductionStopped(ReductionException): | ||
""" | ||
Exception to signal that reduction has been stopped, e.g., because some time | ||
limit or test count limit has been reached. | ||
""" | ||
|
||
|
||
class ReductionError(ReductionException): | ||
""" | ||
Exception to signal that an unexpected error occured during reduction. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright (c) 2023 Renata Hodovan, Akos Kiss. | ||
# | ||
# Licensed under the BSD 3-Clause License | ||
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>. | ||
# This file may not be copied, modified, or distributed except | ||
# according to those terms. | ||
|
||
from datetime import datetime, timedelta | ||
from time import time | ||
|
||
from .exception import ReductionStopped | ||
|
||
|
||
class LimitReduction: | ||
""" | ||
Limit the execution time and/or the number of performed tests during a | ||
reduction. | ||
""" | ||
|
||
def __init__(self, *, deadline=None, max_tests=None): | ||
""" | ||
:param deadline: A soft limit on the execution time of the reduction. | ||
The deadline may be given as a :class:`datetime` object, as a | ||
:class:`timedelta` object (relative to :meth:`~datetime.now`), or as | ||
a ``float`` POSIX timestamp (as returned by :meth:`time.time`). | ||
:param max_tests: A hard limit on the maximum number of tests that may | ||
be executed. | ||
""" | ||
self._deadline = deadline | ||
self._max_tests = max_tests | ||
|
||
if isinstance(deadline, timedelta): | ||
deadline = datetime.now() + deadline | ||
if isinstance(deadline, datetime): | ||
deadline = deadline.timestamp() | ||
self._deadline_timestamp = deadline | ||
self._tests_left = max_tests | ||
|
||
def __call__(self): | ||
if self._deadline is not None: | ||
if time() >= self._deadline_timestamp: | ||
raise ReductionStopped('deadline expired') | ||
if self._max_tests is not None: | ||
if self._tests_left <= 0: | ||
raise ReductionStopped('maximum number of tests performed') | ||
self._tests_left -= 1 | ||
|
||
def __str__(self): | ||
cls = self.__class__ | ||
params = [] | ||
if self._deadline is not None: | ||
params.append(f'deadline={self._deadline}') | ||
if self._max_tests is not None: | ||
params.append(f'max_tests={self._max_tests}') | ||
return f'{cls.__module__}.{cls.__name__}({", ".join(params)})' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.