0.1.48 增加装饰器
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# Module: data_processing (including figure-plotting and file-reading/writing)
|
||||
import guan
|
||||
|
||||
# 导入plt, fig, ax
|
||||
@guan.function_decorator
|
||||
def import_plt_and_start_fig_ax(adjust_bottom=0.2, adjust_left=0.2, labelsize=20):
|
||||
import matplotlib.pyplot as plt
|
||||
fig, ax = plt.subplots()
|
||||
@@ -9,11 +11,10 @@ def import_plt_and_start_fig_ax(adjust_bottom=0.2, adjust_left=0.2, labelsize=20
|
||||
ax.tick_params(labelsize=labelsize)
|
||||
labels = ax.get_xticklabels() + ax.get_yticklabels()
|
||||
[label.set_fontname('Times New Roman') for label in labels]
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return plt, fig, ax
|
||||
|
||||
# 基于plt, fig, ax画图
|
||||
@guan.function_decorator
|
||||
def plot_without_starting_fig(plt, fig, ax, x_array, y_array, xlabel='x', ylabel='y', title='', fontsize=20, style='', y_min=None, y_max=None, linewidth=None, markersize=None, color=None):
|
||||
if color==None:
|
||||
ax.plot(x_array, y_array, style, linewidth=linewidth, markersize=markersize)
|
||||
@@ -28,10 +29,9 @@ def plot_without_starting_fig(plt, fig, ax, x_array, y_array, xlabel='x', ylabel
|
||||
if y_max==None:
|
||||
y_max=max(y_array)
|
||||
ax.set_ylim(y_min, y_max)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 画图
|
||||
@guan.function_decorator
|
||||
def plot(x_array, y_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style='', y_min=None, y_max=None, linewidth=None, markersize=None, adjust_bottom=0.2, adjust_left=0.2):
|
||||
import guan
|
||||
plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize)
|
||||
@@ -50,9 +50,9 @@ def plot(x_array, y_array, xlabel='x', ylabel='y', title='', fontsize=20, labels
|
||||
if show == 1:
|
||||
plt.show()
|
||||
plt.close('all')
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 一组横坐标数据,两组纵坐标数据画图
|
||||
@guan.function_decorator
|
||||
def plot_two_array(x_array, y1_array, y2_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style_1='', style_2='', y_min=None, y_max=None, linewidth_1=None, linewidth_2=None, markersize_1=None, markersize_2=None, adjust_bottom=0.2, adjust_left=0.2):
|
||||
import guan
|
||||
plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize)
|
||||
@@ -76,9 +76,9 @@ def plot_two_array(x_array, y1_array, y2_array, xlabel='x', ylabel='y', title=''
|
||||
if show == 1:
|
||||
plt.show()
|
||||
plt.close('all')
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 两组横坐标数据,两组纵坐标数据画图
|
||||
@guan.function_decorator
|
||||
def plot_two_array_with_two_horizontal_array(x1_array, x2_array, y1_array, y2_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style_1='', style_2='', y_min=None, y_max=None, linewidth_1=None, linewidth_2=None, markersize_1=None, markersize_2=None, adjust_bottom=0.2, adjust_left=0.2):
|
||||
import guan
|
||||
plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize)
|
||||
@@ -102,9 +102,9 @@ def plot_two_array_with_two_horizontal_array(x1_array, x2_array, y1_array, y2_ar
|
||||
if show == 1:
|
||||
plt.show()
|
||||
plt.close('all')
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 一组横坐标数据,三组纵坐标数据画图
|
||||
@guan.function_decorator
|
||||
def plot_three_array(x_array, y1_array, y2_array, y3_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style_1='', style_2='', style_3='', y_min=None, y_max=None, linewidth_1=None, linewidth_2=None, linewidth_3=None,markersize_1=None, markersize_2=None, markersize_3=None, adjust_bottom=0.2, adjust_left=0.2):
|
||||
import guan
|
||||
plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize)
|
||||
@@ -131,9 +131,9 @@ def plot_three_array(x_array, y1_array, y2_array, y3_array, xlabel='x', ylabel='
|
||||
if show == 1:
|
||||
plt.show()
|
||||
plt.close('all')
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 三组横坐标数据,三组纵坐标数据画图
|
||||
@guan.function_decorator
|
||||
def plot_three_array_with_three_horizontal_array(x1_array, x2_array, x3_array, y1_array, y2_array, y3_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style_1='', style_2='', style_3='', y_min=None, y_max=None, linewidth_1=None, linewidth_2=None, linewidth_3=None,markersize_1=None, markersize_2=None, markersize_3=None, adjust_bottom=0.2, adjust_left=0.2):
|
||||
import guan
|
||||
plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize)
|
||||
@@ -160,9 +160,9 @@ def plot_three_array_with_three_horizontal_array(x1_array, x2_array, x3_array, y
|
||||
if show == 1:
|
||||
plt.show()
|
||||
plt.close('all')
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 画三维图
|
||||
@guan.function_decorator
|
||||
def plot_3d_surface(x_array, y_array, matrix, xlabel='x', ylabel='y', zlabel='z', title='', fontsize=20, labelsize=15, show=1, save=0, filename='a', file_format='.jpg', dpi=300, z_min=None, z_max=None, rcount=100, ccount=100):
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -202,10 +202,9 @@ def plot_3d_surface(x_array, y_array, matrix, xlabel='x', ylabel='y', zlabel='z'
|
||||
if show == 1:
|
||||
plt.show()
|
||||
plt.close('all')
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 画Contour图
|
||||
@guan.function_decorator
|
||||
def plot_contour(x_array, y_array, matrix, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=15, cmap='jet', levels=None, show=1, save=0, filename='a', file_format='.jpg', dpi=300):
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -229,10 +228,9 @@ def plot_contour(x_array, y_array, matrix, xlabel='x', ylabel='y', title='', fon
|
||||
if show == 1:
|
||||
plt.show()
|
||||
plt.close('all')
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 画棋盘图/伪彩色图
|
||||
@guan.function_decorator
|
||||
def plot_pcolor(x_array, y_array, matrix, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=15, cmap='jet', levels=None, show=1, save=0, filename='a', file_format='.jpg', dpi=300):
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -256,10 +254,9 @@ def plot_pcolor(x_array, y_array, matrix, xlabel='x', ylabel='y', title='', font
|
||||
if show == 1:
|
||||
plt.show()
|
||||
plt.close('all')
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 通过坐标画点和线
|
||||
@guan.function_decorator
|
||||
def draw_dots_and_lines(coordinate_array, draw_dots=1, draw_lines=1, max_distance=1.1, line_style='-k', linewidth=1, dot_style='ro', markersize=3, show=1, save=0, filename='a', file_format='.eps', dpi=300):
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -285,10 +282,9 @@ def draw_dots_and_lines(coordinate_array, draw_dots=1, draw_lines=1, max_distanc
|
||||
plt.savefig(filename+file_format)
|
||||
else:
|
||||
plt.savefig(filename+file_format, dpi=dpi)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 合并两个图片
|
||||
@guan.function_decorator
|
||||
def combine_two_images(image_path_array, figsize=(16,8), show=0, save=1, filename='a', file_format='.jpg', dpi=300):
|
||||
import numpy as np
|
||||
num = np.array(image_path_array).shape[0]
|
||||
@@ -312,10 +308,9 @@ def combine_two_images(image_path_array, figsize=(16,8), show=0, save=1, filenam
|
||||
if save == 1:
|
||||
plt.savefig(filename+file_format, dpi=dpi)
|
||||
plt.close('all')
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 合并三个图片
|
||||
@guan.function_decorator
|
||||
def combine_three_images(image_path_array, figsize=(16,5), show=0, save=1, filename='a', file_format='.jpg', dpi=300):
|
||||
import numpy as np
|
||||
num = np.array(image_path_array).shape[0]
|
||||
@@ -343,10 +338,9 @@ def combine_three_images(image_path_array, figsize=(16,5), show=0, save=1, filen
|
||||
if save == 1:
|
||||
plt.savefig(filename+file_format, dpi=dpi)
|
||||
plt.close('all')
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 合并四个图片
|
||||
@guan.function_decorator
|
||||
def combine_four_images(image_path_array, figsize=(16,16), show=0, save=1, filename='a', file_format='.jpg', dpi=300):
|
||||
import numpy as np
|
||||
num = np.array(image_path_array).shape[0]
|
||||
@@ -378,10 +372,9 @@ def combine_four_images(image_path_array, figsize=(16,16), show=0, save=1, filen
|
||||
if save == 1:
|
||||
plt.savefig(filename+file_format, dpi=dpi)
|
||||
plt.close('all')
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 对某个目录中的txt文件批量读取和画图
|
||||
@guan.function_decorator
|
||||
def batch_reading_and_plotting(directory, xlabel='x', ylabel='y'):
|
||||
import re
|
||||
import os
|
||||
@@ -392,9 +385,9 @@ def batch_reading_and_plotting(directory, xlabel='x', ylabel='y'):
|
||||
filename = file[:-4]
|
||||
x_array, y_array = guan.read_one_dimensional_data(filename=filename)
|
||||
guan.plot(x_array, y_array, xlabel=xlabel, ylabel=ylabel, title=filename, show=0, save=1, filename=filename)
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 将图片制作GIF动画
|
||||
@guan.function_decorator
|
||||
def make_gif(image_path_array, filename='a', duration=0.1):
|
||||
import imageio
|
||||
images = []
|
||||
@@ -402,34 +395,30 @@ def make_gif(image_path_array, filename='a', duration=0.1):
|
||||
im = imageio.imread(image_path)
|
||||
images.append(im)
|
||||
imageio.mimsave(filename+'.gif', images, 'GIF', duration=duration)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 选取Matplotlib颜色
|
||||
@guan.function_decorator
|
||||
def color_matplotlib():
|
||||
color_array = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan']
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return color_array
|
||||
|
||||
# 将变量存到文件
|
||||
@guan.function_decorator
|
||||
def dump_data(data, filename, file_format='.txt'):
|
||||
import pickle
|
||||
with open(filename+file_format, 'wb') as f:
|
||||
pickle.dump(data, f)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 从文件中恢复数据到变量
|
||||
@guan.function_decorator
|
||||
def load_data(filename, file_format='.txt'):
|
||||
import pickle
|
||||
with open(filename+file_format, 'rb') as f:
|
||||
data = pickle.load(f)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return data
|
||||
|
||||
# 读取文件中的一维数据(一行一组x和y)
|
||||
@guan.function_decorator
|
||||
def read_one_dimensional_data(filename='a', file_format='.txt'):
|
||||
import numpy as np
|
||||
f = open(filename+file_format, 'r')
|
||||
@@ -450,11 +439,10 @@ def read_one_dimensional_data(filename='a', file_format='.txt'):
|
||||
y_array = [y_row]
|
||||
else:
|
||||
y_array = np.append(y_array, [y_row], axis=0)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return x_array, y_array
|
||||
|
||||
# 读取文件中的一维数据(一行一组x和y)(支持复数形式)
|
||||
@guan.function_decorator
|
||||
def read_one_dimensional_complex_data(filename='a', file_format='.txt'):
|
||||
import numpy as np
|
||||
f = open(filename+file_format, 'r')
|
||||
@@ -475,11 +463,10 @@ def read_one_dimensional_complex_data(filename='a', file_format='.txt'):
|
||||
y_array = [y_row]
|
||||
else:
|
||||
y_array = np.append(y_array, [y_row], axis=0)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return x_array, y_array
|
||||
|
||||
# 读取文件中的二维数据(第一行和第一列分别为横纵坐标)
|
||||
@guan.function_decorator
|
||||
def read_two_dimensional_data(filename='a', file_format='.txt'):
|
||||
import numpy as np
|
||||
f = open(filename+file_format, 'r')
|
||||
@@ -506,11 +493,10 @@ def read_two_dimensional_data(filename='a', file_format='.txt'):
|
||||
matrix = [matrix_row]
|
||||
else:
|
||||
matrix = np.append(matrix, [matrix_row], axis=0)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return x_array, y_array, matrix
|
||||
|
||||
# 读取文件中的二维数据(第一行和第一列分别为横纵坐标)(支持复数形式)
|
||||
@guan.function_decorator
|
||||
def read_two_dimensional_complex_data(filename='a', file_format='.txt'):
|
||||
import numpy as np
|
||||
f = open(filename+file_format, 'r')
|
||||
@@ -537,34 +523,30 @@ def read_two_dimensional_complex_data(filename='a', file_format='.txt'):
|
||||
matrix = [matrix_row]
|
||||
else:
|
||||
matrix = np.append(matrix, [matrix_row], axis=0)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return x_array, y_array, matrix
|
||||
|
||||
# 读取文件中的二维数据(不包括x和y)
|
||||
@guan.function_decorator
|
||||
def read_two_dimensional_data_without_xy_array(filename='a', file_format='.txt'):
|
||||
import numpy as np
|
||||
matrix = np.loadtxt(filename+file_format)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return matrix
|
||||
|
||||
# 打开文件用于新增内容
|
||||
@guan.function_decorator
|
||||
def open_file(filename='a', file_format='.txt'):
|
||||
f = open(filename+file_format, 'a', encoding='UTF-8')
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return f
|
||||
|
||||
# 在文件中写入一维数据(一行一组x和y)
|
||||
@guan.function_decorator
|
||||
def write_one_dimensional_data(x_array, y_array, filename='a', file_format='.txt'):
|
||||
import guan
|
||||
with open(filename+file_format, 'w', encoding='UTF-8') as f:
|
||||
guan.write_one_dimensional_data_without_opening_file(x_array, y_array, f)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 在文件中写入一维数据(一行一组x和y)(需要输入已打开的文件)
|
||||
@guan.function_decorator
|
||||
def write_one_dimensional_data_without_opening_file(x_array, y_array, f):
|
||||
import numpy as np
|
||||
x_array = np.array(x_array)
|
||||
@@ -579,17 +561,16 @@ def write_one_dimensional_data_without_opening_file(x_array, y_array, f):
|
||||
f.write(str(y_array[i0, j0])+' ')
|
||||
f.write('\n')
|
||||
i0 += 1
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 在文件中写入二维数据(第一行和第一列分别为横纵坐标)
|
||||
@guan.function_decorator
|
||||
def write_two_dimensional_data(x_array, y_array, matrix, filename='a', file_format='.txt'):
|
||||
import guan
|
||||
with open(filename+file_format, 'w', encoding='UTF-8') as f:
|
||||
guan.write_two_dimensional_data_without_opening_file(x_array, y_array, matrix, f)
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 在文件中写入二维数据(第一行和第一列分别为横纵坐标)(需要输入已打开的文件)
|
||||
@guan.function_decorator
|
||||
def write_two_dimensional_data_without_opening_file(x_array, y_array, matrix, f):
|
||||
import numpy as np
|
||||
x_array = np.array(x_array)
|
||||
@@ -608,26 +589,25 @@ def write_two_dimensional_data_without_opening_file(x_array, y_array, matrix, f)
|
||||
j0 += 1
|
||||
f.write('\n')
|
||||
i0 += 1
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 在文件中写入二维数据(不包括x和y)
|
||||
@guan.function_decorator
|
||||
def write_two_dimensional_data_without_xy_array(matrix, filename='a', file_format='.txt'):
|
||||
import guan
|
||||
with open(filename+file_format, 'w', encoding='UTF-8') as f:
|
||||
guan.write_two_dimensional_data_without_xy_array_and_without_opening_file(matrix, f)
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 在文件中写入二维数据(不包括x和y)(需要输入已打开的文件)
|
||||
@guan.function_decorator
|
||||
def write_two_dimensional_data_without_xy_array_and_without_opening_file(matrix, f):
|
||||
for row in matrix:
|
||||
for element in row:
|
||||
f.write(str(element)+' ')
|
||||
f.write('\n')
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 以显示编号的样式,打印数组
|
||||
@guan.function_decorator
|
||||
def print_array_with_index(array, show_index=1, index_type=0):
|
||||
if show_index==0:
|
||||
for i0 in array:
|
||||
@@ -643,10 +623,9 @@ def print_array_with_index(array, show_index=1, index_type=0):
|
||||
for i0 in array:
|
||||
index += 1
|
||||
print(index, i0)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 获取目录中的所有文件名
|
||||
@guan.function_decorator
|
||||
def get_all_filenames_in_directory(directory='./', file_format=None):
|
||||
import os
|
||||
file_list = []
|
||||
@@ -660,6 +639,7 @@ def get_all_filenames_in_directory(directory='./', file_format=None):
|
||||
return file_list
|
||||
|
||||
# 读取文件夹中某种文本文件类型的文件路径和内容
|
||||
@guan.function_decorator
|
||||
def read_text_files_in_directory(directory='./', file_format='.md'):
|
||||
import os
|
||||
file_list = []
|
||||
@@ -671,11 +651,10 @@ def read_text_files_in_directory(directory='./', file_format='.md'):
|
||||
for file in file_list:
|
||||
with open(file, 'r') as f:
|
||||
content_array.append(f.read())
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return file_list, content_array
|
||||
|
||||
# 在多个文本文件中查找关键词
|
||||
@guan.function_decorator
|
||||
def find_words_in_multiple_files(words, directory='./', file_format='.md'):
|
||||
import guan
|
||||
file_list, content_array = guan.read_text_files_in_directory(directory=directory, file_format=file_format)
|
||||
@@ -684,10 +663,10 @@ def find_words_in_multiple_files(words, directory='./', file_format='.md'):
|
||||
for i0 in range(num_files):
|
||||
if words in content_array[i0]:
|
||||
file_list_with_words.append(file_list[i0])
|
||||
guan.statistics_of_guan_package()
|
||||
return file_list_with_words
|
||||
|
||||
# 并行计算前的预处理,把参数分成多份
|
||||
@guan.function_decorator
|
||||
def preprocess_for_parallel_calculations(parameter_array_all, cpus=1, task_index=0):
|
||||
import numpy as np
|
||||
num_all = np.array(parameter_array_all).shape[0]
|
||||
@@ -700,36 +679,32 @@ def preprocess_for_parallel_calculations(parameter_array_all, cpus=1, task_index
|
||||
parameter_array = parameter_array_all[task_index*num_parameter:(task_index+1)*num_parameter]
|
||||
else:
|
||||
parameter_array = parameter_array_all[task_index*num_parameter:num_all]
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return parameter_array
|
||||
|
||||
# 随机获得一个整数,左闭右闭
|
||||
@guan.function_decorator
|
||||
def get_random_number(start=0, end=1):
|
||||
import random
|
||||
rand_number = random.randint(start, end) # 左闭右闭 [start, end]
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return rand_number
|
||||
|
||||
# 选取一个种子生成固定的随机整数
|
||||
@guan.function_decorator
|
||||
def generate_random_int_number_for_a_specific_seed(seed=0, x_min=0, x_max=10):
|
||||
import numpy as np
|
||||
np.random.seed(seed)
|
||||
rand_num = np.random.randint(x_min, x_max) # 左闭右开[x_min, x_max)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return rand_num
|
||||
|
||||
# 使用jieba软件包进行分词
|
||||
@guan.function_decorator
|
||||
def divide_text_into_words(text):
|
||||
import jieba
|
||||
words = jieba.lcut(text)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return words
|
||||
|
||||
# 判断某个字符是中文还是英文或其他
|
||||
@guan.function_decorator
|
||||
def check_Chinese_or_English(a):
|
||||
if '\u4e00' <= a <= '\u9fff' :
|
||||
word_type = 'Chinese'
|
||||
@@ -740,6 +715,7 @@ def check_Chinese_or_English(a):
|
||||
return word_type
|
||||
|
||||
# 统计中英文文本的字数,默认不包括空格
|
||||
@guan.function_decorator
|
||||
def count_words(text, include_space=0, show_words=0):
|
||||
import jieba
|
||||
import guan
|
||||
@@ -764,46 +740,41 @@ def count_words(text, include_space=0, show_words=0):
|
||||
new_words_2.append(word)
|
||||
if show_words == 1:
|
||||
print(new_words_2)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return num_words
|
||||
|
||||
# 将RGB转成HEX
|
||||
@guan.function_decorator
|
||||
def rgb_to_hex(rgb, pound=1):
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
if pound==0:
|
||||
return '%02x%02x%02x' % rgb
|
||||
else:
|
||||
return '#%02x%02x%02x' % rgb
|
||||
|
||||
# 将HEX转成RGB
|
||||
@guan.function_decorator
|
||||
def hex_to_rgb(hex):
|
||||
hex = hex.lstrip('#')
|
||||
length = len(hex)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return tuple(int(hex[i:i+length//3], 16) for i in range(0, length, length//3))
|
||||
|
||||
# 使用MD5进行散列加密
|
||||
@guan.function_decorator
|
||||
def encryption_MD5(password, salt=''):
|
||||
import hashlib
|
||||
password = salt+password
|
||||
hashed_password = hashlib.md5(password.encode()).hexdigest()
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return hashed_password
|
||||
|
||||
# 使用SHA-256进行散列加密
|
||||
@guan.function_decorator
|
||||
def encryption_SHA_256(password, salt=''):
|
||||
import hashlib
|
||||
password = salt+password
|
||||
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
return hashed_password
|
||||
|
||||
# 自动先后运行程序
|
||||
@guan.function_decorator
|
||||
def run_programs_sequentially(program_files=['./a.py', './b.py'], execute='python ', show_time=0):
|
||||
import os
|
||||
import time
|
||||
@@ -821,25 +792,22 @@ def run_programs_sequentially(program_files=['./a.py', './b.py'], execute='pytho
|
||||
if show_time == 1:
|
||||
end = time.time()
|
||||
print('Total running time = '+str((end-start)/60)+' min')
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 如果不存在文件夹,则新建文件夹
|
||||
@guan.function_decorator
|
||||
def make_directory(directory='./test'):
|
||||
import os
|
||||
if not os.path.exists(directory):
|
||||
os.makedirs(directory)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 复制一份文件
|
||||
@guan.function_decorator
|
||||
def copy_file(file1='./a.txt', file2='./b.txt'):
|
||||
import shutil
|
||||
shutil.copy(file1, file2)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
|
||||
# 拼接两个PDF文件
|
||||
@guan.function_decorator
|
||||
def combine_two_pdf_files(input_file_1='a.pdf', input_file_2='b.pdf', output_file='combined_file.pdf'):
|
||||
import PyPDF2
|
||||
output_pdf = PyPDF2.PdfWriter()
|
||||
@@ -852,6 +820,4 @@ def combine_two_pdf_files(input_file_1='a.pdf', input_file_2='b.pdf', output_fil
|
||||
for page in range(len(pdf2.pages)):
|
||||
output_pdf.add_page(pdf2.pages[page])
|
||||
with open(output_file, 'wb') as combined_file:
|
||||
output_pdf.write(combined_file)
|
||||
import guan
|
||||
guan.statistics_of_guan_package()
|
||||
output_pdf.write(combined_file)
|
||||
Reference in New Issue
Block a user