-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
159 lines (126 loc) · 4.3 KB
/
utilities.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import math
import pickle
import re
import os
import time
from multiprocessing import current_process, Process
from threading import Thread
from glob import glob
import shutil
import settings
def get_chunksize(files_count):
max_divider = 100 # 100%
files_per_divider = 0
while files_per_divider < 1 and max_divider >= 1:
files_per_divider = files_count // max_divider
max_divider -= 1
if files_per_divider < 1:
files_per_divider = 1
return files_per_divider
def clean_filename(filename):
return re.sub(r'[:<>"\\/|?*]', '', str(filename))
class DatabaseStructure(object):
"""
Variables:
self.folder
self.image_folder
self.database_file
self.postfix
"""
def __init__(self, folder):
self.folder = clean_filename(settings.DATABASE_FOLDER.format(folder=folder))
self.image_folder = self.folder + '/' + clean_filename(settings.IMAGES_FOLDER)
self.postfix = settings.POSTFIX
def get_image_filename(self, image_name):
return (self.image_folder + '/{}.' + self.postfix).format(clean_filename(image_name))
def make_folders(self):
if not os.path.isdir(self.folder):
os.mkdir(self.folder)
if not os.path.isdir(self.image_folder):
os.mkdir(self.image_folder)
def get_list_name(self, number):
return (self.image_folder + '/{}.' + self.postfix).format(number)
def get_list_names(self):
return glob(self.image_folder + '/*' + self.postfix)
def remove_existing_files(self):
if os.path.isdir(self.folder):
shutil.rmtree(self.folder)
def get_database_structure(folder):
return DatabaseStructure(folder)
def save(item, filename):
if not os.path.isfile(filename):
with open(filename, 'wb') as file:
pickle.dump(item, file, protocol=pickle.HIGHEST_PROTOCOL)
def load(filename):
with open(filename, 'rb') as file:
return pickle.load(file)
is_first_print = True
last_percent = None
last_percent_time_left = None
last_percent_print_time = None
est_time_lefts = [0, 0, 0]
# est time left is very unaccurate
def print_progress(curr, total, curr_chunk=None, total_chunks=None):
global is_first_print
global last_percent
global last_percent_time_left
global last_percent_print_time
global est_time_lefts
curr_percent = math.floor(curr / total * 100)
curr_time = time.time()
if is_first_print:
est_time_left = float("inf")
is_first_print = False
last_percent_time_left = est_time_left
last_percent_print_time = curr_time
elif last_percent == curr_percent:
est_time_left = last_percent_time_left
else:
bad_est_time_left = (curr_time - last_percent_print_time) / (curr_percent - last_percent) * (100 - curr_percent)
est_time_lefts.append(bad_est_time_left)
est_time_lefts = est_time_lefts[1:]
est_time_left = sum(est_time_lefts) / len(est_time_lefts)
last_percent_time_left = est_time_left
last_percent_print_time = curr_time
last_percent = curr_percent
print('\r >>> {0} / {1} => {2}% | Time Left est. {3:.2f}s'.format(curr, total, curr_percent, est_time_left), end='')
if curr_chunk and total_chunks:
print(' | {} / {}'.format(curr_chunk, total_chunks), end='')
def print_done(msg):
if isinstance(msg, str):
print(' [ done ] => {msg}s'.format(msg=msg))
else: # a float, time taken
print(' [ done ] => {0:.2f}s'.format(msg))
global is_first_print
global last_percent
global last_percent_time_left
global last_percent_print_time
global est_time_lefts
is_first_print = True
last_percent = None
last_percent_print_time = None
last_percent_time_left = None
est_time_lefts = [0, 0, 0]
# def remove_empty(a_list):
# if not isinstance(a_list, list):
# return a_list
#
# if not a_list:
# return False
#
# result = []
# for item in a_list:
# item = remove_empty(item)
# if item:
# result.append(item)
# return result
def remove_empty(a_list):
if not isinstance(a_list, list):
return a_list
result = []
for item in a_list:
if item:
item = remove_empty(item)
if item:
result.append(item)
return result