[modify] minor modification, change write2textbox and using shorter func name

This commit is contained in:
2024-05-30 08:28:59 +08:00
parent 05f461f8c1
commit 9b849897e2
3 changed files with 56 additions and 65 deletions

View File

@@ -1,24 +1,25 @@
# coding: utf-8
import os
import sys
import openpyxl
import time
from os import scandir
from os.path import isfile, exists
from sys import argv
from openpyxl import load_workbook
from time import time, sleep, strftime, localtime
from threading import Thread
import pandas
from pandas import read_csv
def traversal_files(path, w2t):
# 功能:以列表的形式分别返回指定路径下的文件和文件夹,不包含子目录
# 参数:路径
# 返回值:路径下的文件夹列表 路径下的文件列表
if not os.path.exists(path):
if not exists(path):
msg = f'数据文件夹{path}不存在,请确认后重试......'
w2t(msg, 1)
w2t(msg)
else:
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():
@@ -71,7 +72,7 @@ def find_row_start(data_file, df, conditions, av, rr, axis, vel, trq, w2t, rpm):
row_start -= step
else:
msg = f"可能是{data_file}这个文件数据采集有问题,比如未采集理论速度值,也有可能是程序步长设定问题,请检查......"
w2t(msg, 2)
w2t(msg)
return row_max, row_start
@@ -119,10 +120,10 @@ def single_file_process(data_file, wb_result, count, av, rr, axis, vel, trq, w2t
# 返回值:-
if data_file.endswith('.data'):
sep = '\t'
df = pandas.read_csv(data_file, sep=sep)
df = read_csv(data_file, sep=sep)
elif data_file.endswith('.csv'):
sep = ','
df = pandas.read_csv(data_file, sep=sep, encoding='gbk', header=8)
df = read_csv(data_file, sep=sep, encoding='gbk', header=8)
conditions = sorted(data_file.split('\\')[-2].split('_')[1:])
result_sheet_name = find_result_sheet_name(conditions, count)
@@ -139,8 +140,8 @@ def now_doing_msg(docs, flag, w2t):
# 功能:输出正在处理的文件或目录
# 参数文件或目录start 或 done 标识
# 返回值:-
now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
file_type = 'file' if os.path.isfile(docs) else 'dir'
now = strftime('%Y-%m-%d %H:%M:%S', localtime(time()))
file_type = 'file' if isfile(docs) else 'dir'
if flag == 'start' and file_type == 'dir':
w2t(f"[{now}] 正在处理目录 {docs} 中的数据......")
elif flag == 'start' and file_type == 'file':
@@ -159,7 +160,7 @@ class GetThreadResult(Thread):
self.result = 0
def run(self):
time.sleep(1)
sleep(1)
self.result = self.func(*self.args)
def get_result(self):
@@ -170,12 +171,12 @@ class GetThreadResult(Thread):
return None
def w2t_local(msg, exitcode, wait, w2t):
def w2t_local(msg, wait, w2t):
while True:
global stop
if stop == 0 and wait != 0:
time.sleep(1)
w2t(msg, exitcode, wait)
sleep(1)
w2t(msg, wait)
else:
break
@@ -185,19 +186,19 @@ def data_process(result_file, raw_data_dirs, av, rr, axis, vel, trq, w2t, rpm):
# 参数:结果文件,数据目录,以及预读取的参数
# 返回值:-
file_name = result_file.split('\\')[-1]
w2t(f"打开文件 {file_name} 需要 1min 左右,请耐心等待", -1, 1)
w2t(f"打开文件 {file_name} 需要 1min 左右", 1)
global stop
stop = 0
t_excel = GetThreadResult(openpyxl.load_workbook, args=(result_file, ))
t_wait = Thread(target=w2t_local, args=('.', -1, 1, w2t))
t_excel = GetThreadResult(load_workbook, args=(result_file, ))
t_wait = Thread(target=w2t_local, args=('.', 1, w2t))
t_excel.start()
t_wait.start()
t_excel.join()
wb_result = t_excel.get_result()
stop = 1
time.sleep(1.1)
w2t('', -1, 0)
sleep(1.1)
w2t('')
prefix = result_file.split('\\')[-1].split('_')[0]
for raw_data_dir in raw_data_dirs:
@@ -222,16 +223,16 @@ def data_process(result_file, raw_data_dirs, av, rr, axis, vel, trq, w2t, rpm):
# ---------------------------------------------------
now_doing_msg(result_file, 'done', w2t)
w2t(f"保存文件 {file_name} 需要 1min 左右,请耐心等待", -1, 1)
w2t(f"保存文件 {file_name} 需要 1min 左右", 1)
stop = 0
t_excel = Thread(target=wb_result.save, args=(result_file, ))
t_wait = Thread(target=w2t_local, args=('.', -1, 1, w2t))
t_wait = Thread(target=w2t_local, args=('.', 1, w2t))
t_excel.start()
t_wait.start()
t_excel.join()
stop = 1
time.sleep(1.1)
w2t('\n', -1, 0)
sleep(1.1)
w2t('\n')
def check_files(raw_data_dirs, result_files, w2t):
@@ -242,7 +243,7 @@ def check_files(raw_data_dirs, result_files, w2t):
msg = "结果文件数目错误,结果文件有且只有三个,请确认!"
for result_file in result_files:
w2t(result_file)
w2t(msg, 3)
w2t(msg)
prefix = []
for result_file in result_files:
@@ -255,7 +256,7 @@ def check_files(raw_data_dirs, result_files, w2t):
1. load33_自研_制动性能测试.xlsx
2. load66_自研_制动性能测试.xlsx
3. load100_自研_制动性能测试.xlsx"""
w2t(msg, 4)
w2t(msg)
for raw_data_dir in raw_data_dirs:
components = raw_data_dir.split('\\')[-1].split('_')
@@ -267,16 +268,16 @@ def check_files(raw_data_dirs, result_files, w2t):
f"命名规则:\n 1. loadAA_speedBB_reachCC\n 2. loadAA_reachBB_speedCC\n" \
f"规则解释AA/BB/CC 指的是负载/速度/臂展的比例\n" \
f"load66_speed100_reach3366% 负载100% 速度以及 33% 臂展情况下的测试结果文件夹"
w2t(msg, 5)
w2t(msg)
_, raw_data_files = traversal_files(raw_data_dir, w2t)
if len(raw_data_files) != 3:
msg = f"数据目录 {raw_data_dir} 下数据文件个数错误,每个数据目录下有且只能有三个以 .data 为后缀的数据文件"
w2t(msg, 6)
w2t(msg)
for raw_data_file in raw_data_files:
if not (raw_data_file.split('\\')[-1].endswith('.data') or raw_data_file.split('\\')[-1].endswith('.csv')):
msg = f"数据文件 {raw_data_file} 后缀错误,每个数据目录下有且只能有三个以 .data/csv 为后缀的数据文件"
w2t(msg, 7)
w2t(msg)
w2t("数据目录合规性检查结束,未发现问题......")
@@ -285,7 +286,7 @@ def execution(path, av, rr, rpm, axis,vel, trq, w2t):
# 功能:执行处理所有数据文件
# 参数initialization函数的返回值
# 返回值:-
time_start = time.time()
time_start = time()
raw_data_dirs, result_files = traversal_files(path, w2t)
check_files(raw_data_dirs, result_files, w2t)
@@ -305,13 +306,13 @@ def execution(path, av, rr, rpm, axis,vel, trq, w2t):
# threads.append(Thread(target=data_process, args=(result_file, raw_data_dirs, AV, RR, RC, AXIS)))
# [t.start() for t in threads]
# [t.join() for t in threads]
w2t("----------------------------------------------------------")
w2t("全部处理完毕")
except Exception as Err:
msg = "程序运行错误,请检查配置文件是否准确设定,以及数据文件组织是否正确,也有可能是结果文件损坏,尝试重新复制一份,再运行!"
w2t(msg)
time_end = time.time()
w2t("----------------------------------------------------------")
w2t("全部处理完毕")
time_end = time()
time_total = time_end - time_start
msg = f"数据处理时间:{time_total // 3600:02.0f} h {time_total % 3600 // 60:02.0f} min {time_total % 60:02.0f} s"
w2t(msg)
@@ -323,4 +324,4 @@ def main(path, av, rr, rpm, axis,vel, trq, w2t):
if __name__ == "__main__":
stop = 0
main(path=sys.argv[1], av=sys.argv[2], rr=sys.argv[3], rpm=sys.argv[4], axis=sys.argv[5], vel=sys.argv[6], trq=sys.argv[7], w2t=sys.argv[8])
main(path=argv[1], av=argv[2], rr=argv[3], rpm=argv[4], axis=argv[5], vel=argv[6], trq=argv[7], w2t=argv[8])