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
))
76 print "Writing statistics file",self
.filename
77 ss
=toprettyxml_fixed(self
.root
)
78 f
=open(self
.filename
,"w")
79 print >>f
,"<!DOCTYPE AFUTrainerStatistics>"
84 print "Opening statistics file",self
.filename
85 self
.stat
= minidom
.parse (self
.filename
)
86 self
.root
= self
.stat
.documentElement
88 self
.date
= self
.root
.getAttribute ("date")
89 self
.version
= self
.root
.getAttribute ("version")
90 self
.name
= self
.root
.getAttribute ("name")
93 def GetStatistics(self
):
94 print "Parsing statistics xml"
97 for q
in self
.root
.getElementsByTagName("question"):
98 id = q
.getAttribute ("id")
99 c
= q
.getAttribute ("c")
100 cs
= q
.getAttribute ("cs")
101 ws
= q
.getAttribute ("ws")
102 w
= q
.getAttribute ("w")
105 for a
in q
.childNodes
:
106 if a
.nodeType
== Node
.ELEMENT_NODE
:
107 code
= a
.getAttribute("answer_code")
108 time
= a
.getAttribute("needed_time")
109 when
= a
.getAttribute("datetime")
110 answers
.append ([code
,time
,when
])
112 self
.statistics
.append ([id, c
, cs
, ws
, w
, answers
])
114 def FindQuestion(self
,qid
):
116 for s
in self
.statistics
:
122 def ThisQuestion(self
,qid
):
123 q
= self
.FindQuestion (qid
)
125 c
= self
.statistics
[q
][1]
126 w
= self
.statistics
[q
][4]
133 return str(datetime
.datetime
.today().isoformat()).split(".")[0]
135 def __init__(self
,filename
="DL-A-2007.stat.xml"):
136 self
.filename
=filename