-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtutils.py
More file actions
128 lines (108 loc) · 3.14 KB
/
tutils.py
File metadata and controls
128 lines (108 loc) · 3.14 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
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
from collections import OrderedDict
import os
import numpy as np
import torch
import random
import string
import random
import time
import cv2
from pathlib import Path
TUTILS_DEBUG = True
TUTILS_INFO = True
TUTILS_WARNING = True
def p(*s,end="\n", **kargs ):
if TUTILS_INFO:
print("[Trans Info] ", s, kargs, end="")
print("", end=end)
def w(*s,end="\n", **kargs ):
if TUTILS_WARNING or TUTILS_DEBUG:
print("[Trans Warning] ", s, kargs, end="")
print("", end=end)
def d(*s,end="\n", **kargs ):
if TUTILS_DEBUG:
print("[Trans Debug] ", s, kargs, end="")
print("", end=end)
def tfuncname(func):
def run(*argv, **kargs):
print("--------------------------------------------")
print("[Trans Utils] Function Name: ", end=" ")
print(func.__name__)
ret = func(*argv, **kargs)
# if argv:
# ret = func(*argv)
# else:
# ret = func()
return ret
return run
# @tfuncname
def tt():
# print("[Trans Utils] ", end="")
pass
def time_now():
tt()
return time.strftime("%Y%m%d-%H%M%S", time.localtime())
def generate_random_str(n):
tt()
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, n))
return ran_str
def generate_name():
tt()
return time_now() + generate_random_str(6)
# def write_image_np(image, filename):
# tt()
# cv2.imwrite("wc_" + generate_random_str(5)+'-'+ time_now() +".jpg", image.astype(np.uint8))
# pass
def tdir(*dir_paths):
def checkslash(name):
if name.startswith("/"):
name = name[1:]
return checkslash(name)
else:
return name
names = [dir_paths[0]]
for name in dir_paths[1:]:
names.append(checkslash(name))
dir_path = os.path.join(*names)
d(dir_path)
if not os.path.exists(dir_path):
d("Create Dir Path: ", dir_path)
os.makedirs(dir_path)
return dir_path
def tfilename(*filenames):
def checkslash(name):
if name.startswith("/"):
name = name[1:]
return checkslash(name)
else:
return name
names = [filenames[0]]
for name in filenames[1:]:
names.append(checkslash(name))
filename = os.path.join(*names)
d(filename)
parent, name = os.path.split(filename)
if not os.path.exists(parent):
os.makedirs(parent)
return filename
def texists(*filenames):
path = os.path.join(*filenames)
return os.path.exists(path)
def ttsave(state, path, configs=None):
path = tdir("trans_torch_models", path, generate_name())
if configs is not None:
assert type(configs) is dict
config_path = tfilename(path, "configs.json")
with open(config_path, "wb+") as f:
config_js = json.dumps(configs)
f.write(config_js)
torch.save(state, tfilename(path, "model.pkl"))
def add_total(tuple1, tuple2):
l = list()
for i, item in enumerate(tuple1):
l.append(tuple1[i] + tuple2[i])
return tuple(l)
if __name__ == "__main__":
# tt()
# tfilename("dasd", "/dasdsa", "/dsad")
tdir("dasd", "/dsadads", "/dsdas")