-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclickerQuestions.py
218 lines (179 loc) · 5.72 KB
/
clickerQuestions.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#
# will require a question database
# This module contains the clkrQuestion class, which interprets qblocks,
# and renders itself as html
#
# Also contains methods for parsing tex files and inserting
# questions into a database
#
import re
from string import join
import questions as qu
import web
render = web.template.render('templates/')
banklocation = 'questions/questionbank.tex'
def cleanCSL(csl):
"""
Takes a string representing a comma separated list and removes spaces and trailing colons
"""
csl = csl.replace(' ','')
return csl.strip(',')
def sstrip(s):
return s.strip()
def openFile(filename):
f = open(filename,'r')
return f
def dumpFileToString(filename):
f=openFile(filename)
content = f.read()
f.close()
return content
def noComments(q):
"removes latex comments"
lines = q.split('\n')
pat = re.compile('[^\\\\]%.*')
output = []
for l in lines:
output.append(pat.sub('',l))
return join(output,'\n')
def getEnvironments(en,s):
"Extacts question blocks from a .tex file"
restring = '\\\\begin\\{{{0}\\}}[\\d\\D]*?\\\\end\\{{{0}\\}}'.format(en)
pat = re.compile(restring)
return pat.findall(s)
def extractQuestions(bk):
questions = getEnvironments('clkrQuestion',bk)
return questions
def getBraced(s):
restring = '\\{.*?\\}'
pat = re.compile(restring)
return pat.findall(s)
def deBrace(s):
"removes enclosing braces"
return s.strip("{}")
def fixGraphics(bk):
"""
Takes a qblock and replaces latex code for images with html code.
"""
restring = '\\\\includegraphics.*?\\{.*?\\}'
pat = re.compile(restring)
latexImgLines = pat.findall(bk)#all images
popout = lambda x: deBrace(getBraced(x)[0])
mkimgline = lambda x: "<div class=\"container\"style=\"text-align:center\"><img src=\"/static/images/{0}\"></div>".format(x)#use css for image
htmlize = lambda x:mkimgline(popout(x))
convDic = {}
for i in latexImgLines:
convDic[i]=htmlize(i)
output = bk
for i in convDic:
output = output.replace(i,convDic[i])
output = output.replace("\\begin{center}","")
output = output.replace("\\end{center}","")
return output
def getId(q):
inter = getBraced(q)[1]
return deBrace(inter)
def getTags(q):
inter = getBraced(q)[2]
return deBrace(inter)
def fixChoiceLine(a):
a=a.rstrip()
label = getBraced(a)[0]
label = deBrace(label)
restring = '\\s*\\\\label\\s*\\{{{0}\\}}\\s*'.format(label)
pat = re.compile(restring)
stmt = pat.sub('',a)
return [label,stmt]
def choiceDict(q):#takes an enumerate environment
liq = q.split('\\item')
liq.remove(liq[0])
if liq ==[]:#fixes the no answe choice bug.
return {}
lastentry = liq[len(liq)-1]
lastentry = lastentry.split('\\end{enumerate}')[0]
liq[len(liq)-1]=lastentry
output = {}
for i in liq:
fixi = fixChoiceLine(i)
output[fixi[0]]=fixi[1]
return output
def getChoices(q):
return noComments(getEnvironments('enumerate',q)[0])
def getQStatement(q):
output = noComments(q)
headre = '\\\\begin\\{clkrQuestion\\}\\{.*\\}\\{.*\\}'
headpat = re.compile(headre)
output = headpat.sub('',output) #remove head
tailre = '\\\\begin\\{enumerate\\}[\s\S]*'
tailpat = re.compile(tailre)
output = tailpat.sub('',output)
return output.strip()
def getAnswers(q):
ansre = '\\\\answer\\{.*\\}'
anspat = re.compile(ansre)
ans = anspat.findall(q)
if len(ans) == 0:
return ""
else:
ans = ans[0]
return cleanCSL(deBrace(getBraced(ans)[0]))
def getExplanation(q):
explre = '\\\\explanation\\{.*\\}'
explpat = re.compile(explre)
expl = explpat.findall(q)
if len(expl) == 0:
return ""
else:
expl = expl[0]
return deBrace(getBraced(expl)[0])
class clkrQuestion:
def __init__(self,qblock):
self.content = fixGraphics(noComments(qblock))#fixgraphics for graphics.
self.ID = getId(qblock)
self.tagStr = getTags(qblock)
self.qstmt = getQStatement(qblock)
self.choiceStr = getChoices(qblock)
self.answerStr = getAnswers(qblock)
self.explStr = getExplanation(qblock)
def hasTag(self,tag):
tags = map(sstrip,self.tagStr.split(','))
return (tag in tags)
def getChoiceDict(self):
return choiceDict(self.choiceStr)
def getChoices(self):
output = []
for i in self.getChoiceDict():
output.append(i)
output.sort()
return output
def addToDb(self):
#qu.addQuestion(*map(unicode,[self.ID, self.tagStr, self.content]))
qu.addQuestion(self.ID, self.tagStr, self.content)
def getAnswers(self):
return map(sstrip,self.answerStr.split(','))
def checkAnswer(self,choice):
correct = self.getAnswers()
return choice in correct
# def getAnswerStr(self):
# return self.answerStr
def getTotal(self):
return len(map(sstrip,self.answerStr.split(',')))
def getQuestion(self):
return getQStatement(self.content)
def getRendered(self):
return render.qTemplate(self)
def renderInstructor(self):
return self.showCorrect()
def showResponses(self,tally):
return render.respTemplate(self,tally)
def showCorrect(self):
colordic = self.getChoiceDict()
correct = self.getAnswers()
for i in colordic:
if i in correct:
colordic[i]="LightGreen"
else:
colordic[i]="#FFCCCC"
return render.ansTemplate(self,colordic)
def showEdit(self):
return "This should show the correct answer and a couple buttons."