-
Notifications
You must be signed in to change notification settings - Fork 0
/
utls.py
35 lines (28 loc) · 1.12 KB
/
utls.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
import time
import warnings
from pathlib import Path
# A decorator to throw warning when we use deprecated methods/functions/routines
def deprecated(func):
"""This is a decorator which can be used to mark functions
as deprecated. It will result in a warning being emmitted
when the function is used."""
def new_func(*args, **kwargs):
warnings.warn("Call to deprecated function %s." % func.__name__,
category=DeprecationWarning)
return func(*args, **kwargs)
new_func.__name__ = func.__name__
new_func.__doc__ = func.__doc__
new_func.__dict__.update(func.__dict__)
return new_func
# A decorator to time the execution of a function
def timer_decorator(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"Function {func.__name__} took {end_time - start_time} seconds to run.")
return result
return wrapper
def is_docker():
cgroup = Path('/proc/self/cgroup')
return Path('/.dockerenv').is_file() or cgroup.is_file() and 'docker' in cgroup.read_text()