Spaces:
Build error
Build error
update utils
Browse files- src/utils/file.py +32 -1
src/utils/file.py
CHANGED
|
@@ -1,8 +1,39 @@
|
|
| 1 |
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def ensure_folder_exists(path):
|
| 7 |
if not os.path.exists(path):
|
| 8 |
os.makedirs(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
import os
|
| 3 |
+
import glob
|
| 4 |
|
| 5 |
+
from multiprocessing import Pool
|
| 6 |
+
import timeit
|
| 7 |
+
from tqdm import tqdm
|
| 8 |
|
| 9 |
def ensure_folder_exists(path):
|
| 10 |
if not os.path.exists(path):
|
| 11 |
os.makedirs(path)
|
| 12 |
+
|
| 13 |
+
def find_files(pattern):
|
| 14 |
+
files = glob.glob(pattern)
|
| 15 |
+
return files
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def save_to_file(file_name, content):
|
| 19 |
+
with open(file_name, 'w', encoding='utf-8') as f:
|
| 20 |
+
f.write(content)
|
| 21 |
+
# print("Saved: ", file_name)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def multi_process_list(input_list, function, num_workers=20):
|
| 25 |
+
with Pool(num_workers) as p:
|
| 26 |
+
result = list(tqdm(p.imap_unordered(function, input_list), total=len(input_list)))
|
| 27 |
+
return result
|
| 28 |
+
|
| 29 |
+
def process_list(input_list, function):
|
| 30 |
+
# process with process bar
|
| 31 |
+
result = []
|
| 32 |
+
for item in tqdm(input_list):
|
| 33 |
+
result.append(function(item))
|
| 34 |
+
return result
|
| 35 |
+
|
| 36 |
+
def read_txt_content(file):
|
| 37 |
+
with open(file, 'r', encoding='utf-8') as f:
|
| 38 |
+
text = f.read()
|
| 39 |
+
return text
|