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

1"""Code evaluation report.""" 

2 

3import json 

4from dataclasses import dataclass 

5from pathlib import Path 

6from typing import Any 

7 

8from sel_tools.utils.repo import GitlabProject 

9 

10MD_EVALUATION_REPORT = """# Homework Evaluation Report 

11 

12[Repo](%s) 

13 

14Overall: 

15 

16## Auto Evaluation 

17 

18```json 

19%s 

20``` 

21 

22## Manual Evaluation 

23""" 

24 

25 

26@dataclass(frozen=True) 

27class EvaluationResult: 

28 """Evaluation result.""" 

29 

30 name: str 

31 score: int 

32 comment: str = "" 

33 

34 

35@dataclass 

36class EvaluationReport: 

37 """Evaluation report.""" 

38 

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 

44 

45 def to_json(self) -> str: 

46 class JsonEncoder(json.JSONEncoder): 

47 """Evaluation report json encoder.""" 

48 

49 def default(self, o: Any) -> str | Any: 

50 if isinstance(o, Path): 

51 return str(o) 

52 return o.__dict__ 

53 

54 return json.dumps(self, cls=JsonEncoder, indent=4) 

55 

56 def to_md(self) -> str: 

57 return MD_EVALUATION_REPORT % (self.url, self.to_json()) 

58 

59 

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())