.
[HamExam.git] / statistic.py
blob02b3480df05d2f6bdf16707a97f9f69acc74407b
1 #!/usr/bin/python
2 import sys, string
3 from xml.dom import minidom, Node
4 import os
5 import datetime
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 class Statistic:
16 def IncreaseCounter(self, qid, how, answer):
17 nq = self.FindQuestion (qid)
18 if nq >= 0:
19 for q in self.root.getElementsByTagName("question"):
20 id = q.getAttribute ("id")
21 if id == qid:
22 break
24 c = int(q.getAttribute ("c"))
25 w = int(q.getAttribute ("w"))
26 cs = int(q.getAttribute ("cs"))
27 ws = int(q.getAttribute ("ws"))
29 else:
30 print "NEU",qid
31 q = self.stat.createElement(u'question')
32 q.setAttribute("id",qid)
34 sss = self.root.getElementsByTagName("learning")[0]
35 sss.appendChild(q)
37 c=0
38 w=0
39 cs=0
40 ws=0
42 self.statistics.append ([qid, c, cs, ws, w, ""])
43 nq = self.FindQuestion (qid)
45 if how == True:
46 c += 1
47 cs += 1
48 else:
49 w += 1
50 ws += 1
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
58 # xml file stuff
59 q.setAttribute("c",str(c))
60 q.setAttribute("w",str(w))
61 q.setAttribute("cs",str(cs))
62 q.setAttribute("ws",str(ws))
65 t = self.Timestamp()
66 a = [1,2,4,8][(["a","b","c","d"]).index(answer)]
67 nt = 15000 # FIXME
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))
73 q.appendChild(qq)
75 def WriteFile(self):
76 print "Writing statistics file",self.filename
77 ss=toprettyxml_fixed(self.root)
78 f=open(self.filename,"w")
79 print >>f,"<!DOCTYPE AFUTrainerStatistics>"
80 f.write(ss)
81 f.close()
83 def OpenFile(self):
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"
95 self.statistics = []
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")
104 answers = []
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):
115 i = 0
116 for s in self.statistics:
117 if s[0] == qid:
118 return i
119 i += 1
120 return -1
122 def ThisQuestion(self,qid):
123 q = self.FindQuestion (qid)
124 if q >= 0:
125 c = self.statistics[q][1]
126 w = self.statistics[q][4]
127 else:
128 c=str(0)
129 w=str(0)
130 return [c,w]
132 def Timestamp(self):
133 return str(datetime.datetime.today().isoformat()).split(".")[0]
135 def __init__(self,filename="DL-A-2007.stat.xml"):
136 self.filename=filename
138 self.OpenFile()
139 self.GetStatistics()