选题添加最多次数
This commit is contained in:
@@ -8,6 +8,7 @@ class Question:
|
|||||||
def __init__(self, no: str, topic: str):
|
def __init__(self, no: str, topic: str):
|
||||||
self._no: str = no
|
self._no: str = no
|
||||||
self._topic: str = topic
|
self._topic: str = topic
|
||||||
|
self._count: int = 0
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Question<No: {self._no}, Topic: {self._topic}>"
|
return f"Question<No: {self._no}, Topic: {self._topic}>"
|
||||||
@@ -43,6 +44,12 @@ class Question:
|
|||||||
def topic(self) -> str:
|
def topic(self) -> str:
|
||||||
return self._topic
|
return self._topic
|
||||||
|
|
||||||
|
def increase_count(self) -> None:
|
||||||
|
self._count += 1
|
||||||
|
|
||||||
|
def can_pick(self, max_count: int) -> bool:
|
||||||
|
return self._count <= max_count
|
||||||
|
|
||||||
|
|
||||||
class Student:
|
class Student:
|
||||||
def __init__(self, no: str, so: str, name: str, major: str, class_name: str):
|
def __init__(self, no: str, so: str, name: str, major: str, class_name: str):
|
||||||
@@ -105,11 +112,15 @@ class Student:
|
|||||||
def class_name(self) -> str:
|
def class_name(self) -> str:
|
||||||
return self._class_name
|
return self._class_name
|
||||||
|
|
||||||
def pick_question(self, questions: list[Question], num: int = 3) -> None:
|
def pick_question(self, questions: list[Question], num: int = 3, max_count: int = 3) -> None:
|
||||||
if len(questions) < num:
|
available_questions = [q for q in questions if q.can_pick(max_count)]
|
||||||
raise ValueError("Not enough questions to pick from.")
|
|
||||||
self._picked_questions = random.sample(questions, num)
|
|
||||||
|
|
||||||
|
if len(available_questions) < num:
|
||||||
|
raise ValueError("Not enough questions with count <= max_count")
|
||||||
|
self._picked_questions = random.sample(available_questions, num)
|
||||||
|
|
||||||
|
for q in self._picked_questions:
|
||||||
|
q.increase_count()
|
||||||
|
|
||||||
class Course:
|
class Course:
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str):
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import math
|
||||||
import os
|
import os
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
@@ -39,11 +40,13 @@ class DTGWorker(QObject):
|
|||||||
students = Student.load_from_xls(self.input_filepath)
|
students = Student.load_from_xls(self.input_filepath)
|
||||||
questions = Question.load_from_xls(self.input_question_filepath)
|
questions = Question.load_from_xls(self.input_question_filepath)
|
||||||
|
|
||||||
d = DocPaper(self.output_filename, template_path=resource_path("template/template.docx"))
|
max_question_count = math.ceil(len(students) * 3 / len(questions))
|
||||||
|
|
||||||
|
d = DocPaper(self.output_filename, template_path=resource_path("template/template-2.docx"))
|
||||||
for index, student in enumerate(students):
|
for index, student in enumerate(students):
|
||||||
if (p := int((index + 1) / len(students) * 100)) != 100:
|
if (p := int((index + 1) / len(students) * 100)) != 100:
|
||||||
self.progress[int].emit(p)
|
self.progress[int].emit(p)
|
||||||
student.pick_question(questions)
|
student.pick_question(questions, max_count=max_question_count)
|
||||||
d.add_paper(course, student)
|
d.add_paper(course, student)
|
||||||
d.save(self.output_filepath)
|
d.save(self.output_filepath)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user