upplib 3.2.2__py3-none-any.whl → 3.3.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- upplib/__init__.py +6 -1
- upplib/config_data.py +71 -0
- upplib/file.py +49 -0
- upplib/{file_text.py → file_to_text.py} +17 -450
- upplib/index.py +0 -68
- upplib/query_log.py +1 -0
- upplib/text_to_file.py +389 -0
- upplib/util.py +2 -3
- {upplib-3.2.2.dist-info → upplib-3.3.8.dist-info}/METADATA +1 -1
- {upplib-3.2.2.dist-info → upplib-3.3.8.dist-info}/RECORD +13 -11
- {upplib-3.2.2.dist-info → upplib-3.3.8.dist-info}/WHEEL +0 -0
- {upplib-3.2.2.dist-info → upplib-3.3.8.dist-info}/licenses/LICENSE +0 -0
- {upplib-3.2.2.dist-info → upplib-3.3.8.dist-info}/top_level.txt +0 -0
upplib/__init__.py
CHANGED
|
@@ -8,7 +8,12 @@ from upplib.util import *
|
|
|
8
8
|
|
|
9
9
|
# 有关文件操作类的包
|
|
10
10
|
from upplib.file import *
|
|
11
|
-
from upplib.
|
|
11
|
+
from upplib.file_to_text import *
|
|
12
|
+
from upplib.text_to_file import *
|
|
13
|
+
|
|
14
|
+
from upplib.clean_up_msg import *
|
|
15
|
+
|
|
16
|
+
from upplib.config_data import *
|
|
12
17
|
|
|
13
18
|
# 有关 图表的 html 代码的包
|
|
14
19
|
from upplib.chart_html import *
|
upplib/config_data.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from upplib import *
|
|
2
|
+
from upplib.common_package import *
|
|
3
|
+
from upplib.file import *
|
|
4
|
+
|
|
5
|
+
__CONFIG_PATH = '.upp.config'
|
|
6
|
+
|
|
7
|
+
__CONFIG_PATH_BAK = '_upp.config'
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# 将数据写入到 config 中
|
|
11
|
+
def set_config_data(file_name: str = 'config',
|
|
12
|
+
param: dict[str, Any] = None) -> None:
|
|
13
|
+
set_data_in_user_home(file_name, {} if param is None else param)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# 从 config 中获得 配置数据
|
|
17
|
+
def get_config_data(file_name: str = 'config') -> dict[str, Any]:
|
|
18
|
+
# print('get_config_data', file_name)
|
|
19
|
+
config_data = get_data_from_user_home(file_name)
|
|
20
|
+
# print('get_data_from_user_home', config_data)
|
|
21
|
+
if not config_data:
|
|
22
|
+
config_data = get_data_from_path(file_name)
|
|
23
|
+
# print('get_data_from_path', config_data)
|
|
24
|
+
return config_data
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# 在当前用户的主目录中, 获得指定文件的数据
|
|
28
|
+
def get_data_from_user_home(file_name: str = 'config') -> dict[str, Any]:
|
|
29
|
+
return get_data_from_path(file_name, os.path.expanduser("~"))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# 将 data 数据,在当前用户的主目录中, 获得指定文件的数据
|
|
33
|
+
def set_data_in_user_home(file_name: str = 'config',
|
|
34
|
+
param: dict[str, Any] = None) -> None:
|
|
35
|
+
set_data_in_path(file_name, {} if param is None else param, os.path.expanduser("~"))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# 在当前的目录中, 获得指定文件的数据
|
|
39
|
+
def get_data_from_path(file_name: str = 'config',
|
|
40
|
+
file_path: str = None) -> dict[str, Any]:
|
|
41
|
+
param = get_data_from_path_detail(file_name, file_path, __CONFIG_PATH)
|
|
42
|
+
return param if param else get_data_from_path_detail(file_name, file_path, __CONFIG_PATH_BAK)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def get_data_from_path_detail(file_name: str = 'config',
|
|
46
|
+
file_path: str = None,
|
|
47
|
+
path_name: str = __CONFIG_PATH) -> dict[str, Any]:
|
|
48
|
+
config_path = file_path + '/' + path_name if file_path else path_name
|
|
49
|
+
# print('config_path_1', config_path)
|
|
50
|
+
if not os.path.exists(config_path):
|
|
51
|
+
# print('config_path_2', config_path)
|
|
52
|
+
return {}
|
|
53
|
+
file_path = config_path + '/' + file_name + '.json'
|
|
54
|
+
# print('config_path_3', file_path)
|
|
55
|
+
if not os.path.exists(file_path):
|
|
56
|
+
return {}
|
|
57
|
+
# print('to_json_from_file', file_path)
|
|
58
|
+
return to_json_from_file(file_path)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# 在当前的目录中, 设置数据到指定路径下
|
|
62
|
+
def set_data_in_path(file_name: str = 'config',
|
|
63
|
+
param: str = None,
|
|
64
|
+
file_path: str = '') -> None:
|
|
65
|
+
config_path = file_path + '/' + __CONFIG_PATH
|
|
66
|
+
if not os.path.exists(config_path):
|
|
67
|
+
os.mkdir(config_path)
|
|
68
|
+
file_path = config_path + '/' + file_name + '.json'
|
|
69
|
+
text_file = open(file_path, 'w', encoding='utf-8')
|
|
70
|
+
text_file.write(to_str({} if param is None else param))
|
|
71
|
+
text_file.close()
|
upplib/file.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from upplib import *
|
|
2
2
|
from upplib.index import *
|
|
3
|
+
from upplib.index import is_win
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
def get_file(file_path: str = None,
|
|
@@ -37,6 +38,38 @@ def get_file(file_path: str = None,
|
|
|
37
38
|
return r_list
|
|
38
39
|
|
|
39
40
|
|
|
41
|
+
def get_latest_file(file_path: str = None,
|
|
42
|
+
path_startswith: str = None,
|
|
43
|
+
startswith: str = None,
|
|
44
|
+
path_contain: str = None,
|
|
45
|
+
contain: str = None,
|
|
46
|
+
path_endswith: str = None,
|
|
47
|
+
endswith: str = None,
|
|
48
|
+
full_path: bool = None) -> None | tuple[str, str] | str:
|
|
49
|
+
"""
|
|
50
|
+
按照文件名字排序,获得最新的一个文件
|
|
51
|
+
full_path: 是否返回完整的路径
|
|
52
|
+
"""
|
|
53
|
+
html_list = get_file(file_path=file_path,
|
|
54
|
+
path_startswith=path_startswith,
|
|
55
|
+
startswith=startswith,
|
|
56
|
+
path_contain=path_contain,
|
|
57
|
+
contain=contain,
|
|
58
|
+
sort_asc=False,
|
|
59
|
+
path_endswith=path_endswith,
|
|
60
|
+
endswith=endswith)
|
|
61
|
+
r1 = html_list[-1] if len(html_list) > 0 else None
|
|
62
|
+
if r1 is None:
|
|
63
|
+
return None
|
|
64
|
+
r1_short = r1
|
|
65
|
+
file_sep = '\\' if is_win() else '/'
|
|
66
|
+
if file_sep in r1:
|
|
67
|
+
r1_short = r1.split(file_sep)[-1]
|
|
68
|
+
if full_path is None:
|
|
69
|
+
return r1_short, r1
|
|
70
|
+
return r1 if full_path else r1_short
|
|
71
|
+
|
|
72
|
+
|
|
40
73
|
def get_file_folder(file_name_one: str = None) -> str:
|
|
41
74
|
"""
|
|
42
75
|
返回这个文件的文件夹路径
|
|
@@ -212,6 +245,22 @@ def find_file_by_content(file_path: str = '',
|
|
|
212
245
|
to_log(one_file, e)
|
|
213
246
|
continue
|
|
214
247
|
|
|
248
|
+
|
|
249
|
+
def check_file(file_name: str = None) -> None:
|
|
250
|
+
r"""
|
|
251
|
+
检查文件夹是否存在,不存在,就创建新的
|
|
252
|
+
支持多级目录 , 例如: C:\Users\yangpu\Desktop\study\a\b\c\d\e\f
|
|
253
|
+
"""
|
|
254
|
+
if file_name is None or file_name == '':
|
|
255
|
+
return
|
|
256
|
+
file_sep = '\\' if is_win() else '/'
|
|
257
|
+
f_n = file_name.split(file_sep)
|
|
258
|
+
for i in range(1, len(f_n) + 1):
|
|
259
|
+
# C:\Users\yangpu\Desktop\study\p.t
|
|
260
|
+
p_n = file_sep.join(f_n[0:i])
|
|
261
|
+
if p_n and not os.path.exists(p_n):
|
|
262
|
+
os.mkdir(p_n)
|
|
263
|
+
|
|
215
264
|
# print(get_file_data_line(r'D:\notepad_file\202306\a.txt', 'payout', from_last=False))
|
|
216
265
|
|
|
217
266
|
# file_all = get_file(r'C:\Users\yang\Desktop\ticket\no.use', path_contain='03')
|
|
@@ -1,361 +1,6 @@
|
|
|
1
1
|
from upplib import *
|
|
2
|
-
from datetime import datetime, timezone, timedelta
|
|
3
|
-
from typing import Any, Optional, Union
|
|
4
2
|
from upplib.common_package import *
|
|
5
3
|
from upplib.file import get_file
|
|
6
|
-
from collections import defaultdict
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def get_latest_file(file_path: str = None,
|
|
10
|
-
path_prefix: str = None,
|
|
11
|
-
prefix: str = None,
|
|
12
|
-
path_contain: str = None,
|
|
13
|
-
contain: str = None,
|
|
14
|
-
path_suffix: str = None,
|
|
15
|
-
suffix: str = None,
|
|
16
|
-
full_path: bool = None) -> None | tuple[str, str] | str:
|
|
17
|
-
"""
|
|
18
|
-
按照文件名字排序,获得最新的一个文件
|
|
19
|
-
full_path: 是否返回完整的路径
|
|
20
|
-
"""
|
|
21
|
-
html_list = get_file(file_path=file_path,
|
|
22
|
-
path_prefix=path_prefix,
|
|
23
|
-
prefix=prefix,
|
|
24
|
-
path_contain=path_contain,
|
|
25
|
-
contain=contain,
|
|
26
|
-
sort_asc=False,
|
|
27
|
-
path_suffix=path_suffix,
|
|
28
|
-
suffix=suffix)
|
|
29
|
-
r1 = html_list[-1] if len(html_list) > 0 else None
|
|
30
|
-
if r1 is None:
|
|
31
|
-
return None
|
|
32
|
-
r1_short = r1
|
|
33
|
-
file_sep = '\\' if is_win() else '/'
|
|
34
|
-
if file_sep in r1:
|
|
35
|
-
r1_short = r1.split(file_sep)[-1]
|
|
36
|
-
if full_path is None:
|
|
37
|
-
return r1_short, r1
|
|
38
|
-
return r1 if full_path else r1_short
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
def to_print(*args,
|
|
42
|
-
time_prefix: bool = False,
|
|
43
|
-
line_with_space_count: int = None,
|
|
44
|
-
interval: int = None) -> str:
|
|
45
|
-
"""
|
|
46
|
-
记录日志, 如果是对象会转化为 json
|
|
47
|
-
数据直接 print, 不记录到文件
|
|
48
|
-
例如: aaa
|
|
49
|
-
interval: 间隔一段时间,打印一下, 单位: 秒,不要频繁打印
|
|
50
|
-
time_prefix : 是否在前面加时间, 默认 False
|
|
51
|
-
"""
|
|
52
|
-
d = ' '.join(map(lambda x: json.dumps(x) if is_json_serializable(x) else str(x), args))
|
|
53
|
-
d = d.strip()
|
|
54
|
-
lo = datetime.today().strftime('%Y-%m-%d %H:%M:%S') + ' ' + d if time_prefix is True else d
|
|
55
|
-
if lo is None or str(lo) == '':
|
|
56
|
-
lo = to_datetime(r_str=True)
|
|
57
|
-
prefix_space = ' ' * (line_with_space_count or 0)
|
|
58
|
-
if interval is None or get_timestamp() - get_thread_local_index_data().get('to_print_time', 0) >= interval:
|
|
59
|
-
get_thread_local_index_data()['to_print_time'] = get_timestamp()
|
|
60
|
-
s = ''
|
|
61
|
-
if interval is not None:
|
|
62
|
-
s = str(to_datetime()) + ' '
|
|
63
|
-
print(prefix_space + s + str(lo))
|
|
64
|
-
return prefix_space + lo
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
def to_log(*args,
|
|
68
|
-
time_prefix: bool = False,
|
|
69
|
-
line_with_space_count: int = None,
|
|
70
|
-
interval: int = None) -> str:
|
|
71
|
-
"""
|
|
72
|
-
记录日志, 如果是对象会转化为 json
|
|
73
|
-
前面加了时间
|
|
74
|
-
例如: 2024-11-07 10:23:47 aaa
|
|
75
|
-
"""
|
|
76
|
-
return to_print(*args,
|
|
77
|
-
time_prefix=time_prefix if time_prefix is not None else True,
|
|
78
|
-
line_with_space_count=line_with_space_count,
|
|
79
|
-
interval=interval)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
def to_print_file(*args,
|
|
83
|
-
file_path: str = None,
|
|
84
|
-
file_name: str = None,
|
|
85
|
-
file_name_with_date: bool = False,
|
|
86
|
-
file_name_prefix: str = None,
|
|
87
|
-
file_name_suffix: str = None,
|
|
88
|
-
line_with_space_count: int = None,
|
|
89
|
-
mode: str = 'a',
|
|
90
|
-
interval: int = None) -> str:
|
|
91
|
-
"""
|
|
92
|
-
将 print 数据, 写入到 print_file 文件
|
|
93
|
-
文件按照 日期自动创建
|
|
94
|
-
例如: print_file/2020-01-01.txt
|
|
95
|
-
to_print_file(query_string, mode='w', file_path=file_path, file_name=get_file_name(file_name=file_path, is_date=True))
|
|
96
|
-
"""
|
|
97
|
-
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
98
|
-
line_with_space_count] = get_and_save_data_to_thread(
|
|
99
|
-
file_path=file_path,
|
|
100
|
-
file_name=file_name,
|
|
101
|
-
file_name_prefix=file_name_prefix,
|
|
102
|
-
file_name_suffix=file_name_suffix,
|
|
103
|
-
interval=interval,
|
|
104
|
-
mode=mode,
|
|
105
|
-
file_name_with_date=file_name_with_date,
|
|
106
|
-
line_with_space_count=line_with_space_count,
|
|
107
|
-
fun_name=to_print_file
|
|
108
|
-
)
|
|
109
|
-
return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
|
|
110
|
-
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
111
|
-
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
112
|
-
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
113
|
-
file_path=str(file_path if file_path is not None else 'to_print_file'),
|
|
114
|
-
mode=mode,
|
|
115
|
-
fixed_name=True,
|
|
116
|
-
suffix='.txt')
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
def to_print_txt(*args,
|
|
120
|
-
file_path: str = None,
|
|
121
|
-
file_name_with_date: bool = False,
|
|
122
|
-
file_name: str = None,
|
|
123
|
-
file_name_prefix: str = None,
|
|
124
|
-
file_name_suffix: str = None,
|
|
125
|
-
line_with_space_count: int = None,
|
|
126
|
-
mode: str = 'a',
|
|
127
|
-
interval: int = None) -> str:
|
|
128
|
-
"""
|
|
129
|
-
将 print 数据, 写入到 print_txt 文件
|
|
130
|
-
文件按照 日期自动创建
|
|
131
|
-
例如: print_txt/2020-01-01.txt
|
|
132
|
-
"""
|
|
133
|
-
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
134
|
-
line_with_space_count] = get_and_save_data_to_thread(
|
|
135
|
-
file_path=file_path,
|
|
136
|
-
file_name=file_name,
|
|
137
|
-
file_name_prefix=file_name_prefix,
|
|
138
|
-
file_name_suffix=file_name_suffix,
|
|
139
|
-
interval=interval,
|
|
140
|
-
mode=mode,
|
|
141
|
-
file_name_with_date=file_name_with_date,
|
|
142
|
-
line_with_space_count=line_with_space_count,
|
|
143
|
-
fun_name=to_print_txt
|
|
144
|
-
)
|
|
145
|
-
return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
|
|
146
|
-
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
147
|
-
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
148
|
-
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
149
|
-
file_path=str(file_path if file_path is not None else 'to_print_txt'),
|
|
150
|
-
mode=mode,
|
|
151
|
-
fixed_name=True,
|
|
152
|
-
suffix='.txt')
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
def to_log_file(*args,
|
|
156
|
-
file_path: str = None,
|
|
157
|
-
file_name_with_date: bool = False,
|
|
158
|
-
file_name: str = None,
|
|
159
|
-
file_name_prefix: str = None,
|
|
160
|
-
file_name_suffix: str = None,
|
|
161
|
-
line_with_space_count: int = None,
|
|
162
|
-
time_prefix: bool = True,
|
|
163
|
-
mode: str = 'a',
|
|
164
|
-
interval: int = None) -> None:
|
|
165
|
-
"""
|
|
166
|
-
将 log 数据, 写入到 log_file 文件
|
|
167
|
-
文件按照 日期自动创建
|
|
168
|
-
例如: log_file/2020-01-01.log
|
|
169
|
-
"""
|
|
170
|
-
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
171
|
-
line_with_space_count] = get_and_save_data_to_thread(
|
|
172
|
-
file_path=file_path,
|
|
173
|
-
file_name=file_name,
|
|
174
|
-
file_name_prefix=file_name_prefix,
|
|
175
|
-
file_name_suffix=file_name_suffix,
|
|
176
|
-
interval=interval,
|
|
177
|
-
mode=mode,
|
|
178
|
-
file_name_with_date=file_name_with_date,
|
|
179
|
-
line_with_space_count=line_with_space_count,
|
|
180
|
-
fun_name=to_log_file
|
|
181
|
-
)
|
|
182
|
-
to_txt(data_param=[
|
|
183
|
-
to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
|
|
184
|
-
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
185
|
-
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
186
|
-
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
187
|
-
file_path=str(file_path if file_path is not None else 'to_log_file'),
|
|
188
|
-
fixed_name=True,
|
|
189
|
-
mode=mode,
|
|
190
|
-
suffix='.log')
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
def to_log_txt(*args,
|
|
194
|
-
file_path: str = None,
|
|
195
|
-
file_name_with_date: bool = False,
|
|
196
|
-
file_name: str = None,
|
|
197
|
-
file_name_prefix: str = None,
|
|
198
|
-
file_name_suffix: str = None,
|
|
199
|
-
line_with_space_count: int = None,
|
|
200
|
-
time_prefix: bool = True,
|
|
201
|
-
mode: str = 'a',
|
|
202
|
-
interval: int = None) -> None:
|
|
203
|
-
"""
|
|
204
|
-
将 log 数据, 写入到 log_txt 文件夹中
|
|
205
|
-
文件按照 日期自动创建
|
|
206
|
-
例如: log_txt/2020-01-01.txt
|
|
207
|
-
"""
|
|
208
|
-
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
209
|
-
line_with_space_count] = get_and_save_data_to_thread(
|
|
210
|
-
file_path=file_path,
|
|
211
|
-
file_name=file_name,
|
|
212
|
-
file_name_prefix=file_name_prefix,
|
|
213
|
-
file_name_suffix=file_name_suffix,
|
|
214
|
-
interval=interval,
|
|
215
|
-
mode=mode,
|
|
216
|
-
file_name_with_date=file_name_with_date,
|
|
217
|
-
line_with_space_count=line_with_space_count,
|
|
218
|
-
fun_name=to_log_txt
|
|
219
|
-
)
|
|
220
|
-
to_txt(data_param=[
|
|
221
|
-
to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
|
|
222
|
-
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
223
|
-
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
224
|
-
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
225
|
-
file_path=str(file_path if file_path is not None else 'to_log_txt'),
|
|
226
|
-
mode=mode,
|
|
227
|
-
fixed_name=True,
|
|
228
|
-
suffix='.txt')
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
def check_file(file_name: str = None) -> None:
|
|
232
|
-
r"""
|
|
233
|
-
检查文件夹是否存在,不存在,就创建新的
|
|
234
|
-
支持多级目录 , 例如: C:\Users\yangpu\Desktop\study\a\b\c\d\e\f
|
|
235
|
-
"""
|
|
236
|
-
if file_name is None or file_name == '':
|
|
237
|
-
return
|
|
238
|
-
file_sep = '\\' if is_win() else '/'
|
|
239
|
-
f_n = file_name.split(file_sep)
|
|
240
|
-
for i in range(1, len(f_n) + 1):
|
|
241
|
-
# C:\Users\yangpu\Desktop\study\p.t
|
|
242
|
-
p_n = file_sep.join(f_n[0:i])
|
|
243
|
-
if p_n and not os.path.exists(p_n):
|
|
244
|
-
os.mkdir(p_n)
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
def to_txt(data_param: Any,
|
|
248
|
-
file_name: str = 'txt',
|
|
249
|
-
file_path: str = 'txt',
|
|
250
|
-
fixed_name: bool = False,
|
|
251
|
-
mode: str = 'a',
|
|
252
|
-
suffix: str = '.txt',
|
|
253
|
-
sep_list: str = '\t',
|
|
254
|
-
file_name_is_date: bool = False) -> str:
|
|
255
|
-
r"""
|
|
256
|
-
将 list 中的数据以 json 或者基本类型的形式写入到文件中
|
|
257
|
-
data_param : 数组数据, 也可以不是数组
|
|
258
|
-
file_name : 文件名 , 默认 txt
|
|
259
|
-
当文件名是 C:\Users\yangpu\Desktop\study\abc\d\e\f\a.sql 这种类型的时候, 可以直接创建文件夹,
|
|
260
|
-
会赋值 file_name=a,
|
|
261
|
-
file_path=C:\Users\yangpu\Desktop\study\abc\d\e\f,
|
|
262
|
-
fixed_name=True,
|
|
263
|
-
suffix=.sql
|
|
264
|
-
当文件名是 abc 的时候, 按照正常值,计算
|
|
265
|
-
file_path : 文件路径
|
|
266
|
-
fixed_name : 是否固定文件名
|
|
267
|
-
suffix : 文件后缀, 默认 .txt
|
|
268
|
-
sep_list : 当 data_param 是 list(list) 类型的时候 使用 sep_list 作为分割内部的分隔符,
|
|
269
|
-
默认使用 \t 作为分隔符, 如果为 None , 则按照 json 去处理这个 list
|
|
270
|
-
"""
|
|
271
|
-
file_name = str(file_name)
|
|
272
|
-
for sep in ['\\', '/']:
|
|
273
|
-
f_n = file_name.split(sep)
|
|
274
|
-
if len(f_n) > 1:
|
|
275
|
-
file_name = f_n[-1]
|
|
276
|
-
file_path = sep.join(f_n[0:-1])
|
|
277
|
-
if '.' in file_name:
|
|
278
|
-
suffix = '.' + file_name.split('.')[-1]
|
|
279
|
-
file_name = file_name[0:file_name.rfind('.')]
|
|
280
|
-
fixed_name = True
|
|
281
|
-
|
|
282
|
-
# 检查路径 file_path
|
|
283
|
-
while file_path.endswith('/'):
|
|
284
|
-
file_path = file_path[0:-1]
|
|
285
|
-
check_file(file_path)
|
|
286
|
-
|
|
287
|
-
# 在 file_name 中, 检查是否有后缀
|
|
288
|
-
if '.' in file_name:
|
|
289
|
-
suffix = '.' + file_name.split('.')[-1]
|
|
290
|
-
file_name = file_name[0:file_name.rfind('.')]
|
|
291
|
-
|
|
292
|
-
# 生成 file_name
|
|
293
|
-
if fixed_name:
|
|
294
|
-
file_name = file_name + suffix
|
|
295
|
-
else:
|
|
296
|
-
file_name = get_file_name(file_name, suffix, is_date=file_name_is_date)
|
|
297
|
-
# 文件路径
|
|
298
|
-
file_name_path = file_name
|
|
299
|
-
if file_path != '':
|
|
300
|
-
file_name_path = file_path + '/' + file_name
|
|
301
|
-
# 写入文件
|
|
302
|
-
text_file = open(file_name_path, mode, encoding='utf-8')
|
|
303
|
-
if isinstance(data_param, set):
|
|
304
|
-
data_param = list(data_param)
|
|
305
|
-
if not isinstance(data_param, list):
|
|
306
|
-
text_file.write(to_str(data_param) + '\n')
|
|
307
|
-
else:
|
|
308
|
-
for one in data_param:
|
|
309
|
-
if isinstance(one, (list, tuple, set)) and sep_list is not None:
|
|
310
|
-
text_file.write(str(sep_list).join(list(map(lambda x: to_str(x), one))) + '\n')
|
|
311
|
-
else:
|
|
312
|
-
text_file.write(to_str(one) + '\n')
|
|
313
|
-
text_file.close()
|
|
314
|
-
return file_name_path
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
# 将 list 中的数据写入到固定的文件中,自己设置文件后缀
|
|
318
|
-
def to_txt_file(data_param: Any,
|
|
319
|
-
file_name: str = None,
|
|
320
|
-
mode: str = 'a') -> str:
|
|
321
|
-
file_name = get_file_name('to_txt_file', is_date=True) if file_name is None else file_name
|
|
322
|
-
suffix = '.txt'
|
|
323
|
-
f = file_name
|
|
324
|
-
for sep in ['\\', '/']:
|
|
325
|
-
f_n = file_name.split(sep)
|
|
326
|
-
if len(f_n) > 1:
|
|
327
|
-
f = file_name
|
|
328
|
-
if '.' in f:
|
|
329
|
-
suffix = '.' + f.split('.')[-1]
|
|
330
|
-
file_name = file_name.replace(suffix, '')
|
|
331
|
-
return to_txt(data_param=data_param,
|
|
332
|
-
file_name=file_name,
|
|
333
|
-
file_path='to_txt_file',
|
|
334
|
-
suffix=suffix,
|
|
335
|
-
fixed_name=True,
|
|
336
|
-
mode=mode)
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
# 将 list 中的数据写入到固定的文件中,自己设置文件后缀
|
|
340
|
-
def to_file(data_param: Any,
|
|
341
|
-
file_name: str = None,
|
|
342
|
-
mode: str = 'a') -> str:
|
|
343
|
-
file_name = get_file_name('to_file', is_date=True) if file_name is None else file_name
|
|
344
|
-
suffix = '.txt'
|
|
345
|
-
f = file_name
|
|
346
|
-
for sep in ['\\', '/']:
|
|
347
|
-
f_n = file_name.split(sep)
|
|
348
|
-
if len(f_n) > 1:
|
|
349
|
-
f = file_name
|
|
350
|
-
if '.' in f:
|
|
351
|
-
suffix = '.' + f.split('.')[-1]
|
|
352
|
-
file_name = file_name.replace(suffix, '')
|
|
353
|
-
return to_txt(data_param=data_param,
|
|
354
|
-
file_name=file_name,
|
|
355
|
-
file_path='file',
|
|
356
|
-
suffix=suffix,
|
|
357
|
-
fixed_name=True,
|
|
358
|
-
mode=mode)
|
|
359
4
|
|
|
360
5
|
|
|
361
6
|
def to_list(file_name: str = 'a.txt',
|
|
@@ -365,8 +10,9 @@ def to_list(file_name: str = 'a.txt',
|
|
|
365
10
|
sep_line_prefix: str = None,
|
|
366
11
|
sep_line_suffix: str = None,
|
|
367
12
|
sep_all: str = None,
|
|
368
|
-
|
|
369
|
-
|
|
13
|
+
line_ignore_start_with: list[str] | set[str] | str = None,
|
|
14
|
+
line_ignore_end_with: list[str] | set[str] | str | None = None,
|
|
15
|
+
line_ignore_empty: bool | None = None,
|
|
370
16
|
start_index: int = None,
|
|
371
17
|
start_line: str = None,
|
|
372
18
|
end_index: int = None,
|
|
@@ -410,8 +56,9 @@ def to_list(file_name: str = 'a.txt',
|
|
|
410
56
|
sep_line_prefix=sep_line_prefix,
|
|
411
57
|
sep_line_suffix=sep_line_suffix,
|
|
412
58
|
sep_all=sep_all,
|
|
413
|
-
|
|
414
|
-
|
|
59
|
+
line_ignore_start_with=line_ignore_start_with,
|
|
60
|
+
line_ignore_end_with=line_ignore_end_with,
|
|
61
|
+
line_ignore_empty=line_ignore_empty,
|
|
415
62
|
start_index=start_index,
|
|
416
63
|
start_line=start_line,
|
|
417
64
|
end_index=end_index,
|
|
@@ -820,16 +467,16 @@ def to_list_from_txt(file_name: str = 'a.txt',
|
|
|
820
467
|
# 读取文件中的数据,返回一个 str
|
|
821
468
|
def to_str_from_file(file_name: str = 'a.txt',
|
|
822
469
|
str_join: str = ' ',
|
|
823
|
-
|
|
824
|
-
|
|
470
|
+
line_ignore_start_with: list | int | str | None = None,
|
|
471
|
+
line_ignore_end_with: list | int | str | None = None,
|
|
825
472
|
start_index: int = None,
|
|
826
473
|
start_line: str = None,
|
|
827
474
|
end_index: int = None,
|
|
828
475
|
end_line: str = None,
|
|
829
476
|
count: int = None) -> str:
|
|
830
477
|
return to_data_from_file(file_name=file_name,
|
|
831
|
-
|
|
832
|
-
|
|
478
|
+
line_ignore_start_with=line_ignore_start_with,
|
|
479
|
+
line_ignore_end_with=line_ignore_end_with,
|
|
833
480
|
str_join=str_join,
|
|
834
481
|
start_index=start_index,
|
|
835
482
|
start_line=start_line,
|
|
@@ -851,7 +498,7 @@ def to_json_from_file(file_name: str = 'a.txt',
|
|
|
851
498
|
start_line=start_line,
|
|
852
499
|
end_index=end_index,
|
|
853
500
|
end_line=end_line,
|
|
854
|
-
|
|
501
|
+
line_ignore_start_with=['//', '/*', '#'],
|
|
855
502
|
count=count,
|
|
856
503
|
r_json=True)
|
|
857
504
|
|
|
@@ -860,8 +507,9 @@ def to_data_from_file(file_name: str = 'a.txt',
|
|
|
860
507
|
sep: str = None,
|
|
861
508
|
sep_line: str = None,
|
|
862
509
|
sep_all: str = None,
|
|
863
|
-
|
|
864
|
-
|
|
510
|
+
line_ignore_start_with: list | int | str | None = None,
|
|
511
|
+
line_ignore_end_with: list | int | str | None = None,
|
|
512
|
+
line_ignore_empty: bool | None = None,
|
|
865
513
|
start_index: int = None,
|
|
866
514
|
start_line: str = None,
|
|
867
515
|
end_index: int = None,
|
|
@@ -884,8 +532,9 @@ def to_data_from_file(file_name: str = 'a.txt',
|
|
|
884
532
|
sep=sep,
|
|
885
533
|
sep_line=sep_line,
|
|
886
534
|
sep_all=sep_all,
|
|
887
|
-
|
|
888
|
-
|
|
535
|
+
line_ignore_start_with=line_ignore_start_with,
|
|
536
|
+
line_ignore_end_with=line_ignore_end_with,
|
|
537
|
+
line_ignore_empty=line_ignore_empty,
|
|
889
538
|
start_index=start_index,
|
|
890
539
|
start_line=start_line,
|
|
891
540
|
end_index=end_index,
|
|
@@ -896,85 +545,3 @@ def to_data_from_file(file_name: str = 'a.txt',
|
|
|
896
545
|
column_date=column_date,
|
|
897
546
|
column_datetime=column_datetime)
|
|
898
547
|
return str_join.join(d) if r_str else json.loads(str_join.join(d)) if r_json else d
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
# 将文件导出成excel格式的
|
|
902
|
-
def to_excel(data_list: set | list | tuple | None,
|
|
903
|
-
file_name: str = None,
|
|
904
|
-
file_path: str = 'excel') -> None:
|
|
905
|
-
if file_name is None:
|
|
906
|
-
file_name = 'excel'
|
|
907
|
-
file_name = str(file_name)
|
|
908
|
-
while file_path.endswith('/'):
|
|
909
|
-
file_path = file_path[0:-1]
|
|
910
|
-
check_file(file_path)
|
|
911
|
-
# 实例化对象excel对象
|
|
912
|
-
excel_obj = openpyxl.Workbook()
|
|
913
|
-
# excel 内第一个sheet工作表
|
|
914
|
-
excel_obj_sheet = excel_obj[excel_obj.sheetnames[0]]
|
|
915
|
-
# 给单元格赋值
|
|
916
|
-
for one_data in data_list:
|
|
917
|
-
s_list = []
|
|
918
|
-
if isinstance(one_data, list) or isinstance(one_data, set):
|
|
919
|
-
for one in one_data:
|
|
920
|
-
if isinstance(one, dict) or isinstance(one, list):
|
|
921
|
-
s = json.dumps(one)
|
|
922
|
-
else:
|
|
923
|
-
s = str(one)
|
|
924
|
-
s_list.append(s)
|
|
925
|
-
excel_obj_sheet.append(s_list)
|
|
926
|
-
else:
|
|
927
|
-
if is_json_serializable(one_data):
|
|
928
|
-
s = json.dumps(one_data)
|
|
929
|
-
else:
|
|
930
|
-
s = str(one_data)
|
|
931
|
-
excel_obj_sheet.append([s])
|
|
932
|
-
|
|
933
|
-
# 文件保存
|
|
934
|
-
excel_obj.save(file_path + '/' + get_file_name(file_name, '.xlsx', True))
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
def to_csv(data_list: set | list | tuple | dict,
|
|
938
|
-
file_name: str = None,
|
|
939
|
-
file_path: str = 'csv') -> None:
|
|
940
|
-
"""
|
|
941
|
-
将文件导出成csv格式的
|
|
942
|
-
data_list 格式
|
|
943
|
-
data_list = [['Name', 'Age', 'Gender'],
|
|
944
|
-
['Alice', 25, 'Female'],
|
|
945
|
-
['Bob', 30, 'Male'],
|
|
946
|
-
['Charlie', 35, 'Male']]
|
|
947
|
-
data_list = [{
|
|
948
|
-
"a": 1,
|
|
949
|
-
"b": 2,
|
|
950
|
-
},{
|
|
951
|
-
"a": 1,
|
|
952
|
-
"b": 2,
|
|
953
|
-
}]
|
|
954
|
-
file_name = 'data'
|
|
955
|
-
"""
|
|
956
|
-
if file_name is None:
|
|
957
|
-
file_name = 'csv'
|
|
958
|
-
file_name = get_file_name(file_name, '.csv', True)
|
|
959
|
-
while file_path.endswith('/'):
|
|
960
|
-
file_path = file_path[0:-1]
|
|
961
|
-
check_file(file_path)
|
|
962
|
-
d_list = []
|
|
963
|
-
if isinstance(data_list, tuple):
|
|
964
|
-
d_list = list(data_list)
|
|
965
|
-
else:
|
|
966
|
-
if len(data_list) and (isinstance(data_list[0], dict) or isinstance(data_list[0], tuple)):
|
|
967
|
-
title_list = []
|
|
968
|
-
for key in data_list[0]:
|
|
969
|
-
title_list.append(key)
|
|
970
|
-
d_list.append(title_list)
|
|
971
|
-
for one_data in data_list:
|
|
972
|
-
one_list = []
|
|
973
|
-
for k in title_list:
|
|
974
|
-
one_list.append(one_data[k])
|
|
975
|
-
d_list.append(one_list)
|
|
976
|
-
else:
|
|
977
|
-
d_list = data_list
|
|
978
|
-
with open(file_path + '/' + file_name, 'w', newline='') as f:
|
|
979
|
-
writer = csv.writer(f)
|
|
980
|
-
writer.writerows(d_list)
|
upplib/index.py
CHANGED
|
@@ -2,12 +2,8 @@ from upplib import *
|
|
|
2
2
|
from datetime import datetime, timezone, timedelta
|
|
3
3
|
from typing import Any, Optional, Union
|
|
4
4
|
from upplib.common_package import *
|
|
5
|
-
from upplib.file import get_file
|
|
6
5
|
from collections import defaultdict
|
|
7
6
|
|
|
8
|
-
__CONFIG_PATH = '.upp.config'
|
|
9
|
-
|
|
10
|
-
__CONFIG_PATH_BAK = '_upp.config'
|
|
11
7
|
|
|
12
8
|
# 创建一个线程本地存储对象
|
|
13
9
|
__THREAD_LOCAL_INDEX_DATA = threading.local()
|
|
@@ -566,70 +562,6 @@ def format_sorted_kv_string(data_obj: Any,
|
|
|
566
562
|
return sep.join(result)
|
|
567
563
|
|
|
568
564
|
|
|
569
|
-
# 将数据写入到 config 中
|
|
570
|
-
def set_config_data(file_name: str = 'config',
|
|
571
|
-
param: dict[str, Any] = None) -> None:
|
|
572
|
-
set_data_in_user_home(file_name, {} if param is None else param)
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
# 从 config 中获得 配置数据
|
|
576
|
-
def get_config_data(file_name: str = 'config') -> dict[str, Any]:
|
|
577
|
-
# print('get_config_data', file_name)
|
|
578
|
-
config_data = get_data_from_user_home(file_name)
|
|
579
|
-
# print('get_data_from_user_home', config_data)
|
|
580
|
-
if not config_data:
|
|
581
|
-
config_data = get_data_from_path(file_name)
|
|
582
|
-
# print('get_data_from_path', config_data)
|
|
583
|
-
return config_data
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
# 在当前用户的主目录中, 获得指定文件的数据
|
|
587
|
-
def get_data_from_user_home(file_name: str = 'config') -> dict[str, Any]:
|
|
588
|
-
return get_data_from_path(file_name, os.path.expanduser("~"))
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
# 将 data 数据,在当前用户的主目录中, 获得指定文件的数据
|
|
592
|
-
def set_data_in_user_home(file_name: str = 'config',
|
|
593
|
-
param: dict[str, Any] = None) -> None:
|
|
594
|
-
set_data_in_path(file_name, {} if param is None else param, os.path.expanduser("~"))
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
# 在当前的目录中, 获得指定文件的数据
|
|
598
|
-
def get_data_from_path(file_name: str = 'config',
|
|
599
|
-
file_path: str = None) -> dict[str, Any]:
|
|
600
|
-
param = get_data_from_path_detail(file_name, file_path, __CONFIG_PATH)
|
|
601
|
-
return param if param else get_data_from_path_detail(file_name, file_path, __CONFIG_PATH_BAK)
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
def get_data_from_path_detail(file_name: str = 'config',
|
|
605
|
-
file_path: str = None,
|
|
606
|
-
path_name: str = __CONFIG_PATH) -> dict[str, Any]:
|
|
607
|
-
config_path = file_path + '/' + path_name if file_path else path_name
|
|
608
|
-
# print('config_path_1', config_path)
|
|
609
|
-
if not os.path.exists(config_path):
|
|
610
|
-
# print('config_path_2', config_path)
|
|
611
|
-
return {}
|
|
612
|
-
file_path = config_path + '/' + file_name + '.json'
|
|
613
|
-
# print('config_path_3', file_path)
|
|
614
|
-
if not os.path.exists(file_path):
|
|
615
|
-
return {}
|
|
616
|
-
# print('to_json_from_file', file_path)
|
|
617
|
-
return to_json_from_file(file_path)
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
# 在当前的目录中, 设置数据到指定路径下
|
|
621
|
-
def set_data_in_path(file_name: str = 'config',
|
|
622
|
-
param: str = None,
|
|
623
|
-
file_path: str = '') -> None:
|
|
624
|
-
config_path = file_path + '/' + __CONFIG_PATH
|
|
625
|
-
if not os.path.exists(config_path):
|
|
626
|
-
os.mkdir(config_path)
|
|
627
|
-
file_path = config_path + '/' + file_name + '.json'
|
|
628
|
-
text_file = open(file_path, 'w', encoding='utf-8')
|
|
629
|
-
text_file.write(to_str({} if param is None else param))
|
|
630
|
-
text_file.close()
|
|
631
|
-
|
|
632
|
-
|
|
633
565
|
# 执行命令
|
|
634
566
|
def exec_command(command: str = None) -> None:
|
|
635
567
|
if command is None:
|
upplib/query_log.py
CHANGED
upplib/text_to_file.py
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
from upplib import *
|
|
2
|
+
from upplib.common_package import *
|
|
3
|
+
from upplib.file import get_file
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def to_print(*args,
|
|
7
|
+
time_prefix: bool = False,
|
|
8
|
+
line_with_space_count: int = None,
|
|
9
|
+
interval: int = None) -> str:
|
|
10
|
+
"""
|
|
11
|
+
记录日志, 如果是对象会转化为 json
|
|
12
|
+
数据直接 print, 不记录到文件
|
|
13
|
+
例如: aaa
|
|
14
|
+
interval: 间隔一段时间,打印一下, 单位: 秒,不要频繁打印
|
|
15
|
+
time_prefix : 是否在前面加时间, 默认 False
|
|
16
|
+
"""
|
|
17
|
+
d = ' '.join(map(lambda x: json.dumps(x) if is_json_serializable(x) else str(x), args))
|
|
18
|
+
d = d.strip()
|
|
19
|
+
lo = datetime.today().strftime('%Y-%m-%d %H:%M:%S') + ' ' + d if time_prefix is True else d
|
|
20
|
+
if lo is None or str(lo) == '':
|
|
21
|
+
lo = to_datetime(r_str=True)
|
|
22
|
+
prefix_space = ' ' * (line_with_space_count or 0)
|
|
23
|
+
if interval is None or get_timestamp() - get_thread_local_index_data().get('to_print_time', 0) >= interval:
|
|
24
|
+
get_thread_local_index_data()['to_print_time'] = get_timestamp()
|
|
25
|
+
s = ''
|
|
26
|
+
if interval is not None:
|
|
27
|
+
s = str(to_datetime()) + ' '
|
|
28
|
+
print(prefix_space + s + str(lo))
|
|
29
|
+
return prefix_space + lo
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def to_log(*args,
|
|
33
|
+
time_prefix: bool = False,
|
|
34
|
+
line_with_space_count: int = None,
|
|
35
|
+
interval: int = None) -> str:
|
|
36
|
+
"""
|
|
37
|
+
记录日志, 如果是对象会转化为 json
|
|
38
|
+
前面加了时间
|
|
39
|
+
例如: 2024-11-07 10:23:47 aaa
|
|
40
|
+
"""
|
|
41
|
+
return to_print(*args,
|
|
42
|
+
time_prefix=time_prefix if time_prefix is not None else True,
|
|
43
|
+
line_with_space_count=line_with_space_count,
|
|
44
|
+
interval=interval)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def to_print_file(*args,
|
|
48
|
+
file_path: str = None,
|
|
49
|
+
file_name: str = None,
|
|
50
|
+
file_name_with_date: bool = False,
|
|
51
|
+
file_name_prefix: str = None,
|
|
52
|
+
file_name_suffix: str = None,
|
|
53
|
+
line_with_space_count: int = None,
|
|
54
|
+
mode: str = 'a',
|
|
55
|
+
interval: int = None) -> str:
|
|
56
|
+
"""
|
|
57
|
+
将 print 数据, 写入到 print_file 文件
|
|
58
|
+
文件按照 日期自动创建
|
|
59
|
+
例如: print_file/2020-01-01.txt
|
|
60
|
+
to_print_file(query_string, mode='w', file_path=file_path, file_name=get_file_name(file_name=file_path, is_date=True))
|
|
61
|
+
"""
|
|
62
|
+
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
63
|
+
line_with_space_count] = get_and_save_data_to_thread(
|
|
64
|
+
file_path=file_path,
|
|
65
|
+
file_name=file_name,
|
|
66
|
+
file_name_prefix=file_name_prefix,
|
|
67
|
+
file_name_suffix=file_name_suffix,
|
|
68
|
+
interval=interval,
|
|
69
|
+
mode=mode,
|
|
70
|
+
file_name_with_date=file_name_with_date,
|
|
71
|
+
line_with_space_count=line_with_space_count,
|
|
72
|
+
fun_name=to_print_file
|
|
73
|
+
)
|
|
74
|
+
return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
|
|
75
|
+
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
76
|
+
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
77
|
+
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
78
|
+
file_path=str(file_path if file_path is not None else 'to_print_file'),
|
|
79
|
+
mode=mode,
|
|
80
|
+
fixed_name=True,
|
|
81
|
+
suffix='.txt')
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def to_print_txt(*args,
|
|
85
|
+
file_path: str = None,
|
|
86
|
+
file_name_with_date: bool = False,
|
|
87
|
+
file_name: str = None,
|
|
88
|
+
file_name_prefix: str = None,
|
|
89
|
+
file_name_suffix: str = None,
|
|
90
|
+
line_with_space_count: int = None,
|
|
91
|
+
mode: str = 'a',
|
|
92
|
+
interval: int = None) -> str:
|
|
93
|
+
"""
|
|
94
|
+
将 print 数据, 写入到 print_txt 文件
|
|
95
|
+
文件按照 日期自动创建
|
|
96
|
+
例如: print_txt/2020-01-01.txt
|
|
97
|
+
"""
|
|
98
|
+
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
99
|
+
line_with_space_count] = get_and_save_data_to_thread(
|
|
100
|
+
file_path=file_path,
|
|
101
|
+
file_name=file_name,
|
|
102
|
+
file_name_prefix=file_name_prefix,
|
|
103
|
+
file_name_suffix=file_name_suffix,
|
|
104
|
+
interval=interval,
|
|
105
|
+
mode=mode,
|
|
106
|
+
file_name_with_date=file_name_with_date,
|
|
107
|
+
line_with_space_count=line_with_space_count,
|
|
108
|
+
fun_name=to_print_txt
|
|
109
|
+
)
|
|
110
|
+
return to_txt(data_param=[to_print(*args, line_with_space_count=line_with_space_count, interval=interval)],
|
|
111
|
+
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
112
|
+
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
113
|
+
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
114
|
+
file_path=str(file_path if file_path is not None else 'to_print_txt'),
|
|
115
|
+
mode=mode,
|
|
116
|
+
fixed_name=True,
|
|
117
|
+
suffix='.txt')
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def to_log_file(*args,
|
|
121
|
+
file_path: str = None,
|
|
122
|
+
file_name_with_date: bool = False,
|
|
123
|
+
file_name: str = None,
|
|
124
|
+
file_name_prefix: str = None,
|
|
125
|
+
file_name_suffix: str = None,
|
|
126
|
+
line_with_space_count: int = None,
|
|
127
|
+
time_prefix: bool = True,
|
|
128
|
+
mode: str = 'a',
|
|
129
|
+
interval: int = None) -> None:
|
|
130
|
+
"""
|
|
131
|
+
将 log 数据, 写入到 log_file 文件
|
|
132
|
+
文件按照 日期自动创建
|
|
133
|
+
例如: log_file/2020-01-01.log
|
|
134
|
+
"""
|
|
135
|
+
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
136
|
+
line_with_space_count] = get_and_save_data_to_thread(
|
|
137
|
+
file_path=file_path,
|
|
138
|
+
file_name=file_name,
|
|
139
|
+
file_name_prefix=file_name_prefix,
|
|
140
|
+
file_name_suffix=file_name_suffix,
|
|
141
|
+
interval=interval,
|
|
142
|
+
mode=mode,
|
|
143
|
+
file_name_with_date=file_name_with_date,
|
|
144
|
+
line_with_space_count=line_with_space_count,
|
|
145
|
+
fun_name=to_log_file
|
|
146
|
+
)
|
|
147
|
+
to_txt(data_param=[
|
|
148
|
+
to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
|
|
149
|
+
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
150
|
+
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
151
|
+
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
152
|
+
file_path=str(file_path if file_path is not None else 'to_log_file'),
|
|
153
|
+
fixed_name=True,
|
|
154
|
+
mode=mode,
|
|
155
|
+
suffix='.log')
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def to_log_txt(*args,
|
|
159
|
+
file_path: str = None,
|
|
160
|
+
file_name_with_date: bool = False,
|
|
161
|
+
file_name: str = None,
|
|
162
|
+
file_name_prefix: str = None,
|
|
163
|
+
file_name_suffix: str = None,
|
|
164
|
+
line_with_space_count: int = None,
|
|
165
|
+
time_prefix: bool = True,
|
|
166
|
+
mode: str = 'a',
|
|
167
|
+
interval: int = None) -> None:
|
|
168
|
+
"""
|
|
169
|
+
将 log 数据, 写入到 log_txt 文件夹中
|
|
170
|
+
文件按照 日期自动创建
|
|
171
|
+
例如: log_txt/2020-01-01.txt
|
|
172
|
+
"""
|
|
173
|
+
[file_path, file_name, file_name_prefix, file_name_suffix, interval,
|
|
174
|
+
line_with_space_count] = get_and_save_data_to_thread(
|
|
175
|
+
file_path=file_path,
|
|
176
|
+
file_name=file_name,
|
|
177
|
+
file_name_prefix=file_name_prefix,
|
|
178
|
+
file_name_suffix=file_name_suffix,
|
|
179
|
+
interval=interval,
|
|
180
|
+
mode=mode,
|
|
181
|
+
file_name_with_date=file_name_with_date,
|
|
182
|
+
line_with_space_count=line_with_space_count,
|
|
183
|
+
fun_name=to_log_txt
|
|
184
|
+
)
|
|
185
|
+
to_txt(data_param=[
|
|
186
|
+
to_log(*args, time_prefix=time_prefix, line_with_space_count=line_with_space_count, interval=interval)],
|
|
187
|
+
file_name=('' if file_name_prefix is None else file_name_prefix)
|
|
188
|
+
+ (datetime.today().strftime('%Y-%m-%d') if file_name is None else file_name)
|
|
189
|
+
+ ('' if file_name_suffix is None else file_name_suffix),
|
|
190
|
+
file_path=str(file_path if file_path is not None else 'to_log_txt'),
|
|
191
|
+
mode=mode,
|
|
192
|
+
fixed_name=True,
|
|
193
|
+
suffix='.txt')
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def to_txt(data_param: Any,
|
|
197
|
+
file_name: str = 'txt',
|
|
198
|
+
file_path: str = 'txt',
|
|
199
|
+
fixed_name: bool = False,
|
|
200
|
+
mode: str = 'a',
|
|
201
|
+
suffix: str = '.txt',
|
|
202
|
+
sep_list: str = '\t',
|
|
203
|
+
file_name_is_date: bool = False) -> str:
|
|
204
|
+
r"""
|
|
205
|
+
将 list 中的数据以 json 或者基本类型的形式写入到文件中
|
|
206
|
+
data_param : 数组数据, 也可以不是数组
|
|
207
|
+
file_name : 文件名 , 默认 txt
|
|
208
|
+
当文件名是 C:\Users\yangpu\Desktop\study\abc\d\e\f\a.sql 这种类型的时候, 可以直接创建文件夹,
|
|
209
|
+
会赋值 file_name=a,
|
|
210
|
+
file_path=C:\Users\yangpu\Desktop\study\abc\d\e\f,
|
|
211
|
+
fixed_name=True,
|
|
212
|
+
suffix=.sql
|
|
213
|
+
当文件名是 abc 的时候, 按照正常值,计算
|
|
214
|
+
file_path : 文件路径
|
|
215
|
+
fixed_name : 是否固定文件名
|
|
216
|
+
suffix : 文件后缀, 默认 .txt
|
|
217
|
+
sep_list : 当 data_param 是 list(list) 类型的时候 使用 sep_list 作为分割内部的分隔符,
|
|
218
|
+
默认使用 \t 作为分隔符, 如果为 None , 则按照 json 去处理这个 list
|
|
219
|
+
"""
|
|
220
|
+
file_name = str(file_name)
|
|
221
|
+
for sep in ['\\', '/']:
|
|
222
|
+
f_n = file_name.split(sep)
|
|
223
|
+
if len(f_n) > 1:
|
|
224
|
+
file_name = f_n[-1]
|
|
225
|
+
file_path = sep.join(f_n[0:-1])
|
|
226
|
+
if '.' in file_name:
|
|
227
|
+
suffix = '.' + file_name.split('.')[-1]
|
|
228
|
+
file_name = file_name[0:file_name.rfind('.')]
|
|
229
|
+
fixed_name = True
|
|
230
|
+
|
|
231
|
+
# 检查路径 file_path
|
|
232
|
+
while file_path.endswith('/'):
|
|
233
|
+
file_path = file_path[0:-1]
|
|
234
|
+
check_file(file_path)
|
|
235
|
+
|
|
236
|
+
# 在 file_name 中, 检查是否有后缀
|
|
237
|
+
if '.' in file_name:
|
|
238
|
+
suffix = '.' + file_name.split('.')[-1]
|
|
239
|
+
file_name = file_name[0:file_name.rfind('.')]
|
|
240
|
+
|
|
241
|
+
# 生成 file_name
|
|
242
|
+
if fixed_name:
|
|
243
|
+
file_name = file_name + suffix
|
|
244
|
+
else:
|
|
245
|
+
file_name = get_file_name(file_name, suffix, is_date=file_name_is_date)
|
|
246
|
+
# 文件路径
|
|
247
|
+
file_name_path = file_name
|
|
248
|
+
if file_path != '':
|
|
249
|
+
file_name_path = file_path + '/' + file_name
|
|
250
|
+
# 写入文件
|
|
251
|
+
text_file = open(file_name_path, mode, encoding='utf-8')
|
|
252
|
+
if isinstance(data_param, set):
|
|
253
|
+
data_param = list(data_param)
|
|
254
|
+
if not isinstance(data_param, list):
|
|
255
|
+
text_file.write(to_str(data_param) + '\n')
|
|
256
|
+
else:
|
|
257
|
+
for one in data_param:
|
|
258
|
+
if isinstance(one, (list, tuple, set)) and sep_list is not None:
|
|
259
|
+
text_file.write(str(sep_list).join(list(map(lambda x: to_str(x), one))) + '\n')
|
|
260
|
+
else:
|
|
261
|
+
text_file.write(to_str(one) + '\n')
|
|
262
|
+
text_file.close()
|
|
263
|
+
return file_name_path
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
# 将 list 中的数据写入到固定的文件中,自己设置文件后缀
|
|
267
|
+
def to_txt_file(data_param: Any,
|
|
268
|
+
file_name: str = None,
|
|
269
|
+
mode: str = 'a') -> str:
|
|
270
|
+
file_name = get_file_name('to_txt_file', is_date=True) if file_name is None else file_name
|
|
271
|
+
suffix = '.txt'
|
|
272
|
+
f = file_name
|
|
273
|
+
for sep in ['\\', '/']:
|
|
274
|
+
f_n = file_name.split(sep)
|
|
275
|
+
if len(f_n) > 1:
|
|
276
|
+
f = file_name
|
|
277
|
+
if '.' in f:
|
|
278
|
+
suffix = '.' + f.split('.')[-1]
|
|
279
|
+
file_name = file_name.replace(suffix, '')
|
|
280
|
+
return to_txt(data_param=data_param,
|
|
281
|
+
file_name=file_name,
|
|
282
|
+
file_path='to_txt_file',
|
|
283
|
+
suffix=suffix,
|
|
284
|
+
fixed_name=True,
|
|
285
|
+
mode=mode)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
# 将 list 中的数据写入到固定的文件中,自己设置文件后缀
|
|
289
|
+
def to_file(data_param: Any,
|
|
290
|
+
file_name: str = None,
|
|
291
|
+
mode: str = 'a') -> str:
|
|
292
|
+
file_name = get_file_name('to_file', is_date=True) if file_name is None else file_name
|
|
293
|
+
suffix = '.txt'
|
|
294
|
+
f = file_name
|
|
295
|
+
for sep in ['\\', '/']:
|
|
296
|
+
f_n = file_name.split(sep)
|
|
297
|
+
if len(f_n) > 1:
|
|
298
|
+
f = file_name
|
|
299
|
+
if '.' in f:
|
|
300
|
+
suffix = '.' + f.split('.')[-1]
|
|
301
|
+
file_name = file_name.replace(suffix, '')
|
|
302
|
+
return to_txt(data_param=data_param,
|
|
303
|
+
file_name=file_name,
|
|
304
|
+
file_path='file',
|
|
305
|
+
suffix=suffix,
|
|
306
|
+
fixed_name=True,
|
|
307
|
+
mode=mode)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
# 将文件导出成excel格式的
|
|
311
|
+
def to_excel(data_list: set | list | tuple | None,
|
|
312
|
+
file_name: str = None,
|
|
313
|
+
file_path: str = 'excel') -> None:
|
|
314
|
+
if file_name is None:
|
|
315
|
+
file_name = 'excel'
|
|
316
|
+
file_name = str(file_name)
|
|
317
|
+
while file_path.endswith('/'):
|
|
318
|
+
file_path = file_path[0:-1]
|
|
319
|
+
check_file(file_path)
|
|
320
|
+
# 实例化对象excel对象
|
|
321
|
+
excel_obj = openpyxl.Workbook()
|
|
322
|
+
# excel 内第一个sheet工作表
|
|
323
|
+
excel_obj_sheet = excel_obj[excel_obj.sheetnames[0]]
|
|
324
|
+
# 给单元格赋值
|
|
325
|
+
for one_data in data_list:
|
|
326
|
+
s_list = []
|
|
327
|
+
if isinstance(one_data, list) or isinstance(one_data, set):
|
|
328
|
+
for one in one_data:
|
|
329
|
+
if isinstance(one, dict) or isinstance(one, list):
|
|
330
|
+
s = json.dumps(one)
|
|
331
|
+
else:
|
|
332
|
+
s = str(one)
|
|
333
|
+
s_list.append(s)
|
|
334
|
+
excel_obj_sheet.append(s_list)
|
|
335
|
+
else:
|
|
336
|
+
if is_json_serializable(one_data):
|
|
337
|
+
s = json.dumps(one_data)
|
|
338
|
+
else:
|
|
339
|
+
s = str(one_data)
|
|
340
|
+
excel_obj_sheet.append([s])
|
|
341
|
+
|
|
342
|
+
# 文件保存
|
|
343
|
+
excel_obj.save(file_path + '/' + get_file_name(file_name, '.xlsx', True))
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def to_csv(data_list: set | list | tuple | dict,
|
|
347
|
+
file_name: str = None,
|
|
348
|
+
file_path: str = 'csv') -> None:
|
|
349
|
+
"""
|
|
350
|
+
将文件导出成csv格式的
|
|
351
|
+
data_list 格式
|
|
352
|
+
data_list = [['Name', 'Age', 'Gender'],
|
|
353
|
+
['Alice', 25, 'Female'],
|
|
354
|
+
['Bob', 30, 'Male'],
|
|
355
|
+
['Charlie', 35, 'Male']]
|
|
356
|
+
data_list = [{
|
|
357
|
+
"a": 1,
|
|
358
|
+
"b": 2,
|
|
359
|
+
},{
|
|
360
|
+
"a": 1,
|
|
361
|
+
"b": 2,
|
|
362
|
+
}]
|
|
363
|
+
file_name = 'data'
|
|
364
|
+
"""
|
|
365
|
+
if file_name is None:
|
|
366
|
+
file_name = 'csv'
|
|
367
|
+
file_name = get_file_name(file_name, '.csv', True)
|
|
368
|
+
while file_path.endswith('/'):
|
|
369
|
+
file_path = file_path[0:-1]
|
|
370
|
+
check_file(file_path)
|
|
371
|
+
d_list = []
|
|
372
|
+
if isinstance(data_list, tuple):
|
|
373
|
+
d_list = list(data_list)
|
|
374
|
+
else:
|
|
375
|
+
if len(data_list) and (isinstance(data_list[0], dict) or isinstance(data_list[0], tuple)):
|
|
376
|
+
title_list = []
|
|
377
|
+
for key in data_list[0]:
|
|
378
|
+
title_list.append(key)
|
|
379
|
+
d_list.append(title_list)
|
|
380
|
+
for one_data in data_list:
|
|
381
|
+
one_list = []
|
|
382
|
+
for k in title_list:
|
|
383
|
+
one_list.append(one_data[k])
|
|
384
|
+
d_list.append(one_list)
|
|
385
|
+
else:
|
|
386
|
+
d_list = data_list
|
|
387
|
+
with open(file_path + '/' + file_name, 'w', newline='') as f:
|
|
388
|
+
writer = csv.writer(f)
|
|
389
|
+
writer.writerows(d_list)
|
upplib/util.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from upplib import *
|
|
2
|
-
from upplib.
|
|
2
|
+
from upplib.file_text import *
|
|
3
3
|
from datetime import datetime, timezone, timedelta
|
|
4
4
|
from typing import Any, Optional, Union, Callable, List
|
|
5
5
|
|
|
@@ -50,8 +50,7 @@ def get_from_txt(file_name: str = '_start_time_end_time_str.txt',
|
|
|
50
50
|
second : 获得时间,在 second 基础上,前后冗余多少秒
|
|
51
51
|
获得日志
|
|
52
52
|
"""
|
|
53
|
-
date_list = to_list_from_txt(file_name)
|
|
54
|
-
date_list = list(filter(lambda x: len(x) > 0 and not str(x).strip().startswith('#'), date_list))
|
|
53
|
+
date_list = to_list_from_txt(file_name, line_ignore_start_with='#')
|
|
55
54
|
if len(date_list) == 0:
|
|
56
55
|
return None, None
|
|
57
56
|
date_time_list = []
|
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
upplib/__init__.py,sha256=
|
|
1
|
+
upplib/__init__.py,sha256=prPH1HV9IQLl0embjkv1OvdrCMmiTFemWonWvA8q7g4,934
|
|
2
2
|
upplib/chart.py,sha256=DEcfhr1HlWdITaPWTVlL0CVSr3GiJTV_bWyOPb1UoyA,27667
|
|
3
3
|
upplib/chart_html.py,sha256=39CTD2LbN7LNN3wtb3n-ojN2r_X7RJMWappPgiLSJG4,23144
|
|
4
4
|
upplib/clean_up_msg.py,sha256=yR4D2fIXOT8z9VpClfhBjVnjhexObgNZDhvPu5v_5Y4,19237
|
|
5
5
|
upplib/common_package.py,sha256=2-u66k4WbZnX6-PUaYy8wsblnz41nvtX4Hwo3J5hjO0,832
|
|
6
|
+
upplib/config_data.py,sha256=y-2S70V0jwXJXxXz2vsqCK--Heay27VnA7MGpO3a40o,2748
|
|
6
7
|
upplib/db.py,sha256=-3ZALQSzP92-zIhye6ckrXIctbHsVmwoLiCVOlTyDyo,7058
|
|
7
|
-
upplib/file.py,sha256=
|
|
8
|
-
upplib/
|
|
8
|
+
upplib/file.py,sha256=OtVGnmUPd7UO6OJgZjlLgzGMXlftGt94o-_C6-aG2k8,10407
|
|
9
|
+
upplib/file_to_text.py,sha256=N0ENo-iDL7VM7txoLYO-pYNPGoNv6KJkZf5nkVnwuAM,26789
|
|
9
10
|
upplib/format_data.py,sha256=Qxq-OZ8v6HBRJ-tUcNFem1Imbauk3Y5qJTrAGT0CC4I,10153
|
|
10
11
|
upplib/http_util.py,sha256=8Xya5qpeCKGaglxKnirOlOnbnBU3K52FGV5Y9c8HH9k,16119
|
|
11
|
-
upplib/index.py,sha256=
|
|
12
|
+
upplib/index.py,sha256=8m1aDWEFa7_-hvsIToxFKz2VtxMNVrm1GyrQEbFF6aQ,23190
|
|
12
13
|
upplib/mail.py,sha256=pe1ZZlKsah_TiB_Ag3scgiIQegyOznul2awdHur2m4Y,7550
|
|
13
14
|
upplib/mail_html.py,sha256=W2_WT9WhFykmAFageboJ-6E0dnBHBGMG1vQlBb9__M8,4001
|
|
14
15
|
upplib/markdown.py,sha256=YXh2Rtb5gvZ755Ecr3r5FcMoDtd2YcqtOFHFv92jWOQ,5803
|
|
15
16
|
upplib/multi_thread.py,sha256=zOeWG5HGYIzwqiPvef-4ak5bzorf0S3q6Ht8wPit_M0,2705
|
|
16
|
-
upplib/query_log.py,sha256=
|
|
17
|
+
upplib/query_log.py,sha256=BAUAVr3lA_GbDzHNjI5beJPs0ngNmy2gCi5Q6UMdr1s,9238
|
|
17
18
|
upplib/redis_tool.py,sha256=I1kOqBwfQWVIOAY-hQaaOn1Zrx8BNlK83u-pk4uHPCA,707
|
|
18
|
-
upplib/
|
|
19
|
-
upplib
|
|
20
|
-
upplib-3.
|
|
21
|
-
upplib-3.
|
|
22
|
-
upplib-3.
|
|
23
|
-
upplib-3.
|
|
19
|
+
upplib/text_to_file.py,sha256=NgRnasZPIDg4nTRGyWgEv2XvOWQkI2K6BgnDITF1Sy0,14909
|
|
20
|
+
upplib/util.py,sha256=oCQ84njl96A8RGnMqFl3W-2mLZEqcBmpOrK-GDCTmWk,4633
|
|
21
|
+
upplib-3.3.8.dist-info/licenses/LICENSE,sha256=WI5JtXXhjcqnIcPllDA1ZtuxNnZ515xjElcILo7z28o,1073
|
|
22
|
+
upplib-3.3.8.dist-info/METADATA,sha256=YClgEgWCS-OW1XWidi4X_6stnkJUG8u2caTmV6eMGug,940
|
|
23
|
+
upplib-3.3.8.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
24
|
+
upplib-3.3.8.dist-info/top_level.txt,sha256=VwdHDDPP79e1LqtRu5_w30hHB4gT0zlj1weuQYOqFoA,7
|
|
25
|
+
upplib-3.3.8.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|