-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.py
45 lines (34 loc) · 995 Bytes
/
decorator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
""" Most used decorators
"""
from typing import Callable
from logzero import logger
def singleton(some_class):
""" Make a Class Singleton
"""
def on_call(*args, **kwargs):
if on_call.instance is None:
on_call.instance = some_class(*args, **kwargs)
return on_call.instance
on_call.instance = None
return on_call
def deprecated(_describe):
""" Annotation for deprecated functions
"""
def decorator(_func):
def wrapped(*_args, **_kwargs):
return None
return wrapped
return decorator
def shout_err(message: str):
""" Annotate err log on Exception
"""
def decorator(func: Callable):
def wrapped(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as err:
logger.error('<<< %s >>', message.upper())
logger.error(err)
return None
return wrapped
return decorator