1. 程式人生 > 其它 >python函式裝飾器,返回自定義型別

python函式裝飾器,返回自定義型別

from typing import Any, Callable, TypeVar
import requests
T = TypeVar("T")

class MaxRetryError(Exception):
    def __init__(self):
        message = "超出最大重試次數"
        super().__init__(message)

class MaxRetry:
    def __init__(self, max_retry: int = 2):
        self.max_retry = max_retry

    def __call__(self, connect_once: Callable[..., T]) -> Callable[..., T]:
        def connect_n_times(*args: Any, **kwargs: Any) -> T:
            retry = self.max_retry + 1
            while retry:
                try:
                    return connect_once(*args, **kwargs)
                except requests.exceptions.Timeout as e:
                    print("抓取超時,正在嘗試重新連線~")
                finally:
                    retry -= 1
            raise MaxRetryError()

        return connect_n_times
@MaxRetry(2)
def get_info(url: str) -> str:
    re = requests.get(url, timeout=2)
    return re.text
url = "https://www.abc.com/4de25977c7dce0"
get_info(url)

[output]:
抓取超時,正在嘗試重新連線~
抓取超時,正在嘗試重新連線~
抓取超時,正在嘗試重新連線~
---------------------------------------------------------------------------
MaxRetryError                             Traceback (most recent call last)
<ipython-input-37-bc2dde821bd1> in <module>
----> 1 get_info(url)

<ipython-input-34-b40f19f5bf7c> in connect_n_times(*args, **kwargs)
     22                 finally:
     23                     retry -= 1
---> 24             raise MaxRetryError()
     25 
     26         return connect_n_times

MaxRetryError: 超出最大重試次數