Skip to content

Commit d70f114

Browse files
committedFeb 16, 2023
Removed unnecessary docstrings and refactored.
1 parent 6e2b689 commit d70f114

File tree

2 files changed

+19
-54
lines changed

2 files changed

+19
-54
lines changed
 

‎flashcards.py

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self):
1111
self.__chosen_subject = 0
1212
self.__subject_questions = []
1313
self.__subject_answers = []
14-
self.__number_of_subject_questions = 0
14+
self.__number_of_questions_in_subject = 0
1515

1616
self.__random_subject = len(self.__subjects) - 1
1717
self.__random_questions = []
@@ -25,7 +25,6 @@ def __init__(self):
2525
self.__quit_session = False
2626

2727
def start(self):
28-
"""Start the Q&A session."""
2928
if not self.__quit_session:
3029
self.__display_intro()
3130
self.__display_subjects()
@@ -39,7 +38,6 @@ def start(self):
3938
self.start()
4039

4140
def __display_intro(self):
42-
"""Print the program title and instructions."""
4341
if not self.__intro_displayed:
4442
title_length = 35
4543
print("=" * title_length)
@@ -51,77 +49,66 @@ def __display_intro(self):
5149
self.__intro_displayed = True
5250

5351
def __display_subjects(self):
54-
"""Print the available subjects for a Q&A session."""
5552
print("Choose your subject:")
5653
for i, j in enumerate(self.__subjects):
5754
print(f"{(i + 1):>2}. {j}")
5855
print("On any question, input 'q' to quit.")
5956

6057
def __choose_subject(self):
61-
"""Ask the user to choose the Q&A subject."""
6258
chosen_subject = input()
6359
try:
6460
self.__check_valid_subject(chosen_subject)
6561
except ValueError:
6662
self.__check_quit_session(chosen_subject)
6763

6864
def __check_valid_subject(self, chosen_subject):
69-
"""Check if the user has chosen a valid Q&A subject."""
7065
chosen_subject = int(chosen_subject)
7166
if 1 <= chosen_subject <= len(self.__subjects):
7267
self.__chosen_subject = chosen_subject - 1
7368
else:
7469
self.__invalid_subject()
7570

7671
def __check_quit_session(self, chosen_subject):
77-
"""Check if the user has chosen to quit the Q&A session."""
7872
if chosen_subject.lower() == 'q':
7973
self.__display_score()
8074
self.__quit_session = True
8175
else:
8276
self.__invalid_subject()
8377

8478
def __invalid_subject(self):
85-
"""Indicate that an invalid subject has been chosen. Take user to choose another subject."""
8679
print("Invalid option. Please choose again.")
8780
self.__choose_subject()
8881

8982
def __build_qa_session(self):
90-
"""Build a subject or random Q&A session, based on user's choice."""
9183
if self.__chosen_subject == self.__random_subject:
9284
self.__build_random_qa_session()
9385
else:
9486
self.__build_subject_qa_session()
9587

9688
def __build_subject_qa_session(self):
97-
"""Build a subject Q&A session."""
9889
self.__clear_subject_qa_session()
9990
self.__build_subject_questions()
10091
self.__build_subject_answers()
10192

10293
def __clear_subject_qa_session(self):
103-
"""Clear lists of subject questions and answers."""
10494
self.__subject_questions.clear()
10595
self.__subject_answers.clear()
10696

10797
def __build_subject_questions(self):
108-
"""Retrieve questions from the chosen subject."""
10998
filename = self.__subjects_folder \
11099
+ self.__subjects[self.__chosen_subject].lower().replace(' ', '_') \
111100
+ "_questions.txt"
112101
with open(filename) as file_object:
113102
self.__subject_questions = file_object.readlines()
114103

115104
def __build_subject_answers(self):
116-
"""Retrieve answers from the chosen subject."""
117105
filename = self.__subjects_folder\
118106
+ self.__subjects[self.__chosen_subject].lower().replace(' ', '_')\
119107
+ "_answers.txt"
120108
with open(filename) as file_object:
121109
self.__subject_answers = file_object.readlines()
122110

123111
def __build_random_qa_session(self):
124-
"""Retrieve random questions and answers."""
125112
self.__clear_random_qa_session()
126113
while len(self.__random_questions) < self.__number_of_random_questions:
127114
self.__choose_random_subject()
@@ -133,51 +120,41 @@ def __build_random_qa_session(self):
133120
self.__reset_random_subject()
134121

135122
def __clear_random_qa_session(self):
136-
"""Clear lists of random questions and answers."""
137123
self.__random_questions.clear()
138124
self.__random_answers.clear()
139125

140126
def __choose_random_subject(self):
141-
"""Choose a random subject within the range of available subjects."""
142127
self.__chosen_subject = randrange(self.__random_subject)
143128

144129
def __choose_random_question_number(self):
145-
"""Choose a question number within the range of available questions."""
146130
self.__random_question_number = randrange(len(self.__subject_questions))
147131

148132
def __build_random_question(self):
149-
"""Add a new question to the random questions list."""
150133
if self.__subject_questions[self.__random_question_number] not in self.__random_questions:
151134
self.__random_questions.append(self.__subject_questions[self.__random_question_number])
152135

153136
def __build_random_answer(self):
154-
"""Add the corresponding answer to the random answers list."""
155137
if self.__subject_answers[self.__random_question_number] not in self.__random_answers:
156138
self.__random_answers.append(self.__subject_answers[self.__random_question_number])
157139

158140
def __copy_random_questions_and_answers(self):
159-
"""Copy random questions and answers to subject questions and answers."""
160141
self.__subject_questions = self.__random_questions
161142
self.__subject_answers = self.__random_answers
162143

163144
def __reset_random_subject(self):
164-
"""Reset the chosen subject to the random subject."""
165145
self.__chosen_subject = self.__random_subject
166146

167147
def __display_subject_title(self):
168-
"""Print the chosen subject title and the number of questions."""
169-
self.__set_number_of_subject_questions()
170-
print(f"\n--- {self.__subjects[self.__chosen_subject]}: {self.__number_of_subject_questions} questions ---")
148+
self.__set_number_of_questions_in_subject()
149+
print(f"\n--- {self.__subjects[self.__chosen_subject]}: {self.__number_of_questions_in_subject} questions ---")
171150

172-
def __set_number_of_subject_questions(self):
173-
"""Set the total number of questions that the Q&A session will go through."""
151+
def __set_number_of_questions_in_subject(self):
174152
if self.__chosen_subject == self.__random_subject:
175-
self.__number_of_subject_questions = self.__number_of_random_questions
153+
self.__number_of_questions_in_subject = self.__number_of_random_questions
176154
else:
177-
self.__number_of_subject_questions = len(self.__subject_questions)
155+
self.__number_of_questions_in_subject = len(self.__subject_questions)
178156

179157
def __ask_questions(self):
180-
"""Ask the list of questions, one by one."""
181158
for question_number, question in enumerate(self.__subject_questions):
182159
print(f"Q{question_number + 1}. {question[:-1]}")
183160
response = self.__check_answer(question_number)
@@ -188,7 +165,6 @@ def __ask_questions(self):
188165
self.__compute_score(response)
189166

190167
def __check_answer(self, question_number):
191-
"""Check if the user inputs the correct or incorrect answer, or decides to quit."""
192168
answer = input()
193169
if answer == self.__subject_answers[question_number][:-1]:
194170
print("Correct!")
@@ -201,14 +177,12 @@ def __check_answer(self, question_number):
201177
return "incorrect"
202178

203179
def __compute_score(self, response):
204-
"""Compute user's score based on correct and incorrect answers."""
205180
if response == "correct":
206181
self.__correct_answers += 1
207182
elif response == "incorrect":
208183
self.__incorrect_answers += 1
209184

210185
def __display_score(self):
211-
"""Display user's score if there is one."""
212186
total_answers = self.__correct_answers + self.__incorrect_answers
213187
if total_answers > 0:
214188
print("\n--- Results ---")
@@ -218,7 +192,6 @@ def __display_score(self):
218192
print(f"Accuracy rate: {accuracy:.2%}")
219193

220194
def __ask_to_continue(self):
221-
"""Ask the user if he wants to continue with a new Q&A session."""
222195
if not self.__quit_session:
223196
print("\nWould you like to continue with another subject? (y/n)")
224197
continue_with_qa = input()

‎test_flashcards.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55

66
def test_randrange():
7-
"""Test imported function randrange from random module."""
87
assert randrange(1) == 0
98

109

1110
@pytest.fixture()
1211
def cardset():
13-
"""Return a Flashcard object."""
1412
return Flashcard()
1513

1614

@@ -22,16 +20,14 @@ def cardset_with_subject(cardset):
2220
return cardset
2321

2422

25-
def test_choose_subject_valid_subject(cardset, monkeypatch):
26-
"""Test a valid input for the Q&A subject."""
23+
def test_choose_subject_case_valid_subject(cardset, monkeypatch):
2724
chosen_subject = 1
2825
monkeypatch.setattr("builtins.input", lambda: chosen_subject)
2926
cardset._Flashcard__choose_subject()
3027
assert cardset._Flashcard__chosen_subject == 0
3128

3229

33-
def test_choose_subject_quit_session(cardset, monkeypatch):
34-
"""Test quitting the Q&A session from the subject selection."""
30+
def test_choose_subject_case_quit_session(cardset, monkeypatch):
3531
chosen_subject = 'q'
3632
monkeypatch.setattr("builtins.input", lambda: chosen_subject)
3733
cardset._Flashcard__choose_subject()
@@ -72,31 +68,27 @@ def test_build_random_qa_session_answers(cardset):
7268
assert len(cardset._Flashcard__subject_answers) == cardset._Flashcard__number_of_random_questions
7369

7470

75-
def test_set_number_of_subject_questions_subject(cardset_with_subject):
76-
"""Test that the correct number of questions is set for a particular subject."""
77-
cardset_with_subject._Flashcard__set_number_of_subject_questions()
78-
assert cardset_with_subject._Flashcard__number_of_subject_questions \
71+
def test_set_number_of_questions_in_subject_case_subject(cardset_with_subject):
72+
cardset_with_subject._Flashcard__set_number_of_questions_in_subject()
73+
assert cardset_with_subject._Flashcard__number_of_questions_in_subject \
7974
== len(cardset_with_subject._Flashcard__subject_questions)
8075

8176

82-
def test_set_number_of_subject_questions_random(cardset):
83-
"""Test that the correct number of questions is set for the random subject."""
77+
def test_set_number_of_questions_in_subject_case_random(cardset):
8478
cardset._Flashcard__chosen_subject = cardset._Flashcard__random_subject
85-
cardset._Flashcard__set_number_of_subject_questions()
86-
assert cardset._Flashcard__number_of_subject_questions == cardset._Flashcard__number_of_random_questions
79+
cardset._Flashcard__set_number_of_questions_in_subject()
80+
assert cardset._Flashcard__number_of_questions_in_subject == cardset._Flashcard__number_of_random_questions
8781

8882

89-
def test_display_subject_title_subject(cardset_with_subject, capsys):
90-
"""Test that the correct subject title is displayed."""
83+
def test_display_subject_title_case_subject(cardset_with_subject, capsys):
9184
cardset_with_subject._Flashcard__display_subject_title()
9285
stdout, stderr = capsys.readouterr()
9386
assert stdout == (f"\n--- "
9487
f"{cardset_with_subject._Flashcard__subjects[cardset_with_subject._Flashcard__chosen_subject]}: "
95-
f"{cardset_with_subject._Flashcard__number_of_subject_questions} questions ---\n")
88+
f"{cardset_with_subject._Flashcard__number_of_questions_in_subject} questions ---\n")
9689

9790

98-
def test_display_subject_title_random(cardset, capsys):
99-
"""Test that the random subject title is displayed."""
91+
def test_display_subject_title_case_random(cardset, capsys):
10092
cardset._Flashcard__chosen_subject = cardset._Flashcard__random_subject
10193
cardset._Flashcard__display_subject_title()
10294
stdout, stderr = capsys.readouterr()
@@ -113,13 +105,13 @@ def test_check_answer(cardset_with_subject, monkeypatch, mock_answer, expected_r
113105
assert cardset_with_subject._Flashcard__check_answer(question_number) == expected_result
114106

115107

116-
def test_compute_score_correct_answer(cardset):
108+
def test_compute_score_case_correct_answer(cardset):
117109
response = "correct"
118110
cardset._Flashcard__compute_score(response)
119111
assert cardset._Flashcard__correct_answers == 1
120112

121113

122-
def test_compute_score_incorrect_answer(cardset):
114+
def test_compute_score_case_incorrect_answer(cardset):
123115
response = "incorrect"
124116
cardset._Flashcard__compute_score(response)
125117
assert cardset._Flashcard__incorrect_answers == 1

0 commit comments

Comments
 (0)