oafuncs 0.0.81__py2.py3-none-any.whl → 0.0.83__py2.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.
- oafuncs/data_store/OAFuncs.png +0 -0
- oafuncs/oa_cmap.py +1 -0
- oafuncs/oa_data.py +107 -28
- oafuncs/oa_down/__init__.py +5 -4
- oafuncs/oa_down/hycom_3hourly.py +152 -35
- oafuncs/oa_down/user_agent.py +34 -0
- oafuncs/oa_draw.py +165 -103
- oafuncs/oa_file.py +66 -53
- oafuncs/oa_help.py +19 -16
- oafuncs/oa_nc.py +82 -114
- oafuncs-0.0.83.dist-info/METADATA +91 -0
- oafuncs-0.0.83.dist-info/RECORD +26 -0
- oafuncs/oa_down/test.py +0 -151
- oafuncs/oa_s/__init__.py +0 -23
- oafuncs/oa_s/oa_cmap.py +0 -163
- oafuncs/oa_s/oa_data.py +0 -187
- oafuncs/oa_s/oa_draw.py +0 -451
- oafuncs/oa_s/oa_file.py +0 -332
- oafuncs/oa_s/oa_help.py +0 -39
- oafuncs/oa_s/oa_nc.py +0 -410
- oafuncs/oa_s/oa_python.py +0 -107
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/__init__.py" +0 -26
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_cmap.py" +0 -163
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_data.py" +0 -187
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/__init__.py" +0 -20
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/hycom_3hourly.py" +0 -1176
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/literature.py" +0 -332
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_down/test_ua.py" +0 -151
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_draw.py" +0 -451
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_file.py" +0 -332
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_help.py" +0 -39
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_nc.py" +0 -410
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_python.py" +0 -107
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/__init__.py" +0 -21
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/meteorological.py" +0 -168
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/ocean.py" +0 -158
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_sign/scientific.py" +0 -139
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_tool/__init__.py" +0 -18
- oafuncs - /321/205/320/231/320/277/321/206/320/254/320/274/oa_tool/email.py" +0 -114
- oafuncs-0.0.81.dist-info/METADATA +0 -918
- oafuncs-0.0.81.dist-info/RECORD +0 -51
- {oafuncs-0.0.81.dist-info → oafuncs-0.0.83.dist-info}/LICENSE.txt +0 -0
- {oafuncs-0.0.81.dist-info → oafuncs-0.0.83.dist-info}/WHEEL +0 -0
- {oafuncs-0.0.81.dist-info → oafuncs-0.0.83.dist-info}/top_level.txt +0 -0
oafuncs/oa_s/oa_file.py
DELETED
@@ -1,332 +0,0 @@
|
|
1
|
-
#!/usr/bin/env python
|
2
|
-
# coding=utf-8
|
3
|
-
'''
|
4
|
-
Author: Liu Kun && 16031215@qq.com
|
5
|
-
Date: 2024-09-17 15:07:13
|
6
|
-
LastEditors: Liu Kun && 16031215@qq.com
|
7
|
-
LastEditTime: 2024-12-02 10:33:19
|
8
|
-
FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\oa_file.py
|
9
|
-
Description:
|
10
|
-
EditPlatform: vscode
|
11
|
-
ComputerInfo: XPS 15 9510
|
12
|
-
SystemInfo: Windows 11
|
13
|
-
Python Version: 3.11
|
14
|
-
'''
|
15
|
-
|
16
|
-
|
17
|
-
import glob
|
18
|
-
import os
|
19
|
-
import re
|
20
|
-
import shutil
|
21
|
-
|
22
|
-
__all__ = ['find_file', 'link_file', 'copy_file', 'rename_file', 'make_folder', 'clear_folder', 'remove_empty_folders', 'remove', 'file_size']
|
23
|
-
|
24
|
-
|
25
|
-
def find_file(parent_path, fname, mode='path'):
|
26
|
-
'''
|
27
|
-
description:
|
28
|
-
param {*} parent_path: The parent path where the files are located
|
29
|
-
param {*} fname: The file name pattern to search for
|
30
|
-
param {*} mode: 'path' to return the full path of the files, 'file' to return only the file names
|
31
|
-
return {*} A list of file paths or file names if files are found, None otherwise
|
32
|
-
'''
|
33
|
-
def natural_sort_key(s):
|
34
|
-
"""生成一个用于自然排序的键"""
|
35
|
-
return [int(text) if text.isdigit() else text.lower() for text in re.split('([0-9]+)', s)]
|
36
|
-
|
37
|
-
# 将parent_path和fname结合成完整的搜索路径
|
38
|
-
search_pattern = os.path.join(str(parent_path), fname)
|
39
|
-
|
40
|
-
# 使用glob模块查找所有匹配的文件
|
41
|
-
matched_files = glob.glob(search_pattern)
|
42
|
-
|
43
|
-
# 如果没有找到任何文件,则返回False
|
44
|
-
if not matched_files:
|
45
|
-
return None
|
46
|
-
|
47
|
-
# 在find_files函数中替换natsorted调用
|
48
|
-
matched_files = sorted(matched_files, key=natural_sort_key)
|
49
|
-
|
50
|
-
# 根据mode参数决定返回的内容
|
51
|
-
if mode == 'file':
|
52
|
-
# 只返回文件名
|
53
|
-
result = [os.path.basename(file) for file in matched_files]
|
54
|
-
else: # 默认为'path'
|
55
|
-
# 返回文件的绝对路径
|
56
|
-
result = [os.path.abspath(file) for file in matched_files]
|
57
|
-
|
58
|
-
return result
|
59
|
-
|
60
|
-
|
61
|
-
def link_file(src_pattern, dst):
|
62
|
-
'''
|
63
|
-
# 描述:创建符号链接,支持通配符
|
64
|
-
# 使用示例
|
65
|
-
# link_file(r'/data/hejx/liukun/era5/*', r'/data/hejx/liukun/Test/')
|
66
|
-
# link_file(r'/data/hejx/liukun/era5/py.o*', r'/data/hejx/liukun/Test/py.o')
|
67
|
-
# link_file(r'/data/hejx/liukun/era5/py.o*', r'/data/hejx/liukun/Test')
|
68
|
-
param {*} src_pattern # 源文件或目录
|
69
|
-
param {*} dst # 目标文件或目录
|
70
|
-
'''
|
71
|
-
src_pattern = str(src_pattern)
|
72
|
-
# 使用glob.glob来处理可能包含通配符的src
|
73
|
-
src_files = glob.glob(src_pattern)
|
74
|
-
if not src_files:
|
75
|
-
raise FileNotFoundError('文件不存在: {}'.format(src_pattern))
|
76
|
-
|
77
|
-
# 判断dst是路径还是包含文件名的路径
|
78
|
-
if os.path.isdir(dst):
|
79
|
-
# 如果dst是路径,则保持源文件的文件名
|
80
|
-
dst_dir = dst
|
81
|
-
for src_file in src_files:
|
82
|
-
src_file_basename = os.path.basename(src_file)
|
83
|
-
dst_file = os.path.join(dst_dir, src_file_basename)
|
84
|
-
if os.path.exists(dst_file):
|
85
|
-
os.remove(dst_file)
|
86
|
-
os.symlink(src_file, dst_file)
|
87
|
-
print(f'创建符号链接: {src_file} -> {dst_file}')
|
88
|
-
else:
|
89
|
-
# 如果dst包含文件名,则创建链接后重命名
|
90
|
-
dst_dir = os.path.dirname(dst)
|
91
|
-
os.makedirs(dst_dir, exist_ok=True)
|
92
|
-
# 只处理第一个匹配的文件
|
93
|
-
src_file = src_files[0]
|
94
|
-
dst_file = dst
|
95
|
-
if os.path.exists(dst_file):
|
96
|
-
os.remove(dst_file)
|
97
|
-
os.symlink(src_file, dst_file)
|
98
|
-
print(f'创建符号链接并重命名: {src_file} -> {dst_file}')
|
99
|
-
|
100
|
-
|
101
|
-
def copy_file(src_pattern, dst):
|
102
|
-
'''
|
103
|
-
# 描述:复制文件或目录,支持通配符
|
104
|
-
# 使用示例
|
105
|
-
# copy_file(r'/data/hejx/liukun/era5/py.o*', r'/data/hejx/liukun/Test/py.o')
|
106
|
-
# copy_file(r'/data/hejx/liukun/era5/py.o*', r'/data/hejx/liukun/Test')
|
107
|
-
param {*} src_pattern # 源文件或目录
|
108
|
-
param {*} dst # 目标文件或目录
|
109
|
-
'''
|
110
|
-
src_pattern = str(src_pattern)
|
111
|
-
# 使用glob.glob来处理可能包含通配符的src
|
112
|
-
src_files = glob.glob(src_pattern)
|
113
|
-
if not src_files:
|
114
|
-
raise FileNotFoundError('文件不存在: {}'.format(src_pattern))
|
115
|
-
|
116
|
-
# 判断dst是路径还是包含文件名的路径
|
117
|
-
if os.path.isdir(dst):
|
118
|
-
# 如果dst是路径,则保持源文件的文件名
|
119
|
-
dst_dir = dst
|
120
|
-
for src_file in src_files:
|
121
|
-
src_file_basename = os.path.basename(src_file)
|
122
|
-
dst_file = os.path.join(dst_dir, src_file_basename)
|
123
|
-
if os.path.exists(dst_file):
|
124
|
-
if os.path.isdir(dst_file):
|
125
|
-
shutil.rmtree(dst_file)
|
126
|
-
else:
|
127
|
-
os.remove(dst_file)
|
128
|
-
if os.path.isdir(src_file):
|
129
|
-
shutil.copytree(src_file, dst_file, symlinks=True)
|
130
|
-
else:
|
131
|
-
shutil.copy2(src_file, dst_file)
|
132
|
-
print(f'复制文件或目录: {src_file} -> {dst_file}')
|
133
|
-
else:
|
134
|
-
# 如果dst包含文件名,则复制后重命名
|
135
|
-
dst_dir = os.path.dirname(dst)
|
136
|
-
os.makedirs(dst_dir, exist_ok=True)
|
137
|
-
# 只处理第一个匹配的文件
|
138
|
-
src_file = src_files[0]
|
139
|
-
dst_file = dst
|
140
|
-
if os.path.exists(dst_file):
|
141
|
-
if os.path.isdir(dst_file):
|
142
|
-
shutil.rmtree(dst_file)
|
143
|
-
else:
|
144
|
-
os.remove(dst_file)
|
145
|
-
if os.path.isdir(src_file):
|
146
|
-
shutil.copytree(src_file, dst_file, symlinks=True)
|
147
|
-
else:
|
148
|
-
shutil.copy2(src_file, dst_file)
|
149
|
-
print(f'复制文件或目录并重命名: {src_file} -> {dst_file}')
|
150
|
-
|
151
|
-
|
152
|
-
def rename_file(directory, old_str, new_str):
|
153
|
-
'''
|
154
|
-
# 描述:重命名目录下的文件,支持通配符
|
155
|
-
# 使用示例
|
156
|
-
directory_path = r"E:\windfarm\CROCO_FILES"
|
157
|
-
old_str = "croco"
|
158
|
-
new_str = "roms"
|
159
|
-
rename_file(directory_path, old_str, new_str)
|
160
|
-
param {*} directory # 目录
|
161
|
-
param {*} old_str # 要替换的字符串
|
162
|
-
param {*} new_str # 新字符串
|
163
|
-
'''
|
164
|
-
# 获取目录下的所有文件
|
165
|
-
files = os.listdir(directory)
|
166
|
-
|
167
|
-
# 构建正则表达式以匹配要替换的字符串
|
168
|
-
pattern = re.compile(re.escape(old_str))
|
169
|
-
|
170
|
-
# 遍历目录下的文件
|
171
|
-
for filename in files:
|
172
|
-
# 检查文件名中是否包含要替换的字符串
|
173
|
-
if pattern.search(filename):
|
174
|
-
# 构建新的文件名
|
175
|
-
new_filename = pattern.sub(new_str, filename)
|
176
|
-
|
177
|
-
# 构建旧文件的完整路径
|
178
|
-
old_path = os.path.join(directory, filename)
|
179
|
-
|
180
|
-
# 构建新文件的完整路径
|
181
|
-
new_path = os.path.join(directory, new_filename)
|
182
|
-
|
183
|
-
# 重命名文件
|
184
|
-
os.rename(old_path, new_path)
|
185
|
-
print(f"重命名文件: {old_path} -> {new_path}")
|
186
|
-
|
187
|
-
|
188
|
-
# ** 创建子文件夹(可选清空)
|
189
|
-
def make_folder(rootpath: str, folder_name: str, clear=0) -> str:
|
190
|
-
'''
|
191
|
-
# 描述:创建子文件夹(可选清空)
|
192
|
-
# 使用示例
|
193
|
-
rootpath = r'E:\Data\2024\09\17'
|
194
|
-
folder_name = 'var1'
|
195
|
-
newpath = make_folder(rootpath, folder_name, clear=1)
|
196
|
-
param {*} rootpath # 根目录
|
197
|
-
param {*} folder_name # 文件夹名称
|
198
|
-
'''
|
199
|
-
folder_path = os.path.join(str(rootpath), str(folder_name))
|
200
|
-
if clear:
|
201
|
-
shutil.rmtree(folder_path, ignore_errors=True)
|
202
|
-
os.makedirs(folder_path, exist_ok=True)
|
203
|
-
return folder_path
|
204
|
-
|
205
|
-
# ** 清空文件夹
|
206
|
-
|
207
|
-
|
208
|
-
def clear_folder(folder_path):
|
209
|
-
'''
|
210
|
-
# 描述:清空文件夹
|
211
|
-
# 使用示例
|
212
|
-
clear_folder(r'E:\Data\2024\09\17\var1')
|
213
|
-
param {*} folder_path # 文件夹路径
|
214
|
-
'''
|
215
|
-
folder_path = str(folder_path)
|
216
|
-
if os.path.exists(folder_path):
|
217
|
-
try:
|
218
|
-
# 遍历文件夹中的所有文件和子文件夹
|
219
|
-
for filename in os.listdir(folder_path):
|
220
|
-
file_path = os.path.join(folder_path, filename)
|
221
|
-
# 判断是文件还是文件夹
|
222
|
-
if os.path.isfile(file_path) or os.path.islink(file_path):
|
223
|
-
os.unlink(file_path) # 删除文件或链接
|
224
|
-
elif os.path.isdir(file_path):
|
225
|
-
shutil.rmtree(file_path) # 删除子文件夹
|
226
|
-
print(f'成功清空文件夹: {folder_path}')
|
227
|
-
except Exception as e:
|
228
|
-
print(f'清空文件夹失败: {folder_path}')
|
229
|
-
print(e)
|
230
|
-
|
231
|
-
|
232
|
-
# ** 清理空文件夹
|
233
|
-
def remove_empty_folders(path, print_info=1):
|
234
|
-
'''
|
235
|
-
# 描述:清理空文件夹
|
236
|
-
# 使用示例
|
237
|
-
remove_empty_folders(r'E:\Data\2024\09\17', print_info=1)
|
238
|
-
param {*} path # 文件夹路径
|
239
|
-
param {*} print_info # 是否打印信息
|
240
|
-
'''
|
241
|
-
path = str(path)
|
242
|
-
# 遍历当前目录下的所有文件夹和文件
|
243
|
-
for root, dirs, files in os.walk(path, topdown=False):
|
244
|
-
# 遍历文件夹列表
|
245
|
-
for folder in dirs:
|
246
|
-
folder_path = os.path.join(root, folder)
|
247
|
-
# 判断文件是否有权限访问
|
248
|
-
try:
|
249
|
-
os.listdir(folder_path)
|
250
|
-
except OSError:
|
251
|
-
continue
|
252
|
-
# 判断文件夹是否为空
|
253
|
-
if not os.listdir(folder_path):
|
254
|
-
# 删除空文件夹
|
255
|
-
try:
|
256
|
-
os.rmdir(folder_path)
|
257
|
-
print(f"Deleted empty folder: {folder_path}")
|
258
|
-
except OSError:
|
259
|
-
if print_info:
|
260
|
-
print(f"Skipping protected folder: {folder_path}")
|
261
|
-
pass
|
262
|
-
|
263
|
-
|
264
|
-
# ** 删除相关文件,可使用通配符
|
265
|
-
def remove(pattern):
|
266
|
-
'''
|
267
|
-
# 描述:删除相关文件,可使用通配符
|
268
|
-
remove(r'E:\Code\Python\Model\WRF\Radar2\bzip2-radar-0*')
|
269
|
-
# or
|
270
|
-
os.chdir(r'E:\Code\Python\Model\WRF\Radar2')
|
271
|
-
remove('bzip2-radar-0*')
|
272
|
-
param {*} pattern # 文件路径或通配符
|
273
|
-
'''
|
274
|
-
# 使用glob.glob来获取所有匹配的文件
|
275
|
-
# 可以使用通配符*来匹配所有文件
|
276
|
-
pattern = str(pattern)
|
277
|
-
file_list = glob.glob(pattern)
|
278
|
-
for file_path in file_list:
|
279
|
-
if os.path.exists(file_path):
|
280
|
-
try:
|
281
|
-
shutil.rmtree(file_path)
|
282
|
-
print(f'成功删除文件: {file_path}')
|
283
|
-
except Exception as e:
|
284
|
-
print(f'删除文件失败: {file_path}')
|
285
|
-
print(e)
|
286
|
-
else:
|
287
|
-
print(f'文件不存在: {file_path}')
|
288
|
-
|
289
|
-
|
290
|
-
def file_size(file_path, unit='KB'):
|
291
|
-
'''
|
292
|
-
Description: 获取文件大小
|
293
|
-
|
294
|
-
Args:
|
295
|
-
file_path: 文件路径
|
296
|
-
unit: 单位(PB、TB、GB、MB、KB)
|
297
|
-
|
298
|
-
Returns:
|
299
|
-
文件大小(单位:PB、TB、GB、MB、KB)
|
300
|
-
'''
|
301
|
-
# 检查文件是否存在
|
302
|
-
if not os.path.exists(file_path):
|
303
|
-
return "文件不存在"
|
304
|
-
|
305
|
-
# 获取文件大小(字节)
|
306
|
-
file_size = os.path.getsize(file_path)
|
307
|
-
|
308
|
-
# 单位转换字典
|
309
|
-
unit_dict = {
|
310
|
-
'PB': 1024**5,
|
311
|
-
'TB': 1024**4,
|
312
|
-
'GB': 1024**3,
|
313
|
-
'MB': 1024**2,
|
314
|
-
'KB': 1024
|
315
|
-
}
|
316
|
-
|
317
|
-
# 检查传入的单位是否合法
|
318
|
-
if unit not in unit_dict:
|
319
|
-
return "单位不合法,请选择PB、TB、GB、MB、KB中的一个"
|
320
|
-
|
321
|
-
# 转换文件大小到指定单位
|
322
|
-
converted_size = file_size / unit_dict[unit]
|
323
|
-
|
324
|
-
return converted_size
|
325
|
-
|
326
|
-
|
327
|
-
if __name__ == '__main__':
|
328
|
-
# newpath = make_folder('D:/Data/2024/09/17/', 'var1', clear=1)
|
329
|
-
# print(newpath)
|
330
|
-
pass
|
331
|
-
|
332
|
-
remove(r'I:\Delete\test\*')
|
oafuncs/oa_s/oa_help.py
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
#!/usr/bin/env python
|
2
|
-
# coding=utf-8
|
3
|
-
'''
|
4
|
-
Author: Liu Kun && 16031215@qq.com
|
5
|
-
Date: 2024-10-06 19:25:29
|
6
|
-
LastEditors: Liu Kun && 16031215@qq.com
|
7
|
-
LastEditTime: 2024-11-21 12:59:18
|
8
|
-
FilePath: \\Python\\My_Funcs\\OAFuncs\\oafuncs\\oa_help.py
|
9
|
-
Description:
|
10
|
-
EditPlatform: vscode
|
11
|
-
ComputerInfo: XPS 15 9510
|
12
|
-
SystemInfo: Windows 11
|
13
|
-
Python Version: 3.11
|
14
|
-
'''
|
15
|
-
|
16
|
-
import oafuncs
|
17
|
-
|
18
|
-
def query():
|
19
|
-
'''
|
20
|
-
description: 查看OAFuncs模块的函数列表
|
21
|
-
example: query()
|
22
|
-
'''
|
23
|
-
funcs = [func for func in dir(oafuncs) if callable(getattr(oafuncs, func))]
|
24
|
-
print('函数数量:')
|
25
|
-
print(len(funcs))
|
26
|
-
print('函数列表:')
|
27
|
-
print(funcs)
|
28
|
-
|
29
|
-
|
30
|
-
def use(func='get_var'):
|
31
|
-
'''
|
32
|
-
description: 查看函数的模块全路径和函数提示
|
33
|
-
param {func} : 函数名
|
34
|
-
example: use('get_var')
|
35
|
-
'''
|
36
|
-
print('模块全路径:')
|
37
|
-
print(getattr(oafuncs, func).__module__+'.'+func)
|
38
|
-
print('函数提示:')
|
39
|
-
print(getattr(oafuncs, func).__doc__)
|