-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.py
35 lines (31 loc) · 1.1 KB
/
decorators.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
def timer(func):
''' Decorate functions with this function get execution time.
'''
import time # If not imported at the top.
def wrapper(*args, **kwargs):
t0 = time.time()
result = func(*args, **kwargs)
t1 = time.time()
print('Exexcution time:', t1-t0)
return result
return wrapper
def aws_clean_fail(func):
'''
A decorator to cleanly exit on a failed call to AWS.
catch a `botocore.exceptions.ClientError` raised from an action.
This sort of error is raised if you are targeting a region that
isn't set up.
https://github.com/fugue/credstash/blob/b6d56359247440cc3fab8813b901f2118f464d46/credstash.py#L253
'''
def func_wrapper(*args, **kwargs):
# from botocore.exceptions import ClientError
try:
return func(*args, **kwargs)
except botocore.exceptions.ClientError as e:
print(str(e), file=sys.stderr)
logger.exception(e)
except Exception as e:
print(str(e), file=sys.stderr)
logger.exception(e)
sys.exit(1)
return func_wrapper