3 from xml
.dom
import minidom
, Node
7 from xml
.dom
.ext
import PrettyPrint
8 from StringIO
import StringIO
10 def toprettyxml_fixed (node
): #, encoding='utf-8'):
11 tmpStream
= StringIO()
12 PrettyPrint(node
, stream
=tmpStream
)# , encoding=encoding)
13 return tmpStream
.getvalue()
16 def IncreaseCounter(self
, qid
, how
, answer
, time
):
18 Return True, is question is good
20 nq
= self
.FindQuestion (qid
)
22 for q
in self
.root
.getElementsByTagName("question"):
23 id = q
.getAttribute ("id")
27 c
= int(q
.getAttribute ("c"))
28 w
= int(q
.getAttribute ("w"))
29 cs
= int(q
.getAttribute ("cs"))
30 ws
= int(q
.getAttribute ("ws"))
34 q
= self
.stat
.createElement(u
'question')
35 q
.setAttribute("id",qid
)
37 sss
= self
.root
.getElementsByTagName("learning")[0]
45 self
.statistics
.append ([qid
, c
, cs
, ws
, w
, ""])
46 nq
= self
.FindQuestion (qid
)
66 # in-memory statistics
67 self
.statistics
[nq
][1] = c
68 self
.statistics
[nq
][2] = cs
69 self
.statistics
[nq
][3] = ws
70 self
.statistics
[nq
][4] = w
73 q
.setAttribute("c",str(c
))
74 q
.setAttribute("w",str(w
))
75 q
.setAttribute("cs",str(cs
))
76 q
.setAttribute("ws",str(ws
))
80 a
= [1,2,4,8][(["a","b","c","d"]).index(answer
)]
83 qq
= self
.stat
.createElement("answer_clicked")
84 qq
.setAttribute ("datetime", str(t
))
85 qq
.setAttribute ("answer_code", str(a
))
86 qq
.setAttribute ("needed_time", str(nt
))
92 rr
= float(cs
)*self
.norm
94 return rr
>= self
.ratio
97 print "Writing statistics file",self
.filename
98 ss
=toprettyxml_fixed(self
.root
)
99 f
=open(self
.filename
,"w")
100 print >>f
,"<!DOCTYPE AFUTrainerStatistics>"
105 print "Opening statistics file",self
.filename
106 self
.stat
= minidom
.parse (self
.filename
)
107 self
.root
= self
.stat
.documentElement
109 self
.date
= self
.root
.getAttribute ("date")
110 self
.version
= self
.root
.getAttribute ("version")
111 self
.name
= self
.root
.getAttribute ("name")
114 def GetStatistics(self
):
115 print "Parsing statistics xml"
120 self
.newquestion
= []
121 self
.goodquestion
= []
122 self
.badquestion
= []
123 self
.norm
= 1./self
.mincorrect
125 for q
in self
.root
.getElementsByTagName("question"):
126 id = q
.getAttribute ("id")
127 c
= q
.getAttribute ("c")
128 cs
= q
.getAttribute ("cs")
129 ws
= q
.getAttribute ("ws")
130 w
= q
.getAttribute ("w")
135 rr
= float(cs
)*self
.norm
138 self
.goodquestion
.append (id)
140 self
.badquestion
.append (id)
141 else: # never reached
142 self
.newquestion
.append (id)
145 for a
in q
.childNodes
:
146 if a
.nodeType
== Node
.ELEMENT_NODE
:
147 code
= a
.getAttribute("answer_code")
148 time
= a
.getAttribute("needed_time")
149 when
= a
.getAttribute("datetime")
150 answers
.append ([code
,time
,when
])
152 self
.statistics
.append ([id, c
, cs
, ws
, w
, answers
])
154 def GetPriority(self
,qid
):
157 def FindQuestion(self
,qid
):
159 for s
in self
.statistics
:
165 def ThisQuestion(self
,qid
):
166 q
= self
.FindQuestion (qid
)
168 c
= self
.statistics
[q
][1]
169 w
= self
.statistics
[q
][4]
170 cs
= self
.statistics
[q
][2]
171 ws
= self
.statistics
[q
][3]
177 return [str(c
),str(w
),str(cs
),str(ws
)]
180 return str(datetime
.datetime
.today().isoformat()).split(".")[0]
182 def __init__(self
,filename
="DL-A-2007.stat.xml"):
183 self
.filename
=filename