Documentation, slight refactor.
This commit is contained in:
parent
ebc7deb572
commit
7ac08f910f
1 changed files with 118 additions and 6 deletions
124
quiz.py
124
quiz.py
|
|
@ -1,5 +1,10 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
## -*- coding: utf-8 -*-
|
## -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
A MozQuizz question library for Python.
|
||||||
|
See http://moxquizz.de/ for the original implementation in TCL.
|
||||||
|
"""
|
||||||
|
|
||||||
from __future__ import unicode_literals, print_function
|
from __future__ import unicode_literals, print_function
|
||||||
from io import open
|
from io import open
|
||||||
|
|
||||||
|
|
@ -10,30 +15,103 @@ class Question:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
category = None
|
category = None
|
||||||
|
"""
|
||||||
|
The question category. Arbitrary text; optional.
|
||||||
|
"""
|
||||||
|
|
||||||
question = None
|
question = None
|
||||||
|
"""
|
||||||
|
The question. Arbitrary text; required.
|
||||||
|
"""
|
||||||
|
|
||||||
answer = None
|
answer = None
|
||||||
|
"""
|
||||||
|
The answer. Arbitrary text; required. Correct answers can also be covered
|
||||||
|
by the :attr:`regexp` property.
|
||||||
|
"""
|
||||||
|
|
||||||
regexp = None
|
regexp = None
|
||||||
|
"""
|
||||||
|
A regular expression that will generate correct answers. Optional. See
|
||||||
|
also the :attr:`answer` property.
|
||||||
|
"""
|
||||||
|
|
||||||
author = None
|
author = None
|
||||||
level = None
|
"""
|
||||||
|
The question author. Arbitrary text; optional.
|
||||||
|
"""
|
||||||
|
|
||||||
|
level = None # Default: NORMAL (constructor)
|
||||||
|
"""
|
||||||
|
The difficulty level. Value must be from the :attr:`LEVELS` tuple.
|
||||||
|
The default value is :attr:`NORMAL`.
|
||||||
|
"""
|
||||||
|
|
||||||
comment = None
|
comment = None
|
||||||
score = 0
|
"""
|
||||||
|
A comment. Arbitrary text; optional.
|
||||||
|
"""
|
||||||
|
|
||||||
|
score = 1
|
||||||
|
"""
|
||||||
|
The points scored for the correct answer. Integer value; default is 1.
|
||||||
|
"""
|
||||||
tip = list()
|
tip = list()
|
||||||
|
"""
|
||||||
|
An ordered list of tips (hints) to display to users. Optional.
|
||||||
|
"""
|
||||||
|
|
||||||
tipcycle = 0
|
tipcycle = 0
|
||||||
|
"""
|
||||||
|
Indicates which tip is to be displayed next, if any.
|
||||||
|
"""
|
||||||
|
|
||||||
TRIVIAL = 1
|
TRIVIAL = 1
|
||||||
|
"""
|
||||||
|
A value for :attr:`level` that indicates a question of trivial difficulty.
|
||||||
|
"""
|
||||||
|
|
||||||
EASY = 2
|
EASY = 2
|
||||||
|
"""
|
||||||
|
A value for :attr:`level` that indicates a question of easy difficulty.
|
||||||
|
"""
|
||||||
|
|
||||||
NORMAL = 3
|
NORMAL = 3
|
||||||
|
"""
|
||||||
|
A value for :attr:`level` that indicates a question of average or normal
|
||||||
|
difficulty.
|
||||||
|
"""
|
||||||
|
|
||||||
HARD = 4
|
HARD = 4
|
||||||
|
"""
|
||||||
|
A value for :attr:`level` that indicates a question of hard difficulty.
|
||||||
|
"""
|
||||||
|
|
||||||
EXTREME = 5
|
EXTREME = 5
|
||||||
|
"""
|
||||||
|
A value for :attr:`level` that indicates a question of extreme difficulty
|
||||||
|
or obscurity.
|
||||||
|
"""
|
||||||
|
|
||||||
LEVELS = (TRIVIAL, EASY, NORMAL, HARD, EXTREME)
|
LEVELS = (TRIVIAL, EASY, NORMAL, HARD, EXTREME)
|
||||||
|
"""
|
||||||
|
The available :attr:`level` difficulty values, :attr:`TRIVIAL`, :attr:`EASY`,
|
||||||
|
:attr:`NORMAL`, :attr:`HARD` and :attr:`EXTREME`.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, attributes_dict):
|
def __init__(self, attributes_dict):
|
||||||
|
"""
|
||||||
|
Constructor that takes a dictionary of MoxQuizz key-value pairs. Usually
|
||||||
|
called from a :class:`QuestionBank`.
|
||||||
|
"""
|
||||||
|
# Set defaults first.
|
||||||
|
self.level = self.NORMAL
|
||||||
self.parse(attributes_dict)
|
self.parse(attributes_dict)
|
||||||
|
|
||||||
def parse(self, attributes_dict):
|
def parse(self, attributes_dict):
|
||||||
"""
|
"""
|
||||||
Populate fields from a dictionary of attributes (from a question bank).
|
Populate fields from a dictionary of attributes, usually provided by a
|
||||||
|
:class:`QuestionBank` :attr:`~QuestionBank.parse` call.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
## Valid keys:
|
## Valid keys:
|
||||||
|
|
@ -69,7 +147,9 @@ class Question:
|
||||||
self.category = attributes_dict['Author']
|
self.category = attributes_dict['Author']
|
||||||
|
|
||||||
if 'Level' in attributes_dict.keys() and attributes_dict['Level'] in self.LEVELS:
|
if 'Level' in attributes_dict.keys() and attributes_dict['Level'] in self.LEVELS:
|
||||||
self.level = attributes_dict['level']
|
self.level = attributes_dict['Level']
|
||||||
|
elif 'Level' in attributes_dict.keys() and attributes_dict['Level'] in QuestionBank.LEVEL_VALUES.keys():
|
||||||
|
self.level = QuestionBank.LEVEL_VALUES[attributes_dict['Level']]
|
||||||
|
|
||||||
if 'Comment' in attributes_dict.keys():
|
if 'Comment' in attributes_dict.keys():
|
||||||
self.comment = attributes_dict['Comment']
|
self.comment = attributes_dict['Comment']
|
||||||
|
|
@ -90,7 +170,15 @@ class QuestionBank:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
filename = ''
|
filename = ''
|
||||||
|
"""
|
||||||
|
The path or filename of the question bank file.
|
||||||
|
"""
|
||||||
|
|
||||||
questions = list()
|
questions = list()
|
||||||
|
"""
|
||||||
|
A list of :class:`Question` objects, constituting the questions in the
|
||||||
|
question bank.
|
||||||
|
"""
|
||||||
|
|
||||||
# Case sensitive, to remain backwards-compatible with MoxQuizz.
|
# Case sensitive, to remain backwards-compatible with MoxQuizz.
|
||||||
KEYS = ('Answer',
|
KEYS = ('Answer',
|
||||||
|
|
@ -104,17 +192,34 @@ class QuestionBank:
|
||||||
'Tip',
|
'Tip',
|
||||||
'Tipcycle',
|
'Tipcycle',
|
||||||
)
|
)
|
||||||
|
"""
|
||||||
|
The valid attributes available in a MoxQuizz question bank file.
|
||||||
|
"""
|
||||||
|
|
||||||
|
LEVEL_VALUES = {
|
||||||
|
'trivial': Question.TRIVIAL,
|
||||||
|
'baby': Question.TRIVIAL,
|
||||||
|
'easy': Question.EASY,
|
||||||
|
'normal': Question.NORMAL,
|
||||||
|
'hard': Question.HARD,
|
||||||
|
'difficult': Question.HARD,
|
||||||
|
'extreme': Question.EXTREME
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
Text labels for the :attr:`Question.level` difficulty values.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, filename):
|
def __init__(self, filename):
|
||||||
"""
|
"""
|
||||||
Construct a question bank from a file.
|
Constructor, takes a MozQuizz-formatted question bank filename.
|
||||||
"""
|
"""
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.questions = self.parse(filename)
|
self.questions = self.parse(filename)
|
||||||
|
|
||||||
def parse(self, filename):
|
def parse(self, filename):
|
||||||
"""
|
"""
|
||||||
Read a Moxquizz question bank file into a list.
|
Read a MoxQuizz-formatted question bank file. Returns a ``list`` of
|
||||||
|
:class:`Question` objects found in the file.
|
||||||
"""
|
"""
|
||||||
questions = list()
|
questions = list()
|
||||||
|
|
||||||
|
|
@ -149,6 +254,8 @@ class QuestionBank:
|
||||||
# Fetch the next parameter.
|
# Fetch the next parameter.
|
||||||
try:
|
try:
|
||||||
(key, value) = line.split(':', 1)
|
(key, value) = line.split(':', 1)
|
||||||
|
key = key.strip()
|
||||||
|
value = value.strip()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("Unexpected weirdness in MoxQuizz questionbank '%s', line %s." % (self.filename, i))
|
print("Unexpected weirdness in MoxQuizz questionbank '%s', line %s." % (self.filename, i))
|
||||||
continue
|
continue
|
||||||
|
|
@ -162,6 +269,11 @@ class QuestionBank:
|
||||||
# Enumerate the Tips.
|
# Enumerate the Tips.
|
||||||
if key == 'Tip':
|
if key == 'Tip':
|
||||||
q['Tip'].append(value.strip())
|
q['Tip'].append(value.strip())
|
||||||
|
elif key == 'Level':
|
||||||
|
if value not in self.LEVEL_VALUES:
|
||||||
|
print("Unexpected Level value '%s' in MoxQuizz questionbank '%s', line '%s'." % (value, self.filename, i))
|
||||||
|
else:
|
||||||
|
q['Level'] = self.LEVEL_VALUES[value]
|
||||||
else:
|
else:
|
||||||
q[key] = value.strip()
|
q[key] = value.strip()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue