62 行
1.8 KiB
Python
62 行
1.8 KiB
Python
"""判题引擎抽象:hydro | judge0 | local(未来更多)。
|
||
|
||
引擎契约(judge.engine 扩展点):
|
||
- submit(pid, lang, code, pretest=False, input=None) -> str # 返回远端记录 ID
|
||
- fetch(rid) -> JudgeResult # 轮询结果
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass, field
|
||
|
||
|
||
@dataclass
|
||
class JudgeCase:
|
||
input: str = ""
|
||
expected: str = ""
|
||
output: str = ""
|
||
status: int = 0 # 0=未判 2=AC 3=WA 5=TLE 7=RE 8=CE
|
||
time_ms: int = 0
|
||
memory_kb: int = 0
|
||
|
||
|
||
@dataclass
|
||
class JudgeResult:
|
||
rid: str
|
||
status: int = 0 # Hydro: 0=等待 1=AC 2=WA 3=TLE 4=MLE 5=OLE 6=RE 7=CE 8=SE 20=判题中
|
||
score: int = 0
|
||
time_ms: int = 0
|
||
memory_kb: int = 0
|
||
test_cases: list[JudgeCase] = field(default_factory=list)
|
||
judge_texts: list[str] = field(default_factory=list)
|
||
|
||
@property
|
||
def verdict(self) -> str:
|
||
return {
|
||
0: "PENDING", 1: "AC", 2: "WA", 3: "TLE", 4: "MLE", 5: "OLE",
|
||
6: "RE", 7: "CE", 8: "SE", 9: "CANCELED", 20: "JUDGING",
|
||
21: "COMPILING", 22: "FETCHED", 30: "IGNORED", 31: "FORMAT_ERROR",
|
||
}.get(self.status, "UNKNOWN")
|
||
|
||
@property
|
||
def done(self) -> bool:
|
||
# NORMAL_STATUS = 1..8(AC/WA/TLE/MLE/OLE/RE/CE/SE)
|
||
return 1 <= self.status <= 8
|
||
|
||
|
||
class JudgeEngineError(Exception):
|
||
"""判题引擎异常(网络/登录/参数),由 service 层捕获转 HTTP。"""
|
||
|
||
|
||
# 兼容别名(适配器内统一用 JudgeError)
|
||
JudgeError = JudgeEngineError
|
||
|
||
|
||
class BaseJudgeEngine:
|
||
name: str = "base"
|
||
|
||
def submit(self, pid: str, lang: str, code: str, pretest: bool = False, input_data: str | None = None) -> str:
|
||
raise NotImplementedError
|
||
|
||
def fetch(self, rid: str) -> JudgeResult:
|
||
raise NotImplementedError
|