coding utf8 import sys from string import strip from random import shu

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf8 -*-
import sys
from string import strip
from random import shuffle, sample, choice
from optparse import OptionParser
class dictionary:
def __init__(self):
self.answers = []
self.questions = []
self.comments = []
self.confirmation = "!lernu"
def __len__(self):
return len(self.answers)
def add(self, answer, question, comment):
self.answers.append(answer)
self.questions.append(question)
self.comments.append(comment)
def show(self):
for i in range(len(self)):
print """%d:
Question: %s
Answer: %s
Comment: %s""" %(i, self.questions[i], self.answers[i], self.comments[i])
def update(self, filename):
try:
f = open(filename)
except IOError:
sys.exit("Could not open file")
if f.readline().strip() != self.confirmation:
sys.exit("Invalid format of the file")
for line in f:
s = line.strip()
if not s or s[0] == "#": continue
try: cm = s.index("#")
except: cm =len(s)
try: df = s.index("==")
except: continue
answer = s[:df].strip()
#question = map(strip, s[df+2:cm].strip().split(";"))
question = s[df+2:cm].strip()
comment =s[cm+1:].strip()
self.add(answer, question, comment)
def ask(n):
print
print "Question: %s" % src.questions[n]
attempts = settings["attempts"]
while attempts:
attempt = raw_input("Your answer: ")
if attempt == src.answers[n]:
print "Right!"
break
else:
attempts -= 1
print "Wrong."
if not attempts:
print "Right answer: %s" % src.answers[n]
if src.comments[n]: print "Comment: %s" % src.comments[n]
return int(attempts > 0)
def test(n):
variants = sample(xrange(len(src)), settings["VarNum"])
if n in variants:
correct = variants.index(n)
else:
correct =choice(range(settings["VarNum"]))
variants[correct] = n
print
print "Question: %s" % src.questions[n]
for i in range(settings["VarNum"]):
print "%d) %s" %(i, src.answers[variants[i]])
attempts = settings["attempts"]
while attempts:
attempt = raw_input("Number of answer: ")
try: attempt = int(attempt)
except:
print "Enter only the number, please"
continue
if attempt == correct:
print "Right!"
break
else:
attempts -= 1
print "Wrong."
if not attempts:
print "Right answer: %d (%s)" %(correct, src.answers[n])
if settings["ShowComments"] and src.comments[n]:
print "Comment: %s" % src.comments[n]
return int(attempts > 0)
def pack(sequence):
if settings["random"]: shuffle(sequence)
scores = 0
mistakes = []
for i in sequence:
if settings["test"]: correct = test(i)
else: correct = ask(i)
if correct: scores +=1
else: mistakes.append(i)
print "All: %d; Correct: %d" % (len(sequence), scores)
if mistakes:
repeat = raw_input("Do you want to check questions you make mistakes in again? ")
if repeat in settings["positiveResponses"]: pack(mistakes)
def update_settings():
usage = "usage: %prog [options] FILE [FILE]..."
parser = OptionParser(usage=usage)
parser.add_option("-a", "--attempts", dest="attempts",
help="NUMBER of attempts to guess", metavar="NUM")
parser.add_option("-r", "--random", dest="random",
action="store_true", help="shuffle questions")
parser.add_option("-c", "--comments", dest="showComments",
action="store_true", help="print comments after mistakes")
parser.add_option("-t", "--test", dest="test", action="store_true",
help="use tests insted of questions")
parser.add_option("-v", "--variables", dest="varNum",
help="NUMBER of variables in test mode", metavar="NUM")
parser.add_option("-s", "--start", dest="start",
help="start with NUM quetion", metavar="NUM")
parser.add_option("-e", "--end", dest="end",
help="end with NUM question", metavar="NUM")
(opts, args) = parser.parse_args()
if args:
for filename in args:
src.update(filename)
if opts.attempts: settings["attempts"] = opts.attempts
if opts.random: settings["random"] = opts.random
if opts.showComments: settings["showComments"] = opts.showComments
if opts.test: settings["test"] = opts.test
if opts.varNum: settings["varNum"] = opts.varNum
if opts.start: settings["start"] = opts.start
if opts.end: settings["end"] = opts.end
else: settings["end"] = len(src)
else:
parser.error("incorrect number of arguments")
if __name__ == "__main__":
src = dictionary()
settings = {
"random" : False,
"attempts" : 1,
"test" : False,
"varNum" : 3,
"showComments" : False,
"positiveResponses" : ["yes", "y", "ok"] ,
"start" : 0,
"end" : 0
}
update_settings()
src.show()
pack(range(settings["start"], settings["end"]))