Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
stop.py131 linesDownload Raw Back to tenacity
1# Copyright 2016–2021 Julien Danjou2# Copyright 2016 Joshua Harlow3# Copyright 2013-2014 Ray Holder4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16import abc17import typing18 19from tenacity import _utils20 21if typing.TYPE_CHECKING:22    import threading23 24    from tenacity import RetryCallState25 26 27class stop_base(abc.ABC):28    """Abstract base class for stop strategies."""29 30    @abc.abstractmethod31    def __call__(self, retry_state: "RetryCallState") -> bool:32        pass33 34    def __and__(self, other: "stop_base") -> "stop_all":35        return stop_all(self, other)36 37    def __or__(self, other: "stop_base") -> "stop_any":38        return stop_any(self, other)39 40 41StopBaseT = typing.Union[stop_base, typing.Callable[["RetryCallState"], bool]]42 43 44class stop_any(stop_base):45    """Stop if any of the stop condition is valid."""46 47    def __init__(self, *stops: stop_base) -> None:48        self.stops = stops49 50    def __call__(self, retry_state: "RetryCallState") -> bool:51        return any(x(retry_state) for x in self.stops)52 53 54class stop_all(stop_base):55    """Stop if all the stop conditions are valid."""56 57    def __init__(self, *stops: stop_base) -> None:58        self.stops = stops59 60    def __call__(self, retry_state: "RetryCallState") -> bool:61        return all(x(retry_state) for x in self.stops)62 63 64class _stop_never(stop_base):65    """Never stop."""66 67    def __call__(self, retry_state: "RetryCallState") -> bool:68        return False69 70 71stop_never = _stop_never()72 73 74class stop_when_event_set(stop_base):75    """Stop when the given event is set."""76 77    def __init__(self, event: "threading.Event") -> None:78        self.event = event79 80    def __call__(self, retry_state: "RetryCallState") -> bool:81        return self.event.is_set()82 83 84class stop_after_attempt(stop_base):85    """Stop when the previous attempt >= max_attempt."""86 87    def __init__(self, max_attempt_number: int) -> None:88        self.max_attempt_number = max_attempt_number89 90    def __call__(self, retry_state: "RetryCallState") -> bool:91        return retry_state.attempt_number >= self.max_attempt_number92 93 94class stop_after_delay(stop_base):95    """96    Stop when the time from the first attempt >= limit.97 98    Note: `max_delay` will be exceeded, so when used with a `wait`, the actual total delay will be greater99    than `max_delay` by some of the final sleep period before `max_delay` is exceeded.100 101    If you need stricter timing with waits, consider `stop_before_delay` instead.102    """103 104    def __init__(self, max_delay: _utils.time_unit_type) -> None:105        self.max_delay = _utils.to_seconds(max_delay)106 107    def __call__(self, retry_state: "RetryCallState") -> bool:108        if retry_state.seconds_since_start is None:109            raise RuntimeError("__call__() called but seconds_since_start is not set")110        return retry_state.seconds_since_start >= self.max_delay111 112 113class stop_before_delay(stop_base):114    """115    Stop right before the next attempt would take place after the time from the first attempt >= limit.116 117    Most useful when you are using with a `wait` function like wait_random_exponential, but need to make118    sure that the max_delay is not exceeded.119    """120 121    def __init__(self, max_delay: _utils.time_unit_type) -> None:122        self.max_delay = _utils.to_seconds(max_delay)123 124    def __call__(self, retry_state: "RetryCallState") -> bool:125        if retry_state.seconds_since_start is None:126            raise RuntimeError("__call__() called but seconds_since_start is not set")127        return (128            retry_state.seconds_since_start + retry_state.upcoming_sleep129            >= self.max_delay130        )131 
codekingpro/portable-devtools · Team Ai