-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.py
245 lines (199 loc) · 5.72 KB
/
Utility.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import prettytable as pt
import re, os
import plistlib
import json
def MakeTable(input_df, column_index_name=""):
tb = pt.PrettyTable()
if column_index_name != "":
tb.add_column(column_index_name, input_df.index)
for (
col
) in (
input_df.columns.values
): # df.columns.values: get the name of list(each columns)
tb.add_column(col, input_df[col])
return tb
class Json_operator:
def __init__(self, path):
self.path = path
if os.path.isfile(path):
self.json = self.load()
else:
self.write({})
self.json = self.load()
def load(self):
try:
with open(self.path, "r") as file:
return json.load(file)
except:
return None
def write(self, root_dict):
try:
with open(self.path, "w") as file:
return json.dump(root_dict, file)
except:
return None
def get(self):
return self.json
def set(self, root_dict):
self.json = root_dict
return self.write(root_dict)
def update(self, key, value):
self.json[key] = value
return self.write(self.json)
class Plist_operator:
def __init__(self, path):
self.path = path
if os.path.isfile(path):
self.plist = self.load()
else:
self.write({})
self.plist = self.load()
def load(self):
try:
with open(self.path, "rb") as file:
return plistlib.load(file)
except:
return None
def write(self, root_dict):
try:
with open(self.path, "wb") as file:
return plistlib.dump(root_dict, file)
except:
return None
def get(self):
return self.plist
def set(self, root_dict):
self.plist = root_dict
return self.write(root_dict)
def update(self, key, value):
self.plist[key] = value
return self.write(self.plist)
def delete(self, key):
del self.plist[key]
return Write_Plist(self.path, self.plist)
def Load_Plist(path):
try:
with open(path, "rb") as file:
return plistlib.load(file)
except:
return None
def Write_Plist(path, root_dict):
try:
with open(path, "wb") as file:
return plistlib.dump(root_dict, file)
except:
return None
def Split_str(input_str, pattern, num=-1):
return input_str.split(pattern, num)
def List_2_Tuple(input_list):
if isinstance(input_list, list):
return tuple(input_list)
def series2list(input_series):
list = []
for i in range(len(input_series)):
list.append(input_series[i])
return list
# input: ["1","2","3"] ("1","2","3") "1" => [1,2,3], (1,2,3), 1
# format can be "Int","Float", it will make all input be that type, or it will "Auto"
def numerify(input, format="Auto"):
if (
isinstance(input, str)
or isinstance(input, int)
or isinstance(input, float)
):
input_type = is_intORfloat(input)
if input_type != "":
if format == "Int":
output = intify(input)
elif format == "Float":
output = floatify(input)
elif format == "Auto":
if input_type == "Int":
output = intify(input)
elif input_type == "Float":
output = floatify(input)
else:
output = input
elif isinstance(input, list) or isinstance(input, tuple):
output = []
for i in input:
i_type = is_intORfloat(i)
if i_type != "":
if format == "Int":
output.append(intify(i))
elif format == "Float":
output.append(floatify(i))
elif format == "Auto":
if i_type == "Int":
output.append(intify(i))
elif i_type == "Float":
output.append(floatify(i))
else:
output.append(i)
if isinstance(input, tuple):
output = List_2_Tuple(output)
else:
output = input
return output
def intify(input):
if is_int(input):
return int(input)
def floatify(input):
if is_float(input):
return float(input)
def isWithStr(input_str, pattern):
if isinstance(input_str, str):
if pattern in input_str:
return True
else:
return False
else:
return False
def is_float(input_str):
try:
float(input_str)
return True
except:
return False
def is_int(input_str):
try:
int(input_str)
return True
except:
return False
def is_intORfloat(input_str):
if is_float(input_str) & is_int(input_str):
if int(input_str) == float(input_str):
return "Int"
else:
return "Float"
else:
return ""
def p_log(print_str_list, log_path, end_s="\n"):
file_str = ""
for i in range(len(print_str_list)):
if i == 0:
file_str = print_str_list[i]
else:
file_str = file_str + " " + str(print_str_list[i])
print(file_str, end=end_s)
file_str = re.sub("\033\[\d*m", "", file_str)
with open(log_path, "a+") as f:
f.write(file_str)
if end_s == "\n":
f.write("\r\n")
else:
f.write(end_s)
f.close()
# Color
class BColors:
PINK = "\033[95m"
BLUE = "\033[94m"
CYAN = "\033[96m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
ENDC = "\033[0m" # End color
BOLD = "\033[1m" # B
UNDERLINE = "\033[4m" # _