Quizbot Readthedocs Io en Latest

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

QuizBot

ducknrone

Apr 06, 2022


CONTENTS:

1 Quiz Foundation 3
1.1 1. Init a quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 2. Create a question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 3. Add a question to quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 4. Attempt to quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 Question types 5
2.1 Multiple choice: QuestionChoice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Single choice: QuestionChoiceSingle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Yes or no : QuestionBool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Check number: QuestionNumber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Check string: QuestionString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3 Telegram bot 7

4 Module Documentation 11
4.1 Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3 Attempt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.4 Bot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.5 Create quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6 Attempt quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.7 Edit quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5 Indices and tables 17

Python Module Index 19

Index 21

i
ii
QuizBot

QuizBot is a Telegram bot with which you can create and attempt quizzes. A quiz is a collection of possibly random
questions.
The processes and conversation of creating, attempting to, renaming, and removing existing quizzes can be shown as
automata-diagrams. Please take a look at the docs, specifically the part of the bot itself.
If you want to read more about the quiz foundation, take a look into Quiz Foundation. If you want to read more about
the question types, take a look into Question types.

CONTENTS: 1
QuizBot

2 CONTENTS:
CHAPTER

ONE

QUIZ FOUNDATION

With the Quiz Foundation you can. . . - . . . create Quizzes with quiz.quiz.py - . . . create Questions of different kinds
with quiz.question_factory.py - . . . attempt quizzes with quiz.attempt.py.
Lets get threw the process.

1.1 1. Init a quiz

With the class Quiz in quiz.quiz.py, you create a new quiz by creating a new Quiz-object. You can specify an author by
entering the authors’ name by argument. Otherwise, the author is an empty string.

my_quiz = Quiz("my_name")

Before you can add a question to the quiz you have to create one.

1.2 2. Create a question

You can create a new question by using one of the subclasses of Question in quiz.question_factory.py. You have to
pass two arguments by the creator: - the question itself as a string - the correct answer(s separated by a ‘,’ if it’s a
multiple-choice question) as a string.
Whatever kind of question you choose, the program checks if the entered answer is convertible to the wanted format.
In this case, you just want to ask for a string.

>>> my_question = QuestionString("What's the best project?", "QuizBot")

Now we want to add this question to our quiz.

1.3 3. Add a question to quiz

We can add an instance of the class Question simply by calling my_quiz.add_question.

>>> my_quiz.add_question(my_question)

If we’d want to add more questions to the quiz, we would create a new question and add it. Otherwise, we could specify
if we want to print questions in random order (and more stuff ;). At the moment we don’t want to do more. But we
want to attempt it.

3
QuizBot

1.4 4. Attempt to quiz

We can attempt to a quiz by passing it to the constructor of the Attempt class in quiz.attempt.py.

>>> my_attempt = Attempt(my_quiz)

Now we can if it has a question left with has_next_question. In that case, we can ask for the question itself with
act_question. If we know the answer to the question, we pass it by input_answer as a string and call enter_answer to
enter it. You can check whether you were right or not by checking the return value of enter_answer.

>>> print(my_attempt.has_next_question())
true
>>> print(my_attempt.act_question)
"What's the best project?"
>>> act_question.input_answer("QuizBot")
>>> print(act_question.enter_answer())
true
>>> print(my_attempt.has_next_question())
false

Because we don’t have questions left, we can check the result of every question in my_attempt.user_points. That’s
it. . . it’s not rocket science ;)

4 Chapter 1. Quiz Foundation


CHAPTER

TWO

QUESTION TYPES

You can use different kinds of questions in your quizzes. But you are free to add more by creating a subclass of Question.
Here are the types which already exist.

2.1 Multiple choice: QuestionChoice

What is QuizBot?
1. A Telegram bot
2. A python application
3. A city
Answer: A Telegram bot, A python application

my_question = QuestionChoice("What is QuizBot?", "A Telegram bot, A python application")


my_question.add_possible_answer("A city")

2.2 Single choice: QuestionChoiceSingle

What is Quizbot?
1. A Telegram bot
2. A dish
3. A fish
Answer: A Telegram bot

my_question = QuestionChoiceSingle("What is QuizBot?", "A Telegram bot")


my_question.add_possible_answer("A dish")
my_question.add_possible_answer("A fish")

5
QuizBot

2.3 Yes or no : QuestionBool

Is Quizbot a telegram bot?


• yes
• no
Answer: yes

my_question = QuestionBool("What is QuizBot?", "Yes")

2.4 Check number: QuestionNumber

In which year was QuizBot created?


Answer: 2020

my_question = QuestionNumber("In which year was QuizBot created?", "2020")

2.5 Check string: QuestionString

Where can you contribute?


Answer: Github

my_question = QuestionString("Where can you contribute?", "Github")

6 Chapter 2. Question types


CHAPTER

THREE

TELEGRAM BOT

The telegram bot is using the quiz foundation and it has three main functions: - creating a quiz, - attempt a quiz, - edit
data at database.
Every one of them uses a ConversationHandler and their way to work is specified in: - bot.create_quiz.py, -
bot.attempt_quiz.py, - bot.edit_quiz.py.
The best way to show their way to work is by using an automata diagram. You can cancel the process during the creation
and attempt like in the renaming and removing process.

Fig. 1: The automata of the process to create a new quiz

The rename- and remove-process takes place in bot.edit_quiz.py. Saving, loading, renaming and deleting a quiz works
with mongoDB

7
QuizBot

Fig. 2: The automata of the process to attempt to a quiz

Fig. 3: The automata of the process to rename an existing quiz

8 Chapter 3. Telegram bot


QuizBot

Fig. 4: The automata of the process to remove an existing quiz

9
QuizBot

10 Chapter 3. Telegram bot


CHAPTER

FOUR

MODULE DOCUMENTATION

This page contains documentation of everything QuizBot has to offer.

4.1 Quiz

With this module, you can create quizzes with questions different kinds.
class quizbot.quiz.quiz.Quiz(author='')
Bases: object
An Instance of the class Quiz has a list of questions, which defines the Quiz. You can choose whether
• the order of the questions is random
• the result of the entered answer is shown after the question
• the result of the entered answer of every question is shown after the quiz
__init__(author='') → None
Initializes an instance of the class Quiz.
Parameters author – Author of the quiz
add_question(new_question: quizbot.quiz.question_factory.Question)
Add an instance of the class Question to the list of questions.
Parameters new_question – New question of the quiz.
get_questions()
Returns a copy of the list of questions.
Returns List of questions.

4.2 Questions

With this module, you can create questions different kinds.


class quizbot.quiz.question_factory.Question(question, correct_answer)
Bases: object
General class for questions.

11
QuizBot

__init__(question, correct_answer)
Initialize a question by the question and the correct answer. Additionally, it initializes the user answer as
an empty string.
Parameters
• question – Question of the question-instance as string.
• correct_answer – Correct answer of the question in (question specific) type.
Raises AssertionError – If the question or the correct answer is empty.
check_solution()
Checks the entered solution of the user with the correct answer.
Returns Boolean value whether the entered solution equals the correct answer.
Raises AssertionError – No solution was entered by the user yet.
enter_solution(answer)
Enters the answer by the user.
Parameters answer – Answer by the user as a string.
class quizbot.quiz.question_factory.QuestionBool(question, correct_answer)
Bases: quizbot.quiz.question_factory.Question
Subclass for questions with a boolean value as answer. Inherits by question.
__init__(question, correct_answer)
Initialize a question by the question and the correct answer. Additionally, it initializes the user answer as
an empty string.
Parameters
• question – Question of the question-instance as string.
• correct_answer – Correct answer of the question as boolean value (True or False).
Raises AssertionError – If the question or the correct answer is not a boolean value.
check_solution()
Compares the entered answer by the user with the correct boolean value.
Returns Boolean value whether the entered value equals the correct value.
Raises AssertionError – If no solution was entered by the user yet.
enter_solution(answer)
Enters the users’ answer.
Parameters answer – Answer by the user as a string.
Raises AssertionError – If the entered answer isn’t “True” or “False”
class quizbot.quiz.question_factory.QuestionChoice(question, correct_answer)
Bases: quizbot.quiz.question_factory.Question
Subclass for questions with multiple possible and correct answers. Inherits by question.
__init__(question, correct_answer)
Initialize a question by the question and the correct answer. Additionally, it initializes the user answer as
an empty string, the randomness as False and the list of possible answers as a list of correct answers.
Parameters

12 Chapter 4. Module Documentation


QuizBot

• question – Question of the question-instance as string.


• correct_answer – Correct answers of the question as string (seperated by comma).
Raises AssertionError – If the question is empty or the count of correct answers is smaller
than one.
add_possible_answer(new_answer)
Adds an answer to the list of possible answers.
Parameters new_answer – New answer in the list of possible answers as string.
check_solution()
Checks if the intersection of the list of entered answers and the list of correct answers equals the entered
answers.
Returns Boolean value if the entered values equals the correct values.
Raises AssertionError – If no solution was entered by the user yet.
class quizbot.quiz.question_factory.QuestionChoiceSingle(question='', correct_answer='')
Bases: quizbot.quiz.question_factory.QuestionChoice
Subclass for questions with multiple possible and correct answers. Inherits by question_choice.
__init__(question='', correct_answer='')
Initialize a question by the question and the correct answer. Additionally, it initializes the user answer as
an empty string, the randomness as False and the list of possible answers as an empty list.
Parameters
• question – Question of the question-instance as string.
• correct_answer – Correct answers of the question as list.
Raises AssertionError – If the question is empty or the count of correct answers doesn’t equal
one.
enter_solution(answer)
Enters the answer by the user.
Parameters answer – Answer by the user as a string.
Raises AssertionError – If the entered string includes more than one answer.
class quizbot.quiz.question_factory.QuestionNumber(question, correct_answer)
Bases: quizbot.quiz.question_factory.Question
Subclass for questions with an integer as answer. Inherits from question.
__init__(question, correct_answer)
Initialize a question by the question and the correct answer. Additionally, it initializes the user answer as
an empty string.
Parameters
• question – Question of the question-instance as string.
• correct_answer – Correct answer of the question as number.
Raises AssertionError – If the question or the correct answer is not a number.

4.2. Questions 13
QuizBot

check_solution()
Compares the entered answer by the user with the correct number.
Returns Boolean value whether the entered number equals the correct number.
Raises AssertionError – If no solution was entered by the user yet.
enter_solution(answer)
Enters the answer by the user.
Parameters answer – Answer by the user as a string.
Raises ValueError – If answer is not an integer.
class quizbot.quiz.question_factory.QuestionString(question, correct_answer)
Bases: quizbot.quiz.question_factory.Question
Subclass for questions with a string as answer. Inherits by question.
check_solution()
Checks the entered answer of the user with the correct string.
Returns Boolean value whether the entered string equals the correct string.
Raises AssertionError – If no solution was entered by the user yet.

4.3 Attempt

With this module you can create one attemp of a quiz.


class quizbot.quiz.attempt.Attempt(quiz: quizbot.quiz.quiz.Quiz)
Bases: object
An Instance of the class Attempt has a quiz and a list of the left questions.
__init__(quiz: quizbot.quiz.quiz.Quiz) → None
Initializes an instance of the class Attempt. It shuffeles the question if the quiz specifies it.
Parameters quiz – Quiz which wants the user to attempt.
act_question()
Returns the current question.
Returns Current question.
enter_answer()
Checks the users’ answer and removes the current question from the list.
Returns A pair of a boolean value whether the user answer was correct and the correct answer.
has_next_question()
Checks if a question is left.
Returns If a question is left.
input_answer(user_answer)
Enter one user answer.
Parameters user_answer – A answer by the user

14 Chapter 4. Module Documentation


QuizBot

4.4 Bot

Telegram bot to create and attempt to quizzes.


quizbot.bot.bot.error(update, context)
Log Errors caused by Updates.
quizbot.bot.bot.print_help(update, _)
Send a message when the command /help is issued.
quizbot.bot.bot.setup_bot(updater)
Setups the handlers

4.5 Create quiz

Module with methods to create a quiz with a telegram bot


quizbot.bot.create_quiz.cancel(update, _)
Cancels a creation ofa quiz by deleting the users’ entries.
quizbot.bot.create_quiz.enter_answer(update, _)
After entering the correct answer it tries to process it. If it fails, it asks for the correct answer again. Otherwise,
it asks for additional possible answers, if the question is an instance of QuestionChoice. Otherwise, it adds the
question to the quiz and asks for the type of the next question.
quizbot.bot.create_quiz.enter_possible_answer(update, _)
After entering additional possible answers, it asks whether the order of the answers should be random.
quizbot.bot.create_quiz.enter_question(update, _)
Asks for the correct answer to the question after entering the question itself.
quizbot.bot.create_quiz.enter_quiz_name(update, context)
After entering the name of the quiz, it looks up if the quiz name is occupied. Otherwise is saves the quiz.
quizbot.bot.create_quiz.enter_randomness_question(update, _)
After entering whether the order if the answers should be random, it adds the question to the quiz. After that, it
asks for the type of next question.
quizbot.bot.create_quiz.enter_randomness_quiz(update, _)
After entering whether the order if the questions should be random, it asks if the result of the question be displayed
after the question itself.
quizbot.bot.create_quiz.enter_result_after_question(update, _)
After entering whether the result of the question should be displayed after the question itself, it asks if the result
of every question be displayed after the quiz.
quizbot.bot.create_quiz.enter_result_after_quiz(update, _)
After entering whether the result of every question should be displayed after the quiz, it asks for the name of the
quiz?
quizbot.bot.create_quiz.enter_type(update, _)
After entering the new question type, it asks for the question itself, if the entered string isn’t ‘Enter’. Otherwise,
it asks if the question should be displayed in random order.
quizbot.bot.create_quiz.start(update, _)
Starts a conversation about quiz creation. Welcomes the user and asks for the type of the first question.

4.4. Bot 15
QuizBot

4.6 Attempt quiz

Module with methods to attempt to a quiz with a telegram bot


quizbot.bot.attempt_quiz.ask_question(update)
Formats the keyboard and prints the current question.
quizbot.bot.attempt_quiz.cancel(update, _)
Cancels an attempt to a quiz by deleting the users’ entries.
quizbot.bot.attempt_quiz.enter_answer(update, _)
It processes the answer to a question and asks a new question, if possible. Otherwise, it prints results.
quizbot.bot.attempt_quiz.enter_quiz(update, context)
Enters a quiz. Try to load a quiz from the input. If it succeeded, the bot asks the first question. Otherwise, it asks
for another quiz.
quizbot.bot.attempt_quiz.start(update, _)
Starts a conversation about an attempt at a quiz. Welcomes the user and asks for a quiz.

4.7 Edit quiz

Module with methods to rename and remove a quiz with a telegram bot
quizbot.bot.edit_quiz.cancel_edit(update, _)
Cancels the process of deletion or renaming.
quizbot.bot.edit_quiz.enter_name_remove(update, context)
Deltes a quiz after entering its’ name.
quizbot.bot.edit_quiz.enter_new_name(update, context)
After entering the new name of the quiz, it renames it.
quizbot.bot.edit_quiz.enter_old_name(update, context)
After entering the old quiz name, it asks for the new one.
quizbot.bot.edit_quiz.start_remove(update, _)
Start a process to remove a quiz.
quizbot.bot.edit_quiz.start_rename(update, _)
Starts a process to rename a quiz.

16 Chapter 4. Module Documentation


CHAPTER

FIVE

INDICES AND TABLES

• genindex
• modindex
• search

17
QuizBot

18 Chapter 5. Indices and tables


PYTHON MODULE INDEX

q
quizbot.bot.attempt_quiz, 16
quizbot.bot.bot, 15
quizbot.bot.create_quiz, 15
quizbot.bot.edit_quiz, 16
quizbot.quiz.attempt, 14
quizbot.quiz.question_factory, 11
quizbot.quiz.quiz, 11

19
QuizBot

20 Python Module Index


INDEX

Symbols 16
__init__() (quizbot.quiz.attempt.Attempt method), 14 enter_answer() (in module quizbot.bot.create_quiz), 15
__init__() (quizbot.quiz.question_factory.Question enter_answer() (quizbot.quiz.attempt.Attempt method),
method), 11 14
__init__() (quizbot.quiz.question_factory.QuestionBool enter_name_remove() (in module
method), 12 quizbot.bot.edit_quiz), 16
__init__() (quizbot.quiz.question_factory.QuestionChoiceenter_new_name() (in module quizbot.bot.edit_quiz),
method), 12 16
enter_old_name()
__init__() (quizbot.quiz.question_factory.QuestionChoiceSingle (in module quizbot.bot.edit_quiz),
method), 13 16
__init__() (quizbot.quiz.question_factory.QuestionNumber enter_possible_answer() (in module
method), 13 quizbot.bot.create_quiz), 15
__init__() (quizbot.quiz.quiz.Quiz method), 11 enter_question() (in module quizbot.bot.create_quiz),
15
A enter_quiz() (in module quizbot.bot.attempt_quiz), 16
enter_quiz_name() (in module
act_question() (quizbot.quiz.attempt.Attempt method),
quizbot.bot.create_quiz), 15
14
enter_randomness_question() (in module
add_possible_answer()
quizbot.bot.create_quiz), 15
(quizbot.quiz.question_factory.QuestionChoice
enter_randomness_quiz() (in module
method), 13
quizbot.bot.create_quiz), 15
add_question() (quizbot.quiz.quiz.Quiz method), 11
enter_result_after_question() (in module
ask_question() (in module quizbot.bot.attempt_quiz),
quizbot.bot.create_quiz), 15
16
enter_result_after_quiz() (in module
Attempt (class in quizbot.quiz.attempt), 14
quizbot.bot.create_quiz), 15
C enter_solution() (quizbot.quiz.question_factory.Question
method), 12
cancel() (in module quizbot.bot.attempt_quiz), 16
enter_solution() (quizbot.quiz.question_factory.QuestionBool
cancel() (in module quizbot.bot.create_quiz), 15
method), 12
cancel_edit() (in module quizbot.bot.edit_quiz), 16
enter_solution() (quizbot.quiz.question_factory.QuestionChoiceSingle
check_solution() (quizbot.quiz.question_factory.Question
method), 13
method), 12
enter_solution() (quizbot.quiz.question_factory.QuestionNumber
check_solution() (quizbot.quiz.question_factory.QuestionBool
method), 14
method), 12
enter_type() (in module quizbot.bot.create_quiz), 15
check_solution() (quizbot.quiz.question_factory.QuestionChoice
error() (in module quizbot.bot.bot), 15
method), 13
check_solution() (quizbot.quiz.question_factory.QuestionNumber
method), 13
G
get_questions() (quizbot.quiz.quiz.Quiz method), 11
check_solution() (quizbot.quiz.question_factory.QuestionString
method), 14
H
E has_next_question() (quizbot.quiz.attempt.Attempt
enter_answer() (in module quizbot.bot.attempt_quiz), method), 14

21
QuizBot

I
input_answer() (quizbot.quiz.attempt.Attempt method),
14

M
module
quizbot.bot.attempt_quiz, 16
quizbot.bot.bot, 15
quizbot.bot.create_quiz, 15
quizbot.bot.edit_quiz, 16
quizbot.quiz.attempt, 14
quizbot.quiz.question_factory, 11
quizbot.quiz.quiz, 11

P
print_help() (in module quizbot.bot.bot), 15

Q
Question (class in quizbot.quiz.question_factory), 11
QuestionBool (class in quizbot.quiz.question_factory),
12
QuestionChoice (class in
quizbot.quiz.question_factory), 12
QuestionChoiceSingle (class in
quizbot.quiz.question_factory), 13
QuestionNumber (class in
quizbot.quiz.question_factory), 13
QuestionString (class in
quizbot.quiz.question_factory), 14
Quiz (class in quizbot.quiz.quiz), 11
quizbot.bot.attempt_quiz
module, 16
quizbot.bot.bot
module, 15
quizbot.bot.create_quiz
module, 15
quizbot.bot.edit_quiz
module, 16
quizbot.quiz.attempt
module, 14
quizbot.quiz.question_factory
module, 11
quizbot.quiz.quiz
module, 11

S
setup_bot() (in module quizbot.bot.bot), 15
start() (in module quizbot.bot.attempt_quiz), 16
start() (in module quizbot.bot.create_quiz), 15
start_remove() (in module quizbot.bot.edit_quiz), 16
start_rename() (in module quizbot.bot.edit_quiz), 16

22 Index

You might also like