Skip to content

Commit

Permalink
Add a solution in the question, allowing the user to write down the d…
Browse files Browse the repository at this point in the history
…etailed answer of the question.

Introduce another way to enter answers.
Allow users to print out solution of the test.
  • Loading branch information
vungocbinh2009 committed Nov 27, 2019
1 parent 1ec40f8 commit 9056dce
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
47 changes: 41 additions & 6 deletions pytexexam/latexexam.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def __init__(self, exam_title: str, exam: Exam):
"""The content of the exam"""
self.question_theorem = "Question"
"""The content of the beginning of each question will be printed"""
self.solution_theorem = "Detailed answer"
"""The content of the beginning of each detailed answer will be printed"""
self.latex_preamble: str = """
\\documentclass[12pt,a4paper,notitlepage]{{article}}
\\usepackage[utf8]{{vietnam}}
Expand Down Expand Up @@ -53,6 +55,15 @@ def add_ams_math_preamble(self):
\\usepackage{amssymb}
""")

@staticmethod
def __print_question(question: Question) -> str:
if question.get_answer_column() == 1:
return LatexExam.__print_question_1(question)
elif question.get_answer_column() == 2:
return LatexExam.__print_question_2(question)
else:
return LatexExam.__print_question_4(question)

@staticmethod
def __print_question_2(question: Question) -> str:
"""
Expand Down Expand Up @@ -143,12 +154,7 @@ def export_tex_exam(self, file_name: str):
"""
question_list_string = ""
for question in self.exam_content.question_list:
if question.get_answer_column() == 1:
question_list_string += (self.__print_question_1(question) + "\n")
elif question.get_answer_column() == 2:
question_list_string += (self.__print_question_2(question) + "\n")
else:
question_list_string += (self.__print_question_4(question) + "\n")
question_list_string += (self.__print_question(question) + "\n")
latex_string = inspect.cleandoc("""
{latex_preamble}
\\begin{{document}}
Expand Down Expand Up @@ -204,3 +210,32 @@ def export_pdf_answer(self, file_name: str):
"""
self.export_tex_answer(file_name)
os.system("pdflatex {file_name}".format(file_name=file_name))

def export_tex_solution(self, file_name: str):
"""Export a file containing detailed answers for each question in the exam"""
solution_string = ""
for question in self.exam_content.question_list:
solution_string += (self.__print_question(question) + "\n")
solution_string += """
\\begin{{solution}}
{solution}
\\end{{solution}}
""".format(solution=question.get_solution())

latex_string = inspect.cleandoc("""
{latex_preamble}
\\newtheorem{{solution}}{{{solution_theorem}}}
\\begin{{document}}
{exam_header}
{solution_list}
\\end{{document}}
""".format(latex_preamble=self.latex_preamble, solution_theorem=self.solution_theorem,
exam_header=self.exam_header, solution_list=solution_string))
file = open(file_name, "wt")
file.write(latex_string)

def export_pdf_solution(self, file_name: str):
"""Export a file containing detailed answers for each question in the exam"""
self.export_tex_solution(file_name)
os.system("pdflatex {file_name}".format(file_name=file_name))
pass
32 changes: 32 additions & 0 deletions pytexexam/question.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from answer import Answer
import random
from typing import Dict


class Question:
Expand All @@ -20,6 +21,8 @@ def __init__(self, question: str):
self.__answer_column = 1
"""Number of columns for which the answer will be presented. Answers can be presented as
1 column, 2 columns or 4 columns"""
self.__solution = ""
"""Solution of the question"""

def answer_a(self, answer: str, true_answer=False):
"""
Expand Down Expand Up @@ -68,6 +71,28 @@ def answer_d(self, answer: str, true_answer=False):
self.__answer_d.answer = answer
self.__answer_d.is_true_answer = true_answer

def answers(self, true_answer: str, answer_dict: Dict[str, str]):
"""
Another way to enter answers to questions.
:param true_answer: The letter that corresponds to the correct answer in the question (A, B,
C, D)
:param answer_dict: A dictionary contains the answers to the questions.The corresponding key
of this dictionary is A, B, C, D.
"""
self.__answer_a.answer = answer_dict.get("A")
self.__answer_b.answer = answer_dict.get("B")
self.__answer_c.answer = answer_dict.get("C")
self.__answer_d.answer = answer_dict.get("D")
if true_answer == "A":
self.__answer_a.is_true_answer = True
elif true_answer == "B":
self.__answer_b.is_true_answer = True
elif true_answer == "C":
self.__answer_c.is_true_answer = True
else:
self.__answer_d.is_true_answer = True

def get_answer(self, answer_number: int) -> str:
"""
This method is used to get answers to questions.
Expand Down Expand Up @@ -133,3 +158,10 @@ def get_true_answer(self) -> str:
return "C"
else:
return "D"

def solution(self, solution: str):
"""This method is used to enter detailed answer to the question"""
self.__solution = solution

def get_solution(self) -> str:
return self.__solution
7 changes: 6 additions & 1 deletion pytexexam/setup.py → setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from setuptools import setup

with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name='pytexexam',
version='1.0',
Expand All @@ -9,5 +12,7 @@
license='Apache License, Version 2.0',
author='binh',
author_email='vungocbinh@protonmail.com',
description='A simple library to create latex exam in python '
description='A simple library to create latex exam in python ',
long_description=long_description,
long_description_content_type="text/markdown",
)

0 comments on commit 9056dce

Please sign in to comment.