/usr/share/pyshared/reverend/ui/tester.py is in python-reverend 0.4-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | # This module is part of the Divmod project and is Copyright 2003 Amir Bakhtiar:
# amir@divmod.org. This is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
from __future__ import generators
from Tkinter import *
import tkFileDialog
import tkSimpleDialog
import tkMessageBox
import os
import time
class TestView(Frame):
def __init__(self, parent=None, guesser=None, app=None):
Frame.__init__(self, parent)
self.pack()
self.guesser = guesser
self.app = app
self.size = 300
self.setupViews()
def setupViews(self):
line = Frame(self, relief=RAISED, borderwidth=1)
line.pack(side=TOP, padx=2, pady=1)
colHeadings = [('Guesses', 8), ('Right', 8), ('Wrong', 8), ('Accuracy %', 10)]
currCol = 0
for cHdr, width in colHeadings:
l = Label(line, text=cHdr, width=width, bg='lightblue')
l.grid(row=0, column=currCol)
currCol += 1
line = Frame(self)
line.pack(fill=X)
iGuess = IntVar()
iRight = IntVar()
iWrong = IntVar()
iAcc = IntVar()
self.model = (iGuess, iRight, iWrong, iAcc)
l = Label(line, textvariable=iGuess, anchor=E, width=8, relief=SUNKEN)
l.grid(row=0, column=0)
l = Label(line, textvariable=iRight, anchor=E, width=8, relief=SUNKEN)
l.grid(row=0, column=1)
l = Label(line, textvariable=iWrong, anchor=E, width=8, relief=SUNKEN)
l.grid(row=0, column=2)
l = Label(line, textvariable=iAcc, anchor=E, width=8, relief=SUNKEN)
l.grid(row=0, column=3)
bp = Button(self, text="Run Test", command=self.runTest)
bp.pack(side=BOTTOM)
canvas = Canvas(self, width=self.size, height=self.size, bg='lightyellow')
canvas.pack(expand=YES, fill=BOTH, side=BOTTOM)
self.canvas = canvas
## slid = Scale(self, label='Wrong', variable=iWrong, to=400, orient=HORIZONTAL, bg='red')
## slid.pack(side=BOTTOM)
## slid = Scale(self, label='Right', variable=iRight, to=400, orient=HORIZONTAL, bg='green')
## slid.pack(side=BOTTOM)
def runTest(self):
# TODO - This is nasty re-write
if len(self.guesser) == 0:
tkMessageBox.showwarning('Underprepared for examination!',
'Your guesser has had no training. Please train and retry.')
return
path = tkFileDialog.askdirectory()
if not path:
return
answer = tkSimpleDialog.askstring('Which Pool do these items belong to?', 'Pool name?',
parent=self.app)
if not answer:
return
if answer not in self.guesser.pools:
return
de = DirectoryExam(path, answer, self.app.itemClass)
testCount = len(de)
scale = self.calcScale(testCount)
x = 0
y = 0
cumTime = 0
iGuess, iRight, iWrong, iAcc = self.model
for m, ans in de:
then = time.time()
g = self.guesser.guess(m)
cumTime += time.time() - then
if g:
g = g[0][0]
iGuess.set(iGuess.get()+1)
if g == ans:
col = 'green'
iRight.set(iRight.get()+1)
else:
col = 'red'
iWrong.set(iWrong.get()+1)
iAcc.set(round(100 * iRight.get()/float(iGuess.get()), 3))
# Plot squares
self.canvas.create_rectangle(x*scale,y*scale,(x+1)*scale,(y+1)*scale,fill=col)
if not divmod(iGuess.get(),(int(self.size/scale)))[1]:
# wrap
x = 0
y += 1
else:
x += 1
self.update_idletasks()
guesses = iGuess.get()
self.app.status.log('%r guesses in %.2f seconds. Avg: %.2f/sec.' % (guesses, cumTime,
round(guesses/cumTime, 2)))
def calcScale(self, testCount):
import math
scale = int(self.size/(math.sqrt(testCount)+1))
return scale
class DirectoryExam(object):
"""Creates a iterator that returns a pair at a time.
(Item, correctAnswer). This Exam creates items from
a directory and uses the same answer for each.
"""
def __init__(self, path, answer, itemClass):
self.path = path
self.answer = answer
self.itemClass = itemClass
def __iter__(self):
files = os.listdir(self.path)
for file in files:
fp = open(os.path.join(self.path, file), 'rb')
try:
item = self.itemClass.fromFile(fp)
finally:
fp.close()
if item is None:
continue
yield (item, self.answer)
def __len__(self):
files = os.listdir(self.path)
return len(files)
|