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
):
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
)
54 # in-memory statistics
55 self
.statistics
[nq
][1] = c
56 self
.statistics
[nq
][2] = cs
57 self
.statistics
[nq
][3] = ws
58 self
.statistics
[nq
][4] = w
61 q
.setAttribute("c",str(c
))
62 q
.setAttribute("w",str(w
))
63 q
.setAttribute("cs",str(cs
))
64 q
.setAttribute("ws",str(ws
))
68 a
= [1,2,4,8][(["a","b","c","d"]).index(answer
)]
71 qq
= self
.stat
.createElement("answer_clicked")
72 qq
.setAttribute ("datetime", str(t
))
73 qq
.setAttribute ("answer_code", str(a
))
74 qq
.setAttribute ("needed_time", str(nt
))
77 # FIXME: UPDATE QUESTION PRIORITIES
80 print "Writing statistics file",self
.filename
81 ss
=toprettyxml_fixed(self
.root
)
82 f
=open(self
.filename
,"w")
83 print >>f
,"<!DOCTYPE AFUTrainerStatistics>"
88 print "Opening statistics file",self
.filename
89 self
.stat
= minidom
.parse (self
.filename
)
90 self
.root
= self
.stat
.documentElement
92 self
.date
= self
.root
.getAttribute ("date")
93 self
.version
= self
.root
.getAttribute ("version")
94 self
.name
= self
.root
.getAttribute ("name")
97 def GetStatistics(self
):
98 print "Parsing statistics xml"
103 self
.newquestion
= []
104 self
.goodquestion
= []
105 self
.badquestion
= []
106 norm
= 1./self
.mincorrect
108 for q
in self
.root
.getElementsByTagName("question"):
109 id = q
.getAttribute ("id")
110 c
= q
.getAttribute ("c")
111 cs
= q
.getAttribute ("cs")
112 ws
= q
.getAttribute ("ws")
113 w
= q
.getAttribute ("w")
121 self
.goodquestion
.append (id)
123 self
.badquestion
.append (id)
124 else: # never reache # never reachedd
125 self
.newquestion
.append (id)
128 for a
in q
.childNodes
:
129 if a
.nodeType
== Node
.ELEMENT_NODE
:
130 code
= a
.getAttribute("answer_code")
131 time
= a
.getAttribute("needed_time")
132 when
= a
.getAttribute("datetime")
133 answers
.append ([code
,time
,when
])
135 self
.statistics
.append ([id, c
, cs
, ws
, w
, answers
])
137 def GetPriority(self
,qid
):
140 def FindQuestion(self
,qid
):
142 for s
in self
.statistics
:
148 def ThisQuestion(self
,qid
):
149 q
= self
.FindQuestion (qid
)
151 c
= self
.statistics
[q
][1]
152 w
= self
.statistics
[q
][4]
153 cs
= self
.statistics
[q
][2]
154 ws
= self
.statistics
[q
][3]
160 return [str(c
),str(w
),str(cs
),str(ws
)]
163 return str(datetime
.datetime
.today().isoformat()).split(".")[0]
165 def __init__(self
,filename
="DL-A-2007.stat.xml"):
166 self
.filename
=filename