-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathio_worker.py
117 lines (93 loc) · 3.14 KB
/
io_worker.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import logging
import math
import os
import shutil
import pickle
import zlib
def get_size_obj(num, suffix="B"):
if num == 0:
return "0"
magnitude = int(math.floor(math.log(num, 1024)))
val = num / math.pow(1024, magnitude)
if magnitude > 7:
return "{:3.1f}{}{}".format(val, "Yi", suffix)
return "{:3.1f}{}{}".format(
val, ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"][magnitude], suffix
)
def print_status(message, is_screen=True, is_log=True) -> object:
if isinstance(message, int):
message = f"{message:,}"
if is_screen:
print(message)
if is_log:
logging.info(message)
def delete_folder(folder_dir):
if os.path.exists(folder_dir):
shutil.rmtree(folder_dir, ignore_errors=False)
return True
def delete_file(file_dir):
if os.path.exists(file_dir):
os.remove(file_dir)
return True
def create_dir(file_dir):
folder_dir = os.path.dirname(file_dir)
if not os.path.exists(folder_dir):
os.makedirs(folder_dir)
def save_obj_pkl(file_name, save_object, is_compress=False, is_message=True):
create_dir(file_name)
save_file = file_name
if ".pkl" not in file_name:
save_file = file_name + ".pkl"
if is_compress and ".zlib" not in file_name:
save_file += ".zlib"
temp_file = save_file + ".temp"
# Write temp
with open(temp_file, "wb") as fp:
if is_compress:
save_data = zlib.compress(
pickle.dumps(save_object, pickle.HIGHEST_PROTOCOL)
)
fp.write(save_data)
else:
pickle.dump(save_object, fp, pickle.HIGHEST_PROTOCOL)
try:
if os.path.exists(save_file):
os.remove(save_file)
except Exception as message:
print_status(message)
os.rename(temp_file, save_file)
if is_message:
print_status("Saved: - %d - %s" % (len(save_object), save_file), is_log=False)
return save_file
def load_obj_pkl(file_name, is_message=False):
load_obj = None
if not os.path.exists(file_name) and ".pkl" not in file_name:
file_name = file_name + ".pkl"
if not os.path.exists(file_name) and ".zlib" not in file_name:
file_name = file_name + ".zlib"
with open(file_name, "rb") as fp:
if ".zlib" in file_name:
load_obj = pickle.loads(zlib.decompress(fp.read()))
else:
load_obj = pickle.load(fp)
if is_message and load_obj:
print_status("%d loaded items - %s" % (len(load_obj), file_name))
return load_obj
def get_size_of_file(num, suffix="B"):
"""Get human friendly file size
https://gist.github.com/cbwar/d2dfbc19b140bd599daccbe0fe925597#gistcomment-2845059
Args:
num (int): Bytes value
suffix (str, optional): Unit. Defaults to 'B'.
Returns:
str: file size0
"""
if num == 0:
return "0"
magnitude = int(math.floor(math.log(num, 1024)))
val = num / math.pow(1024, magnitude)
if magnitude > 7:
return "{:3.1f}{}{}".format(val, "Yi", suffix)
return "{:3.1f}{}{}".format(
val, ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"][magnitude], suffix
)