<s> from __future__ import generators <EOL> from Tkinter import * <EOL> import tkFileDialog <EOL> import tkSimpleDialog <EOL> import tkMessageBox <EOL> import os <EOL> import time <EOL> class TestView ( Frame ) : <EOL> def __init__ ( self , parent = None , guesser = None , app = None ) : <EOL> Frame . __init__ ( self , parent ) <EOL> self . pack ( ) <EOL> self . guesser = guesser <EOL> self . app = app <EOL> self . size = <NUM_LIT> <EOL> self . setupViews ( ) <EOL> def setupViews ( self ) : <EOL> line = Frame ( self , relief = RAISED , borderwidth = <NUM_LIT:1> ) <EOL> line . pack ( side = TOP , padx = <NUM_LIT:2> , pady = <NUM_LIT:1> ) <EOL> colHeadings = [ ( '<STR_LIT>' , <NUM_LIT:8> ) , ( '<STR_LIT>' , <NUM_LIT:8> ) , ( '<STR_LIT>' , <NUM_LIT:8> ) , ( '<STR_LIT>' , <NUM_LIT:10> ) ] <EOL> currCol = <NUM_LIT:0> <EOL> for cHdr , width in colHeadings : <EOL> l = Label ( line , text = cHdr , width = width , bg = '<STR_LIT>' ) <EOL> l . grid ( row = <NUM_LIT:0> , column = currCol ) <EOL> currCol += <NUM_LIT:1> <EOL> line = Frame ( self ) <EOL> line . pack ( fill = X ) <EOL> iGuess = IntVar ( ) <EOL> iRight = IntVar ( ) <EOL> iWrong = IntVar ( ) <EOL> iAcc = IntVar ( ) <EOL> self . model = ( iGuess , iRight , iWrong , iAcc ) <EOL> l = Label ( line , textvariable = iGuess , anchor = E , width = <NUM_LIT:8> , relief = SUNKEN ) <EOL> l . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:0> ) <EOL> l = Label ( line , textvariable = iRight , anchor = E , width = <NUM_LIT:8> , relief = SUNKEN ) <EOL> l . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:1> ) <EOL> l = Label ( line , textvariable = iWrong , anchor = E , width = <NUM_LIT:8> , relief = SUNKEN ) <EOL> l . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:2> ) <EOL> l = Label ( line , textvariable = iAcc , anchor = E , width = <NUM_LIT:8> , relief = SUNKEN ) <EOL> l . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:3> ) <EOL> bp = Button ( self , text = "<STR_LIT>" , command = self . runTest ) <EOL> bp . pack ( side = BOTTOM ) <EOL> canvas = Canvas ( self , width = self . size , height = self . size , bg = '<STR_LIT>' ) <EOL> canvas . pack ( expand = YES , fill = BOTH , side = BOTTOM ) <EOL> self . canvas = canvas <EOL> def runTest ( self ) : <EOL> if len ( self . guesser ) == <NUM_LIT:0> : <EOL> tkMessageBox . showwarning ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> return <EOL> path = tkFileDialog . askdirectory ( ) <EOL> if not path : <EOL> return <EOL> answer = tkSimpleDialog . askstring ( '<STR_LIT>' , '<STR_LIT>' , <EOL> parent = self . app ) <EOL> if not answer : <EOL> return <EOL> if answer not in self . guesser . pools : <EOL> return <EOL> de = DirectoryExam ( path , answer , self . app . itemClass ) <EOL> testCount = len ( de ) <EOL> scale = self . calcScale ( testCount ) <EOL> x = <NUM_LIT:0> <EOL> y = <NUM_LIT:0> <EOL> cumTime = <NUM_LIT:0> <EOL> iGuess , iRight , iWrong , iAcc = self . model <EOL> for m , ans in de : <EOL> then = time . time ( ) <EOL> g = self . guesser . guess ( m ) <EOL> cumTime += time . time ( ) - then <EOL> if g : <EOL> g = g [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> iGuess . set ( iGuess . get ( ) + <NUM_LIT:1> ) <EOL> if g == ans : <EOL> col = '<STR_LIT>' <EOL> iRight . set ( iRight . get ( ) + <NUM_LIT:1> ) <EOL> else : <EOL> col = '<STR_LIT>' <EOL> iWrong . set ( iWrong . get ( ) + <NUM_LIT:1> ) <EOL> iAcc . set ( round ( <NUM_LIT:100> * iRight . get ( ) / float ( iGuess . get ( ) ) , <NUM_LIT:3> ) ) <EOL> self . canvas . create_rectangle ( x * scale , y * scale , ( x + <NUM_LIT:1> ) * scale , ( y + <NUM_LIT:1> ) * scale , fill = col ) <EOL> if not divmod ( iGuess . get ( ) , ( int ( self . size / scale ) ) ) [ <NUM_LIT:1> ] : <EOL> x = <NUM_LIT:0> <EOL> y += <NUM_LIT:1> <EOL> else : <EOL> x += <NUM_LIT:1> <EOL> self . update_idletasks ( ) <EOL> guesses = iGuess . get ( ) <EOL> self . app . status . log ( '<STR_LIT>' % ( guesses , cumTime , <EOL> round ( guesses / cumTime , <NUM_LIT:2> ) ) ) <EOL> def calcScale ( self , testCount ) : <EOL> import math <EOL> scale = int ( self . size / ( math . sqrt ( testCount ) + <NUM_LIT:1> ) ) <EOL> return scale <EOL> class DirectoryExam ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path , answer , itemClass ) : <EOL> self . path = path <EOL> self . answer = answer <EOL> self . itemClass = itemClass <EOL> def __iter__ ( self ) : <EOL> files = os . listdir ( self . path ) <EOL> for file in files : <EOL> fp = open ( os . path . join ( self . path , file ) , '<STR_LIT:rb>' ) <EOL> try : <EOL> item = self . itemClass . fromFile ( fp ) <EOL> finally : <EOL> fp . close ( ) <EOL> if item is None : <EOL> continue <EOL> yield ( item , self . answer ) <EOL> def __len__ ( self ) : <EOL> files = os . listdir ( self . path ) <EOL> return len ( files ) </s>
