- Retrier
- RequestRetry
- MysqlRetry
- get_host_ip
- pager
- redis_connection
- user_agent
-
Retrier
A universial class decorator for both coroutine function and normal function.
It catchesexceptions
declares specifically by user in the arguments of the decorator, and retries it by statedretry
time andcountdown
between every retry if one of theexceptions
occurs. It will not do anything as long as the function run smoothly, or elseexception_return
will be return when the retry time exhausts or returnother_exception_return
if any Exception, others than theexceptions
, are caught.Demo:
from GenericUtils import Retrier ## decorate normal function @Retrier( exceptions=(KeyError,), exception_return=False, other_exception_return=False, retry=3, countdown=0, ) def test1(*args, **kwargs): print("Starting test") raise KeyError test1(1, 2, a=3, b=4) ## decorate coroutine function @Retrier( exceptions=(KeyError,) ) async def test2(*args, **kwargs): print("Starting test") raise KeyError ## call it like normal function test2(1, 2, a=3, b=4)
-
RequestRetry
A universial class decorator for both coroutine function and normal function.
It catcheshttp_exception
define in GenericUtils.utils.exception, and retries it by statedretry
time and sleep5
seconds between every retry if one of thehttp_exception
occurs. It will not do anything as long as the function run smoothly, or elseexception_return
will be return when the retry time exhausts or returnother_exception_return
if any Exception, others than theexceptions
, are caught.Demo:
from GenericUtils import RequestRetry from aiohttp.client_exceptions import ServerDisconnectedError from requests.exceptions import ConnectionError ## decorate normal function @RequestRetry( exception_return=False, other_exception_return=False, retry=3, ) def test3(*args, **kwargs): print("Starting test") raise ConnectionError test3(1, 2, a=3, b=4) ## decorate coroutine function @RequestRetry( exception_return=False, other_exception_return=False, retry=3, ) async def test4(*args, **kwargs): print("Starting test") raise ServerDisconnectedError test4(1, 2, a=3, b=4)
-
MysqlRetry
It catches
mysql_exception
define in GenericUtils.utils.exception, and retries it by statedretry
time and sleep30
seconds between every retry if one of themysql_exception
occurs. It will construct the mysql connector and cursor by mysql.connector before we execute the function, and commit & close the connection after the function finishes. If one ofmysql_exception
occurs, rollback the cursor and retry.exception_return
will be returned when the retry time exhausts. If any others Exception occurs, rollback the cursor and returnother_exception_return
.Demo:
from GenericUtils import MysqlRetry from mysql.connector.errors import OperationalError @MysqlRetry( exception_return=False, other_exception_return=False, retry=3, host = "127.0.0.1", port = "3306", user_name = "user_name", password = "password", database = "database", ) def test5(*args, **kwargs): print("Starting test") conn, cur = kwargs["conn"], kwargs["cur"] cur.execute( operation="SELECT * FROM TABLE_NAME" ) results = cur.fetchall() print(results[0]) raise OperationalError test5(1, 2, a=3, b=4)
-
get_host_ip
get local ip addr
Demo:
from GenericUtils import get_host_ip ip = get_host_ip() print(ip)
-
pager
paging util
Demo:
from GenericUtils import pager for _ in pager(5, 61, 20): print(_)
-
redis_connection
redis connection
Demo:
from GenericUtils import redis_connection connection = redis_connection(host="127.0.0.1", port="6379", password="password", db=0, decode_responses=True ) connection.sadd("test", 1)
-
user_agent