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()
15 import xml
.etree
.ElementTree
as ET
18 def IncreaseCounter(self
, qid
, how
, answer
):
19 nq
= self
.FindQuestion (qid
)
21 for q
in self
.root
.getElementsByTagName("question"):
22 id = q
.getAttribute ("id")
26 c
= int(q
.getAttribute ("c"))
27 w
= int(q
.getAttribute ("w"))
28 cs
= int(q
.getAttribute ("cs"))
29 ws
= int(q
.getAttribute ("ws"))
33 q
= self
.stat
.createElement(u
'question')
34 q
.setAttribute("id",qid
)
36 sss
= self
.root
.getElementsByTagName("learning")[0]
52 q
.setAttribute("c",str(c
))
53 q
.setAttribute("w",str(w
))
54 q
.setAttribute("cs",str(cs
))
55 q
.setAttribute("ws",str(ws
))
59 a
= [1,2,4,8][(["a","b","c","d"]).index(answer
)]
62 qq
= self
.stat
.createElement("answer_clicked")
63 qq
.setAttribute ("datetime", str(t
))
64 qq
.setAttribute ("answer_code", str(a
))
65 qq
.setAttribute ("needed_time", str(nt
))
72 ss
=toprettyxml_fixed(self
.root
)
73 f
=open(self
.filename
,"w")
74 print >>f
,"<!DOCTYPE AFUTrainerStatistics>"
79 self
.stat
= minidom
.parse (self
.filename
)
80 self
.root
= self
.stat
.documentElement
82 self
.date
= self
.root
.getAttribute ("date")
83 self
.version
= self
.root
.getAttribute ("version")
84 self
.name
= self
.root
.getAttribute ("name")
87 def GetStatistics(self
):
90 for q
in self
.root
.getElementsByTagName("question"):
91 id = q
.getAttribute ("id")
92 c
= q
.getAttribute ("c")
93 cs
= q
.getAttribute ("cs")
94 ws
= q
.getAttribute ("ws")
95 w
= q
.getAttribute ("w")
98 for a
in q
.childNodes
:
99 if a
.nodeType
== Node
.ELEMENT_NODE
:
100 code
= a
.getAttribute("answer_code")
101 time
= a
.getAttribute("needed_time")
102 when
= a
.getAttribute("datetime")
103 answers
.append ([code
,time
,when
])
105 self
.statistics
.append ([id, c
, cs
, ws
, w
, answers
])
107 def FindQuestion(self
,qid
):
109 for s
in self
.statistics
:
115 def ThisQuestion(self
,qid
):
116 q
= self
.FindQuestion (qid
)
118 c
= self
.statistics
[q
][1]
119 w
= self
.statistics
[q
][4]
126 return str(datetime
.datetime
.today().isoformat()).split(".")[0]
128 def __init__(self
,filename
="DL-A-2007.stat.xml"):
129 self
.filename
=filename