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
):
17 nq
= self
.FindQuestion (qid
)
19 for q
in self
.root
.getElementsByTagName("question"):
20 id = q
.getAttribute ("id")
24 c
= int(q
.getAttribute ("c"))
25 w
= int(q
.getAttribute ("w"))
26 cs
= int(q
.getAttribute ("cs"))
27 ws
= int(q
.getAttribute ("ws"))
31 q
= self
.stat
.createElement(u
'question')
32 q
.setAttribute("id",qid
)
34 sss
= self
.root
.getElementsByTagName("learning")[0]
42 self
.statistics
.append ([qid
, c
, cs
, ws
, w
, ""])
43 nq
= self
.FindQuestion (qid
)
52 # in-memory statistics
53 self
.statistics
[nq
][1] = c
54 self
.statistics
[nq
][2] = cs
55 self
.statistics
[nq
][3] = ws
56 self
.statistics
[nq
][4] = w
59 q
.setAttribute("c",str(c
))
60 q
.setAttribute("w",str(w
))
61 q
.setAttribute("cs",str(cs
))
62 q
.setAttribute("ws",str(ws
))
66 a
= [1,2,4,8][(["a","b","c","d"]).index(answer
)]
69 qq
= self
.stat
.createElement("answer_clicked")
70 qq
.setAttribute ("datetime", str(t
))
71 qq
.setAttribute ("answer_code", str(a
))
72 qq
.setAttribute ("needed_time", str(nt
))
75 # FIXME: UPDATE QUESTION PRIORITIES
78 print "Writing statistics file",self
.filename
79 ss
=toprettyxml_fixed(self
.root
)
80 f
=open(self
.filename
,"w")
81 print >>f
,"<!DOCTYPE AFUTrainerStatistics>"
86 print "Opening statistics file",self
.filename
87 self
.stat
= minidom
.parse (self
.filename
)
88 self
.root
= self
.stat
.documentElement
90 self
.date
= self
.root
.getAttribute ("date")
91 self
.version
= self
.root
.getAttribute ("version")
92 self
.name
= self
.root
.getAttribute ("name")
95 def GetStatistics(self
):
96 print "Parsing statistics xml"
100 self
.newquestion
= []
101 self
.goodquestion
= []
102 self
.badquestion
= []
103 self
.nnewquestion
= 0
104 self
.ngoodquestion
= 0
105 self
.nbadquestion
= 0
107 for q
in self
.root
.getElementsByTagName("question"):
108 id = q
.getAttribute ("id")
109 c
= q
.getAttribute ("c")
110 cs
= q
.getAttribute ("cs")
111 ws
= q
.getAttribute ("ws")
112 w
= q
.getAttribute ("w")
115 rr
= float(cs
)/float(ws
)
120 self
.goodquestion
.append (id)
121 self
.ngoodquestion
+= 1
123 self
.badquestion
.append (id)
124 self
.nbadquestion
+= 1
126 self
.newquestion
.append (id)
127 self
.nnewquestion
+= 1
130 for a
in q
.childNodes
:
131 if a
.nodeType
== Node
.ELEMENT_NODE
:
132 code
= a
.getAttribute("answer_code")
133 time
= a
.getAttribute("needed_time")
134 when
= a
.getAttribute("datetime")
135 answers
.append ([code
,time
,when
])
137 self
.statistics
.append ([id, c
, cs
, ws
, w
, answers
])
139 def FindQuestion(self
,qid
):
141 for s
in self
.statistics
:
147 def ThisQuestion(self
,qid
):
148 q
= self
.FindQuestion (qid
)
150 c
= self
.statistics
[q
][1]
151 w
= self
.statistics
[q
][4]
158 return str(datetime
.datetime
.today().isoformat()).split(".")[0]
160 def __init__(self
,filename
="DL-A-2007.stat.xml"):
161 self
.filename
=filename