allow Exception

This commit is contained in:
CakeCN
2025-09-14 14:12:35 +08:00
parent d6c4f8cfc0
commit d5b16eec52
2 changed files with 40 additions and 39 deletions

View File

@@ -19,44 +19,44 @@ def judge(filepath:str, name: str, course_id: str, lesson_name: str, root_path:s
directory = 'books/code_tests/'+course_id+'/'+name
# 如果目录不存在,报错
if not os.path.exists(directory):
status = FunctionStatus.FAILED
return FunctionResponse("judge", status, f"The name {name} is not found.")
files = os.listdir(directory)
in_files = [file for file in files if file.endswith('.in')]
save_dir = root_path
Report = ""
passed_num=0
failed_num=0
erroroutput = ""
exceptoutput=""
for file in in_files:
file_path = os.path.join(save_dir, f"_{name}.out")
if os.path.exists(file_path):
os.remove(file_path)
with open(file_path, 'w') as ff:
ff.write("")
return FunctionResponse("judge", False, f"The name {name} is not found.")
try:
files = os.listdir(directory)
in_files = [file for file in files if file.endswith('.in')]
save_dir = root_path
Report = ""
passed_num=0
failed_num=0
erroroutput = ""
exceptoutput=""
for file in in_files:
file_path = os.path.join(save_dir, f"_{name}.out")
if os.path.exists(file_path):
os.remove(file_path)
with open(file_path, 'w') as ff:
ff.write("")
cmd = "cat " + os.path.join(directory, file) + " | " + "python " + os.path.join(root_path, filepath) +" > " + os.path.join(save_dir, f"_{name}.out")
os.system(cmd)
# 比较结果
re = ""
if compare_file(os.path.join(save_dir, f"_{name}.out"), os.path.join(directory, file.replace(".in", ".out"))):
re = f"Passed {file}"
passed_num += 1
else:
re = f"Failed {file}"
failed_num += 1
with open(os.path.join(directory, file.replace(".in", ".out")),'r') as f: exceptoutput = f.read()
with open(os.path.join(save_dir, f"_{name}.out"),'r') as f: erroroutput = f.read()
Report = Report + re + "\n"
os.remove(os.path.join(save_dir, f"_{name}.out"))
score = int (100 * passed_num / (passed_num + failed_num))
cmd = "cat " + os.path.join(directory, file) + " | " + "python " + os.path.join(root_path, filepath) +" > " + os.path.join(save_dir, f"_{name}.out")
os.system(cmd)
# 比较结果
re = ""
if compare_file(os.path.join(save_dir, f"_{name}.out"), os.path.join(directory, file.replace(".in", ".out"))):
re = f"Passed {file}"
passed_num += 1
else:
re = f"Failed {file}"
failed_num += 1
with open(os.path.join(directory, file.replace(".in", ".out")),'r') as f: exceptoutput = f.read()
with open(os.path.join(save_dir, f"_{name}.out"),'r') as f: erroroutput = f.read()
Report = Report + re + "\n"
os.remove(os.path.join(save_dir, f"_{name}.out"))
score = int (100 * passed_num / (passed_num + failed_num))
return FunctionResponse("judge", True, f"评测得分为 {score}, 在 {passed_num + failed_num} 个测试用例中通过了 {passed_num}个。最后一个错误样例期望输出:'{exceptoutput}',但你的输出:'{erroroutput}")
except Exception as e:
return FunctionResponse("judge", False, f"评测发生意外错误: {str(e)}")
status = FunctionStatus.SUCCESS
return FunctionResponse("judge", status, f"评测得分为 {score}, 在 {passed_num + failed_num} 个测试用例中通过了 {passed_num}个。最后一个错误样例期望输出:'{exceptoutput}',但你的输出:'{erroroutput}")
def compare_file(path1:str, path2:str):
"""

View File

@@ -2,16 +2,17 @@ from pydantic import BaseModel
from enum import Enum
from typing import Optional
FunctionStatus = Enum("FunctionStatus", ["SUCCESS", "FAILED"])
class FunctionResponse(BaseModel):
name: str
status: FunctionStatus
success: bool
message: str = ""
args: Optional[dict] = None
def __init__(self, name: str, status: FunctionStatus, message: str = "", args: Optional[dict] = None):
super().__init__(name=name, status=status, message=message, args=args)
def __init__(self, name: str, success: bool, message: str = "", args: Optional[dict] = None):
super().__init__(name=name, success=success, message=message, args=args)
def to_dict(self):
return self.model_dump()