This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具v2/获取小闲平台使用数据new.py

176 lines
7.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PySide6.QtWidgets import QWidget, QApplication, QFileDialog, QTableWidgetItem
from PySide6.QtGui import QColor
from Ui_获取小闲平台使用数据new import Ui_Form
from database_tools import *
def getFilename(string):
filename = re.findall(r"\d{21}_([\s\S]*?)_高[一二三]_数学",string)[0]
return filename
def getDate(string):
date = re.findall(r"\((\d{8})\).zip",string)
if len(date) > 0:
return date[0]
else:
return ""
class MyWindow(QWidget,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.bind()
def bind(self):
self.lineEdit_threshold.setText("0.75")
self.tableWidget.setColumnWidth(0,200)
self.tableWidget.setColumnWidth(1,280)
self.tableWidget.setColumnWidth(2,80)
self.pushButton_file.clicked.connect(self.getFilePath)
self.pushButton_folder.clicked.connect(self.getDirPath)
self.pushButton_exec.clicked.connect(self.exec)
self.singlefile = True
def getFilePath(self): #选取文件并放置在tableWidget的第一行
self.tableWidget.setRowCount(1)
pathlist = QFileDialog.getOpenFileName(self,"选择文件",".","zip文件(*.zip);;所有文件(*)")
self.zipfilepath = pathlist[0]
self.tableWidget.setItem(0,0,QTableWidgetItem(pathlist[0]))
self.singlefile = True
filename = getFilename(pathlist[0])
self.tableWidget.setItem(0,1,QTableWidgetItem(filename))
self.tableWidget.setItem(0,2,QTableWidgetItem(getDate(pathlist[0])))
def getDirPath(self): #选取文件夹中的所有符合答题纸特征的.zip文件并放置在tableWidget中
dirpath = QFileDialog.getExistingDirectory(self,"选择文件夹")
self.filelist = [os.path.join(dirpath,f) for f in os.listdir(dirpath) if ".zip" in f and re.findall(r"\d{21}",f)!=[]]
self.singlefile = False
count = 0
for f in self.filelist:
if count + 1 > self.tableWidget.rowCount():
self.tableWidget.insertRow(self.tableWidget.rowCount())
self.tableWidget.setItem(count,0,QTableWidgetItem(f))
self.tableWidget.setItem(count,1,QTableWidgetItem(getFilename(f)))
self.tableWidget.setItem(count,2,QTableWidgetItem(getDate(f)))
count += 1
def exec(self):
if self.singlefile:
self.execsinglefile()
else:
self.execmultifile()
def execmultifile(self): #待修改
files = self.filelist
datedfiles = []
tempdir = "临时文件/zips"
statsfilename = "小题分_按学号数学.xlsx"
answersheetseekingpath = "../备课组"
glossoutput = ""
threshold = float(self.lineEdit_threshold.text())
for f in files:
fpath = os.path.join(directory,f)
if len(re.findall(r"\(\d{8}\)",f)) == 0:
print(f"文件 {f} 未标注日期.")
else:
datedfiles.append(fpath)
for zipfpath in datedfiles:
try:
try:
shutil.rmtree(tempdir)
except:
pass
makedir(tempdir)
date = re.findall(r"\((\d{8})\)",zipfpath)[0]
xiaoxianpid = ParseZipname(zipfpath)
paperinfo = FindPaper(xiaoxianpid, answersheetseekingpath)
gradename = paperinfo[1]
idlist = paperinfo[2]
zf = zipfile.ZipFile(zipfpath)
zf.extractall(tempdir) #解压zip文件中的所有内容到tempdir
statsfilepathlist = FindFile(tempdir,statsfilename)
validcols,marks = generateColIndexandMarks(statsfilepathlist,statsfilename,paperinfo)
dfcurrent = pd.read_excel(os.path.join(statsfilepathlist[0],statsfilename))
correspondence_dict = generateIDtoUsageCorrespondence(idlist,validcols,dfcurrent.iloc[1,validcols])
output = CalculateUsages(statsfilepathlist,statsfilename,gradename,threshold,marks,correspondence_dict,validcols,date)
if CheckUsagesValidity(output) == 0:
glossoutput += output
else:
print(f"{zipfpath} 数据有误, 可能需要检查每一题的满分数据")
self.label_filepath.setText(f"{zipfpath} 数据有误, 可能需要检查每一题的满分数据")
print(f"文件 {zipfpath} 处理完成")
except:
print(f"{zipfpath} 操作中有错误, 需要检查源数据")
self.label_filepath.setText(f"{zipfpath} 操作中有错误, 需要检查源数据")
SaveTextFile(glossoutput,"文本文件/metadata.txt")
os.system("code 文本文件/metadata.txt")
# print(datedfiles)
def execsinglefile(self): #待修改
self.tableWidget.clearSelection()
self.tableWidget.item(0,2).setBackground(QColor("transparent"))
date = self.tableWidget.item(0,2).text()
threshold = float(self.lineEdit_threshold.text())
if not len(date.strip()) == 8:
self.tableWidget.item(0,2).setBackground(QColor("red"))
elif not threshold >= 0 or not threshold <= 1:
self.lineEdit_threshold.setText("阈值有误")
# else:
# tempdir = "临时文件/zips"
# statsfilename = "小题分_按学号数学.xlsx"
# answersheetseekingpath = "../备课组"
# try:
# shutil.rmtree(tempdir)
# except:
# pass
# makedir(tempdir)
# xiaoxianpid = ParseZipname(self.zipfilepath)
# paperinfo = FindPaper(xiaoxianpid, answersheetseekingpath)
# gradename = paperinfo[1]
# idlist = paperinfo[2]
# zf = zipfile.ZipFile(self.zipfilepath)
# zf.extractall(tempdir) #解压zip文件中的所有内容到tempdir
# # papertype = CheckPaperType(tempdir,statsfilename)
# statsfilepathlist = FindFile(tempdir,statsfilename)
# validcols,marks = generateColIndexandMarks(statsfilepathlist,statsfilename,paperinfo)
# dfcurrent = pd.read_excel(os.path.join(statsfilepathlist[0],statsfilename))
# correspondence_dict = generateIDtoUsageCorrespondence(idlist,validcols,dfcurrent.iloc[1,validcols])
# output = CalculateUsages(statsfilepathlist,statsfilename,gradename,threshold,marks,correspondence_dict,validcols,date)
# if CheckUsagesValidity(output) == 0:
# if not self.checkBox_appendflag.isChecked():
# SaveTextFile(output,"文本文件/metadata.txt")
# print("数据文件已输出至metadata.txt")
# self.label_filepath.setText("数据文件已输出至metadata.txt")
# os.system("code 文本文件/metadata.txt")
# else:
# AppendTextFile("\n\n"+output,"文本文件/metadata.txt")
# print("数据文件已添加至metadata.txt")
# self.label_filepath.setText("数据文件已添加至metadata.txt")
# os.system("code 文本文件/metadata.txt")
# else:
# print("数据有误, 可能需要检查每一题的满分数据")
# self.label_filepath.setText("数据有误, 可能需要检查每一题的满分数据")
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow()
windows.show()
app.exec()