Coverage for sel_tools/code_evaluation/report.py: 100%
32 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-04 21:22 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-04 21:22 +0000
1"""Code evaluation report."""
3import json
4from dataclasses import dataclass
5from pathlib import Path
6from typing import Any
8from sel_tools.utils.repo import GitlabProject
10MD_EVALUATION_REPORT = """# Homework Evaluation Report
12[Repo](%s)
14Overall:
16## Auto Evaluation
18```json
19%s
20```
22## Manual Evaluation
23"""
26@dataclass(frozen=True)
27class EvaluationResult:
28 """Evaluation result."""
30 name: str
31 score: int
32 comment: str = ""
35@dataclass
36class EvaluationReport:
37 """Evaluation report."""
39 def __init__(self, gitlab_project: GitlabProject, results: list[EvaluationResult]) -> None:
40 self.repo_path = gitlab_project.local_path
41 self.url = gitlab_project.gitlab_project.web_url
42 self.score = sum(result.score for result in set(results))
43 self.results = results
45 def to_json(self) -> str:
46 class JsonEncoder(json.JSONEncoder):
47 """Evaluation report json encoder."""
49 def default(self, o: Any) -> str | Any:
50 if isinstance(o, Path):
51 return str(o)
52 return o.__dict__
54 return json.dumps(self, cls=JsonEncoder, indent=4)
56 def to_md(self) -> str:
57 return MD_EVALUATION_REPORT % (self.url, self.to_json())
60def write_evaluation_reports(reports: list[EvaluationReport], report_base_name: str) -> None:
61 """Write evaluation reports to disk."""
62 for report in reports:
63 report_path = report.repo_path / report_base_name
64 report_path.with_suffix(".md").write_text(report.to_md())
65 report_path.with_suffix(".json").write_text(report.to_json())