allow Exception
This commit is contained in:
@@ -19,43 +19,43 @@ def judge(filepath:str, name: str, course_id: str, lesson_name: str, root_path:s
|
|||||||
directory = 'books/code_tests/'+course_id+'/'+name
|
directory = 'books/code_tests/'+course_id+'/'+name
|
||||||
# 如果目录不存在,报错
|
# 如果目录不存在,报错
|
||||||
if not os.path.exists(directory):
|
if not os.path.exists(directory):
|
||||||
status = FunctionStatus.FAILED
|
return FunctionResponse("judge", False, f"The name {name} is not found.")
|
||||||
return FunctionResponse("judge", status, f"The name {name} is not found.")
|
try:
|
||||||
files = os.listdir(directory)
|
files = os.listdir(directory)
|
||||||
in_files = [file for file in files if file.endswith('.in')]
|
in_files = [file for file in files if file.endswith('.in')]
|
||||||
save_dir = root_path
|
save_dir = root_path
|
||||||
Report = ""
|
Report = ""
|
||||||
passed_num=0
|
passed_num=0
|
||||||
failed_num=0
|
failed_num=0
|
||||||
erroroutput = ""
|
erroroutput = ""
|
||||||
exceptoutput=""
|
exceptoutput=""
|
||||||
for file in in_files:
|
for file in in_files:
|
||||||
file_path = os.path.join(save_dir, f"_{name}.out")
|
file_path = os.path.join(save_dir, f"_{name}.out")
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
os.remove(file_path)
|
os.remove(file_path)
|
||||||
with open(file_path, 'w') as ff:
|
with open(file_path, 'w') as ff:
|
||||||
ff.write("")
|
ff.write("")
|
||||||
|
|
||||||
cmd = "cat " + os.path.join(directory, file) + " | " + "python " + os.path.join(root_path, filepath) +" > " + os.path.join(save_dir, f"_{name}.out")
|
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)
|
os.system(cmd)
|
||||||
# 比较结果
|
# 比较结果
|
||||||
re = ""
|
re = ""
|
||||||
if compare_file(os.path.join(save_dir, f"_{name}.out"), os.path.join(directory, file.replace(".in", ".out"))):
|
if compare_file(os.path.join(save_dir, f"_{name}.out"), os.path.join(directory, file.replace(".in", ".out"))):
|
||||||
re = f"Passed {file}"
|
re = f"Passed {file}"
|
||||||
passed_num += 1
|
passed_num += 1
|
||||||
else:
|
else:
|
||||||
re = f"Failed {file}"
|
re = f"Failed {file}"
|
||||||
failed_num += 1
|
failed_num += 1
|
||||||
with open(os.path.join(directory, file.replace(".in", ".out")),'r') as f: exceptoutput = f.read()
|
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()
|
with open(os.path.join(save_dir, f"_{name}.out"),'r') as f: erroroutput = f.read()
|
||||||
Report = Report + re + "\n"
|
Report = Report + re + "\n"
|
||||||
os.remove(os.path.join(save_dir, f"_{name}.out"))
|
os.remove(os.path.join(save_dir, f"_{name}.out"))
|
||||||
score = int (100 * passed_num / (passed_num + failed_num))
|
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):
|
def compare_file(path1:str, path2:str):
|
||||||
|
|||||||
@@ -2,16 +2,17 @@ from pydantic import BaseModel
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
FunctionStatus = Enum("FunctionStatus", ["SUCCESS", "FAILED"])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class FunctionResponse(BaseModel):
|
class FunctionResponse(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
status: FunctionStatus
|
success: bool
|
||||||
message: str = ""
|
message: str = ""
|
||||||
args: Optional[dict] = None
|
args: Optional[dict] = None
|
||||||
|
|
||||||
def __init__(self, name: str, status: FunctionStatus, message: str = "", args: Optional[dict] = None):
|
def __init__(self, name: str, success: bool, message: str = "", args: Optional[dict] = None):
|
||||||
super().__init__(name=name, status=status, message=message, args=args)
|
super().__init__(name=name, success=success, message=message, args=args)
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return self.model_dump()
|
return self.model_dump()
|
||||||
|
|||||||
Reference in New Issue
Block a user