.
[HamExam.git] / statistic.py
blob017d2b3ce33d7c620c95bead48ecad66089f1139
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 import xml.etree.ElementTree as ET
17 class Statistic:
18 def IncreaseCounter(self, qid, how, answer):
19 nq = self.FindQuestion (qid)
20 if nq >= 0:
21 for q in self.root.getElementsByTagName("question"):
22 id = q.getAttribute ("id")
23 if id == qid:
24 break
26 c = int(q.getAttribute ("c"))
27 w = int(q.getAttribute ("w"))
28 cs = int(q.getAttribute ("cs"))
29 ws = int(q.getAttribute ("ws"))
31 else:
32 print "NEU",qid
33 q = self.stat.createElement(u'question')
34 q.setAttribute("id",qid)
36 sss = self.root.getElementsByTagName("learning")[0]
37 sss.appendChild(q)
39 c=0
40 w=0
41 cs=0
42 ws=0
45 if how == True:
46 c += 1
47 cs += 1
48 else:
49 w += 1
50 ws += 1
52 q.setAttribute("c",str(c))
53 q.setAttribute("w",str(w))
54 q.setAttribute("cs",str(cs))
55 q.setAttribute("ws",str(ws))
58 t = self.Timestamp()
59 a = [1,2,4,8][(["a","b","c","d"]).index(answer)]
60 nt = 15000 # FIXME
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))
66 q.appendChild(qq)
68 self.GetStatistics ()
69 self.WriteFile()
71 def WriteFile(self):
72 ss=toprettyxml_fixed(self.root)
73 f=open(self.filename,"w")
74 print >>f,"<!DOCTYPE AFUTrainerStatistics>"
75 f.write(ss)
76 f.close()
78 def OpenFile(self):
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):
88 self.statistics = []
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")
97 answers = []
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):
108 i = 0
109 for s in self.statistics:
110 if s[0] == qid:
111 return i
112 i += 1
113 return -1
115 def ThisQuestion(self,qid):
116 q = self.FindQuestion (qid)
117 if q >= 0:
118 c = self.statistics[q][1]
119 w = self.statistics[q][4]
120 else:
121 c=str(0)
122 w=str(0)
123 return [c,w]
125 def Timestamp(self):
126 return str(datetime.datetime.today().isoformat()).split(".")[0]
128 def __init__(self,filename="DL-A-2007.stat.xml"):
129 self.filename=filename
131 self.OpenFile()
132 self.GetStatistics()