-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
84 lines (70 loc) · 2.6 KB
/
utils.py
File metadata and controls
84 lines (70 loc) · 2.6 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
import timeit
from functools import wraps
import multiprocessing
class TimeLimitExceeded(Exception):
"""Custom exception to indicate a timeout."""
def __init__(self, message="Time limit exceeded!"):
super().__init__(message)
def run_with_timeout(func, timeout: int, *args, **kwargs):
process = multiprocessing.Process(target=func, args=args, kwargs=kwargs)
process.start()
process.join(timeout)
if process.is_alive():
process.terminate()
process.join()
raise TimeLimitExceeded(f"exceeded the timeout of {timeout} seconds")
def log_execution_time(func, timeout: int = 1, throw: bool = False):
@wraps(func)
def wrapper(*args, **kwargs):
# classified_function_name = f"{args[0].__class__.__name__}.{func.__name__}"
print(f"-------- Executing {func.__name__} --------")
start = timeit.default_timer()
try:
result = run_with_timeout(func, timeout, *args, **kwargs)
except TimeLimitExceeded as e:
if throw:
raise e
print(e)
return
elapsed_time_ms = (timeit.default_timer() - start) * 1000
formatted_time = format_execution_time(elapsed_time_ms)
print(f"{func.__name__} executed in {formatted_time}")
print("---------------------------------------------")
return result
return wrapper
def format_execution_time(elapsed_time_ms):
if elapsed_time_ms >= 1000:
elapsed_time_sec = elapsed_time_ms / 1000
if elapsed_time_sec >= 60:
elapsed_time_min = elapsed_time_sec / 60
return f"{elapsed_time_min:.2f} minutes"
else:
return f"{elapsed_time_sec:.2f} seconds"
else:
return f"{elapsed_time_ms:.3f} ms"
class JSONUtil:
@staticmethod
def to_json(data):
return json.dumps(data, indent=4)
@staticmethod
def from_json(data):
return json.loads(data)
@staticmethod
def from_json_file(file):
with open(file, "r") as f:
return json.load(f)
class PythonUtils:
@staticmethod
def set_python_path(logger=None):
from pathlib import Path
import os
# # Get the current working directory
# current_directory = os.getcwd()
# Get the parent directory of the current working directory
parent_directory = Path.cwd().parent
# Set PYTHONPATH to the parent directory
os.environ["PYTHONPATH"] = str(parent_directory)
if logger:
# Print to verify the PYTHONPATH
logger.debug(f"PYTHONPATH set to: {os.environ['PYTHONPATH']}")