-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
64 lines (46 loc) · 1.53 KB
/
utils.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import time
import socket
import pathlib
import pickle
from functools import wraps
from logging import getLogger
from typing import Any
import numpy
from config import TRAINED_DATA_PATH, MODEL_TYPE
log = getLogger(__name__)
def path_to_all_pkl_files(
root_path: str = TRAINED_DATA_PATH, model_type: str = MODEL_TYPE, default_pattern: str = "*.pkl"
) -> pathlib.Path.rglob:
return pathlib.Path(root_path, model_type).rglob(default_pattern)
def read_one_pkl_file(path: str, log_message: bool = True) -> Any:
with open(file=path, mode="rb") as file:
data = pickle.loads(file.read())
if log_message:
log.debug(f"File: {file.name} unpickled successfully")
return data
def log_execution_time(func):
"""Only for sync functions"""
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
log.info(f"<{func.__module__}.{func.__name__}> execution time {round(end - start, 3)} seconds")
return result
return wrapper
def uniq_id_from_time() -> int:
return int(time.time() * 1000)
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, numpy.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
def check_port(ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
s.shutdown(2)
return True
except Exception:
return False