[modify] v0.0.2(2024/05/20)
1. 功能模块化,为后面其他功能奠定一个基本的框架 2. 使用了多线程提高效率 3. 优化了准备工作中的细节 4. 运行初始化时自动删除 raw_data_dir 中的 .xlsx 文件 5. 优化了输出格式 6. 使用 pyinstaller 库进行代码冻结并调试成功
This commit is contained in:
parent
9a001021b1
commit
c61686065e
@ -1,24 +1,39 @@
|
||||
# coding: utf-8
|
||||
import os
|
||||
import pandas as pd
|
||||
import openpyxl
|
||||
from os import scandir, remove
|
||||
from sys import exit
|
||||
from openpyxl import load_workbook
|
||||
from win32com.client import DispatchEx
|
||||
import time
|
||||
from time import time, strftime, localtime, sleep
|
||||
from threading import Thread
|
||||
import pythoncom
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def just_open(filename):
|
||||
xlApp = DispatchEx("Excel.Application")
|
||||
xlApp.Visible = False
|
||||
xlBook = xlApp.Workbooks.Open(filename)
|
||||
xlApp.DisplayAlerts = 0
|
||||
xlBook.SaveAs(filename)
|
||||
xlBook.Close()
|
||||
for i in range(3):
|
||||
try:
|
||||
pythoncom.CoInitialize()
|
||||
xlapp = DispatchEx("Excel.Application")
|
||||
xlapp.Visible = False
|
||||
xlbook = xlapp.Workbooks.Open(filename)
|
||||
xlapp.DisplayAlerts = False
|
||||
xlbook.SaveAs(filename)
|
||||
xlbook.Close()
|
||||
xlapp.Quit()
|
||||
except Exception as Err:
|
||||
if xlbook is None:
|
||||
xlbook.SaveAs(filename)
|
||||
xlbook.close()
|
||||
if xlapp is not None:
|
||||
xlapp.Quit()
|
||||
print(f"just open 第 {i} 次操作失败,静默三秒钟,重新执行......")
|
||||
sleep(3)
|
||||
|
||||
|
||||
def traversal_files(path):
|
||||
dirs = []
|
||||
files = []
|
||||
for item in os.scandir(path):
|
||||
for item in scandir(path):
|
||||
if item.is_dir():
|
||||
dirs.append(item.path)
|
||||
elif item.is_file():
|
||||
@ -27,9 +42,7 @@ def traversal_files(path):
|
||||
return dirs, files
|
||||
|
||||
|
||||
def find_row_start(excel_file, ws_data, conditions):
|
||||
global AV
|
||||
global RR
|
||||
def find_row_start(excel_file, ws_data, conditions, AV, RR):
|
||||
ratio = float(conditions[1].removeprefix('speed'))/100
|
||||
speed_max = AV * ratio * RR / 6
|
||||
|
||||
@ -45,9 +58,9 @@ def find_row_start(excel_file, ws_data, conditions):
|
||||
else:
|
||||
row_start -= 200
|
||||
else:
|
||||
print(f"{excel_file.replace('xlsx', 'data')}, 这个文件数据有问题,请检查......")
|
||||
os.remove(excel_file)
|
||||
exit(9)
|
||||
remove(excel_file)
|
||||
msg = f"{excel_file.replace('xlsx', 'data')}, 这个文件数据有问题,请检查......"
|
||||
warn_pause_exit(msg, 1, 9)
|
||||
|
||||
return row_max, row_start
|
||||
|
||||
@ -80,12 +93,12 @@ def copy_data_to_result(ws_data, ws_result, row_max, row_start):
|
||||
i = i + 1
|
||||
|
||||
|
||||
def copy_data_to_excel_file(wb_data, ws_result, row_max, row_start, excel_file):
|
||||
def copy_data_to_excel_file(wb_data, ws_result, row_max, row_start, excel_file, RC, RR):
|
||||
try:
|
||||
del wb_data['dp']
|
||||
wb_data.create_sheet('dp')
|
||||
ws_dp = wb_data['dp']
|
||||
except:
|
||||
except Exception as Err:
|
||||
wb_data.create_sheet('dp')
|
||||
ws_dp = wb_data['dp']
|
||||
|
||||
@ -99,26 +112,22 @@ def copy_data_to_excel_file(wb_data, ws_result, row_max, row_start, excel_file):
|
||||
cell.value = data[i]
|
||||
i = i + 1
|
||||
|
||||
global RC
|
||||
global RR
|
||||
ws_dp.cell(row=5, column=7).value = RC
|
||||
ws_dp.cell(row=6, column=7).value = RR
|
||||
|
||||
wb_data.save(excel_file)
|
||||
just_open(excel_file) # 为了能读取到公式计算的数值,必须要用 win32com 打开关闭一次
|
||||
wb_data = openpyxl.load_workbook(excel_file, data_only=True)
|
||||
wb_data = load_workbook(excel_file, data_only=True)
|
||||
ws_dp = wb_data['dp']
|
||||
|
||||
return wb_data, ws_dp
|
||||
|
||||
|
||||
def find_row_start_dp(ws_dp, row_max, row_start, conditions):
|
||||
global AV
|
||||
def find_row_start_dp(ws_dp, row_max, row_start, conditions, AV):
|
||||
ratio = float(conditions[1].removeprefix('speed'))/100
|
||||
av_max = AV * ratio
|
||||
row_max_dp = row_max - row_start + 1 + 1 # title row
|
||||
row_start_dp = row_max_dp - 5
|
||||
print(f"row_start_dp = {row_start_dp}")
|
||||
while row_start_dp > 1:
|
||||
# 处理异常数据:当从数据文件中拷贝的有效数据超过5000时,会触发该代码块
|
||||
if ws_dp.cell(row=row_start_dp, column=4).value is None:
|
||||
@ -137,31 +146,30 @@ def find_row_start_dp(ws_dp, row_max, row_start, conditions):
|
||||
else:
|
||||
row_start_dp -= 5 # 保守一点,每次移动 5 个点位,如果想要加快程序运行,可适当调整更大一些,建议不超过 15
|
||||
else:
|
||||
print("数据有误,请确认!")
|
||||
print("未找到平衡的点")
|
||||
exit(1)
|
||||
msg = "数据有误,未找到平衡的点,请确认!"
|
||||
warn_pause_exit(msg, 1, 1)
|
||||
|
||||
return row_start_dp
|
||||
|
||||
|
||||
def single_file_process(data_file, wb_result, count):
|
||||
def single_file_process(data_file, wb_result, count, AV, RR, RC):
|
||||
excel_file = data_file.replace('data', 'xlsx')
|
||||
sheet_name = data_file.split('\\')[-1].removesuffix('.data')
|
||||
df = pd.read_csv(data_file, sep='\t')
|
||||
df.to_excel(excel_file, sheet_name=sheet_name, index=False)
|
||||
|
||||
conditions = sorted(data_file.split('\\')[-2].split('_')[1:])
|
||||
print(f"conditions = {conditions}")
|
||||
# print(f"conditions = {conditions}")
|
||||
result_sheet_name = find_result_sheet_name(conditions, count)
|
||||
ws_result = wb_result[result_sheet_name]
|
||||
|
||||
wb_data = openpyxl.load_workbook(excel_file)
|
||||
wb_data = load_workbook(excel_file)
|
||||
ws_data = wb_data[sheet_name]
|
||||
row_max, row_start = find_row_start(excel_file, ws_data, conditions)
|
||||
row_max, row_start = find_row_start(excel_file, ws_data, conditions, AV, RR)
|
||||
|
||||
copy_data_to_result(ws_data, ws_result, row_max, row_start)
|
||||
wb_data, ws_dp = copy_data_to_excel_file(wb_data, ws_result, row_max, row_start, excel_file)
|
||||
row_start_dp = find_row_start_dp(ws_dp, row_max, row_start, conditions)
|
||||
wb_data, ws_dp = copy_data_to_excel_file(wb_data, ws_result, row_max, row_start, excel_file, RC, RR)
|
||||
row_start_dp = find_row_start_dp(ws_dp, row_max, row_start, conditions, AV)
|
||||
|
||||
ws_result["G2"] = int(row_start_dp)
|
||||
|
||||
@ -169,59 +177,84 @@ def single_file_process(data_file, wb_result, count):
|
||||
wb_data.close()
|
||||
|
||||
|
||||
def data_process(result_file, raw_data_dirs):
|
||||
def data_process(result_file, raw_data_dirs, AV, RR, RC):
|
||||
prefix = result_file.split('\\')[-1].split('_')[0]
|
||||
print(f"prefix = {prefix}")
|
||||
print(f"raw_data_dirs = {raw_data_dirs}")
|
||||
wb_result = openpyxl.load_workbook(result_file) # 打开和关闭结果文件夹十分耗时间
|
||||
wb_result = load_workbook(result_file) # 打开和关闭结果文件夹十分耗时间
|
||||
for raw_data_dir in raw_data_dirs:
|
||||
if raw_data_dir.split('\\')[-1].split('_')[0] == prefix:
|
||||
print(f"正在处理【{raw_data_dir}】中的数据......")
|
||||
now = strftime('%Y-%m-%d %H:%M:%S', localtime(time()))
|
||||
print(f"[{now}] 正在处理目录【{raw_data_dir}】中的数据......")
|
||||
_, data_files = traversal_files(raw_data_dir)
|
||||
count = 1 # 计数器,对应三次急停数据
|
||||
for data_file in sorted(data_files):
|
||||
print(f"正在处理【{data_file}】....")
|
||||
print(f"count = {count}")
|
||||
single_file_process(data_file, wb_result, count)
|
||||
count += 1
|
||||
|
||||
threads = [Thread(target=single_file_process, args=(data_files[0], wb_result, 1, AV, RR, RC)),
|
||||
Thread(target=single_file_process, args=(data_files[1], wb_result, 2, AV, RR, RC)),
|
||||
Thread(target=single_file_process, args=(data_files[2], wb_result, 3, AV, RR, RC))]
|
||||
[t.start() for t in threads]
|
||||
[t.join() for t in threads]
|
||||
now = strftime('%Y-%m-%d %H:%M:%S', localtime(time()))
|
||||
print(f"[{now}] 目录【{raw_data_dir}】中的数据已处理完成......")
|
||||
|
||||
now = strftime('%Y-%m-%d %H:%M:%S', localtime(time()))
|
||||
print(f"[{now}] 结果文件【{result_file}】的数据已整理完成,保存文件需要1-2min,请耐心等待......")
|
||||
wb_result.save(result_file)
|
||||
wb_result.close()
|
||||
|
||||
|
||||
def warn_pause_exit(msg, pause_num, exit_num):
|
||||
print(msg + '\n')
|
||||
for i in range(pause_num):
|
||||
_ = input("Press ENTER to continue......")
|
||||
exit(exit_num)
|
||||
|
||||
|
||||
def check_files(raw_data_dirs, result_files):
|
||||
if len(result_files) != 3:
|
||||
print("结果文件数目错误,请参考 readme.txt 中的规则。")
|
||||
exit(3)
|
||||
msg = "结果文件数目错误,结果文件有且只有三个,请确认!"
|
||||
for result_file in result_files:
|
||||
print(result_file)
|
||||
warn_pause_exit(msg, 1, 3)
|
||||
|
||||
prefix = []
|
||||
for result_file in result_files:
|
||||
prefix.append(result_file.split('\\')[-1].split('_')[0])
|
||||
if not sorted(prefix) == sorted(['load33', 'load66', 'load100']):
|
||||
wd = result_file.split('\\')
|
||||
wd = result_files[0].split('\\')
|
||||
del wd[-1]
|
||||
wd = '\\'.join(wd)
|
||||
print(f"请关闭所有相关数据文件,并检查工作目录【{wd}】下,有且只允许有类似如下三个文件:")
|
||||
print("1. load33_自研_制动性能测试.xlsx")
|
||||
print("2. load66_自研_制动性能测试.xlsx")
|
||||
print("3. load100_自研_制动性能测试.xlsx")
|
||||
exit(8)
|
||||
msg = f"请关闭所有相关数据文件,并检查工作目录【{wd}】下,有且只允许有类似如下三个文件:\n" \
|
||||
f"1. load33_自研_制动性能测试.xlsx\n" \
|
||||
f"2. load66_自研_制动性能测试.xlsx\n" \
|
||||
f"3. load100_自研_制动性能测试.xlsx"
|
||||
warn_pause_exit(msg, 1, 8)
|
||||
|
||||
for raw_data_dir in raw_data_dirs:
|
||||
prefix = raw_data_dir.split('\\')[-1].split('_')[0]
|
||||
if prefix not in ['load33', 'load66', 'load100']:
|
||||
print(f"报错信息:数据目录【{raw_data_dir}】不合规,请参考如下形式。")
|
||||
print("命名规则:\n\t1. loadAA_speedBB_reachCC\n\t2. loadAA_reachBB_speedCC")
|
||||
print("规则解释:AA/BB/CC 指的是负载/速度/臂展的比例,比如 load66_speed100_reach33 意思是 66% 负载,100% 速度以及 33% 臂展情况下的测试结果文件夹。")
|
||||
exit(7)
|
||||
components = raw_data_dir.split('\\')[-1].split('_')
|
||||
sorted(components)
|
||||
if components[0] not in ['load33', 'load66', 'load100'] or \
|
||||
components[1] not in ['speed33', 'speed66', 'speed100'] or \
|
||||
components[2] not in ['reach33', 'reach66', 'reach100']:
|
||||
msg = f"报错信息:数据目录【{raw_data_dir}】命名不合规,请参考如下形式\n" \
|
||||
f"命名规则:\n 1. loadAA_speedBB_reachCC\n 2. loadAA_reachBB_speedCC\n" \
|
||||
f"规则解释:AA/BB/CC 指的是负载/速度/臂展的比例\n" \
|
||||
f"load66_speed100_reach33:66% 负载,100% 速度以及 33% 臂展情况下的测试结果文件夹"
|
||||
warn_pause_exit(msg, 1, 7)
|
||||
|
||||
# 直接删掉 excel 文件
|
||||
_, raw_data_files = traversal_files(raw_data_dir)
|
||||
for raw_data_file in raw_data_files:
|
||||
if raw_data_file.endswith(".xlsx"):
|
||||
remove(raw_data_file)
|
||||
|
||||
_, raw_data_files = traversal_files(raw_data_dir)
|
||||
if len(raw_data_files) != 3:
|
||||
print(f"数据目录【{raw_data_dir}】下数据文件个数错误,每个数据目录下有且只能有三个以 .data 为后缀的数据文件。")
|
||||
exit(6)
|
||||
msg = f"数据目录【{raw_data_dir}】下数据文件个数错误,每个数据目录下有且只能有三个以 .data 为后缀的数据文件"
|
||||
warn_pause_exit(msg, 1, 6)
|
||||
for raw_data_file in raw_data_files:
|
||||
if not raw_data_file.split('\\')[-1].endswith('.data'):
|
||||
print(f"数据文件【{raw_data_file}】后缀错误,每个数据目录下有且只能有三个以 .data 为后缀的数据文件。")
|
||||
exit(5)
|
||||
print("数据目录合规性检查结束......")
|
||||
msg = f"数据文件【{raw_data_file}】后缀错误,每个数据目录下有且只能有三个以 .data 为后缀的数据文件"
|
||||
warn_pause_exit(msg, 1, 5)
|
||||
|
||||
print("数据目录合规性检查结束,未发现问题......")
|
||||
|
||||
|
||||
def delete_excel_files():
|
||||
@ -231,39 +264,81 @@ def delete_excel_files():
|
||||
_, raw_data_files = traversal_files(raw_data_dir)
|
||||
for raw_data_file in raw_data_files:
|
||||
if raw_data_file.endswith('.xlsx'):
|
||||
os.remove(raw_data_file)
|
||||
remove(raw_data_file)
|
||||
|
||||
|
||||
def initialization():
|
||||
time_start = time() # 记录开始时间
|
||||
try:
|
||||
# read init configurations from config file
|
||||
wb_conf = load_workbook('./configuration.xlsx')
|
||||
ws_conf = wb_conf['conf']
|
||||
|
||||
global data_dir
|
||||
global AV
|
||||
global RR
|
||||
global RC
|
||||
|
||||
data_dir = ws_conf.cell(row=2, column=2).value
|
||||
AV = int(ws_conf.cell(row=3, column=2).value)
|
||||
RR = int(ws_conf.cell(row=4, column=2).value)
|
||||
RC = float(ws_conf.cell(row=5, column=2).value)
|
||||
wb_conf.close()
|
||||
|
||||
raw_data_dirs, result_files = traversal_files(data_dir)
|
||||
# print("#调试信息======================================")
|
||||
# print(f"结果文件:{result_files}")
|
||||
# print(f'数据目录:{raw_data_dirs}')
|
||||
check_files(raw_data_dirs, result_files)
|
||||
|
||||
return raw_data_dirs, result_files, time_start, AV, RR, RC
|
||||
|
||||
except Exception as Err:
|
||||
msg = "无法在当前路径下找到【configuration.xlsx】文件,请确认!"
|
||||
warn_pause_exit(msg, 1, 2)
|
||||
|
||||
|
||||
def execution(args):
|
||||
raw_data_dirs, result_files, time_start, AV, RR, RC = args
|
||||
prefix = []
|
||||
for raw_data_dir in raw_data_dirs:
|
||||
prefix.append(raw_data_dir.split('\\')[-1].split("_")[0])
|
||||
|
||||
try:
|
||||
threads = []
|
||||
for result_file in result_files:
|
||||
if result_file.split('\\')[-1].split('_')[0] not in set(prefix):
|
||||
continue
|
||||
else:
|
||||
now = strftime('%Y-%m-%d %H:%M:%S', localtime(time()))
|
||||
print(f"[{now}] 正在整理结果文件【{result_file}】的数据......")
|
||||
# data_process(result_file, raw_data_dirs, AV, RR, RC)
|
||||
threads.append(Thread(target=data_process, args=(result_file, raw_data_dirs, AV, RR, RC)))
|
||||
[t.start() for t in threads]
|
||||
[t.join() for t in threads]
|
||||
print("#---------------------------------------------------------")
|
||||
print("全部处理完毕")
|
||||
delete_excel_files()
|
||||
except Exception as Err:
|
||||
print("程序运行错误,请检查配置文件是否准确设定,以及数据文件组织是否正确!")
|
||||
delete_excel_files() # 运行结束之后,删除中间临时文件
|
||||
|
||||
time_end = time() # 记录结束时间
|
||||
time_total = time_end - time_start # 计算的时间差为程序的执行时间,单位为秒/s
|
||||
msg = f"数据处理时间:{time_total//3600:02} h {time_total % 3600/60:05.2f} min"
|
||||
warn_pause_exit(msg, 1, 0)
|
||||
|
||||
|
||||
def main():
|
||||
time_start = time.time() # 记录开始时间
|
||||
|
||||
global data_dir
|
||||
raw_data_dirs, result_files = traversal_files(data_dir)
|
||||
print("#调试信息======================================")
|
||||
print(f"结果文件:{result_files}")
|
||||
print(f'数据目录:{raw_data_dirs}')
|
||||
check_files(raw_data_dirs, result_files)
|
||||
|
||||
for result_file in result_files:
|
||||
print(f"正在整理【{result_file}】文件的数据......")
|
||||
data_process(result_file, raw_data_dirs)
|
||||
|
||||
delete_excel_files() # 运行结束之后,删除中间临时文件
|
||||
|
||||
time_end = time.time() # 记录结束时间
|
||||
time_total = time_end - time_start # 计算的时间差为程序的执行时间,单位为秒/s
|
||||
print(f"数据处理时间:{time_total//3600:02} h {time_total % 3600/60:05.2f} min")
|
||||
execution(initialization())
|
||||
|
||||
|
||||
# 定义初始参数,数据文件夹路径/最大角速度/减速比/额定电流
|
||||
global data_dir
|
||||
global AV
|
||||
global RR
|
||||
global RC
|
||||
data_dir = r'D:\Syncthing\company\D-测试工作\X-自动化测试\99-Data\j1'
|
||||
AV = 180 # AV for Angular velocity
|
||||
RR = 120 # RR for Angular velocity
|
||||
RC = 5.6 # RC for Rated Current
|
||||
global data_dir # path for data
|
||||
global AV # AV for Angular velocity
|
||||
global RR # RR for Angular velocity
|
||||
global RC # RC for Rated Current
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
BIN
rokae/brake/configuration.xlsx
Normal file
BIN
rokae/brake/configuration.xlsx
Normal file
Binary file not shown.
43
rokae/brake/file_version_info.txt
Normal file
43
rokae/brake/file_version_info.txt
Normal file
@ -0,0 +1,43 @@
|
||||
# UTF-8
|
||||
#
|
||||
# For more details about fixed file info 'ffi' see:
|
||||
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
|
||||
VSVersionInfo(
|
||||
ffi=FixedFileInfo(
|
||||
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
|
||||
# Set not needed items to zero 0.
|
||||
filevers=(0, 0, 1, 5),
|
||||
prodvers=(0, 0, 1, 5),
|
||||
# Contains a bitmask that specifies the valid bits 'flags'r
|
||||
mask=0x3f,
|
||||
# Contains a bitmask that specifies the Boolean attributes of the file.
|
||||
flags=0x0,
|
||||
# The operating system for which this file was designed.
|
||||
# 0x4 - NT and there is no need to change it.
|
||||
OS=0x4,
|
||||
# The general type of file.
|
||||
# 0x1 - the file is an application.
|
||||
fileType=0x1,
|
||||
# The function of the file.
|
||||
# 0x0 - the function is not defined for this fileType
|
||||
subtype=0x0,
|
||||
# Creation date and time stamp.
|
||||
date=(0, 0)
|
||||
),
|
||||
kids=[
|
||||
StringFileInfo(
|
||||
[
|
||||
StringTable(
|
||||
'040904b0',
|
||||
[StringStruct('CompanyName', 'Rokae - https://www.rokae.com/'),
|
||||
StringStruct('FileDescription', 'All in one automatic operating tool'),
|
||||
StringStruct('FileVersion', '0.0.2 (2024-05-20)'),
|
||||
StringStruct('InternalName', 'AIO.exe'),
|
||||
StringStruct('LegalCopyright', '© 2024-2024 Manford Fan'),
|
||||
StringStruct('OriginalFilename', 'AIO.exe'),
|
||||
StringStruct('ProductName', 'AIO'),
|
||||
StringStruct('ProductVersion', '0.0.2 (2024-05-20)')])
|
||||
]),
|
||||
VarFileInfo([VarStruct('Translation', [1033, 1200])])
|
||||
]
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user