# Python prog to combine butler from several sets into a total table # # Paul Bethe # # #Copyright (c) 2007, Paul Bethe # All rights reserved. # # Redistribution and use in source and binary forms, # with or without modification, are permitted provided # that the following conditions are met: # # 1. Redistributions of source code must retain the # above copyright notice, this list of conditions # and the following disclaimer. # # 2. Redistributions in binary form must reproduce the # above copyright notice, this list of conditions # and the following disclaimer in the documentation # and/or other materials provided with the # distribution. # # 3. Neither the name of Paul Bethe or the # names of its contributors may be used to endorse # or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL # THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. import sys def cmpIncTuple (left, right): if left[1] > right[1]: return -1 if left[1] < right[1]: return 1 else: return 0 def blistCmp (left, right): fb1 = int(left.split()[0]) fb2 = int(right.split()[0]) if fb1 < fb2: return -1 if fb2 < fb1: return 1 return 0 def formatR (scores, brng): if scores.has_key(brng): return "%-10.3f" % scores[brng] else: return "%-10s" % "" def formatC (scores, brng): if scores.has_key(brng): return "%.3f" % scores[brng] else: return "" def printText(pairs, tots, boardlist): print "%-5s%-35s%-10s" % ("Rank", "Pair", "Avg."), print "".join ([ "%-10s" % brng for brng in boardlist ]) for rank, (pname, this_tot) in enumerate(tots): #this_tot = tot / len(pairs[pname].values()) print "%-5d%-35s%-10.4f" % ((1+rank), pname, this_tot), print "".join ([ formatR(pairs[pname], brng) for brng in boardlist ]) def printCsv(pairs, tots, boardlist): print "%s,%s,%s," % ("Rank", "Pair", "Avg."), print ",".join ([ "%s" % brng for brng in boardlist ]) for rank, (pname, this_tot) in enumerate(tots): #this_tot = tot / len(pairs[pname].values()) print "%d,%s,%.4f," % ((1+rank), pname, this_tot), print ",".join ([ formatC(pairs[pname], brng) for brng in boardlist ]) def main(): pairs = {} tots = {} boardlist = [] index = 1 if sys.argv[1] == "-c": index += 1 for filename in sys.argv[index:]: fp = open (filename) brange = " ".join (fp.readline().strip().split()[1:]) if not brange in boardlist: boardlist.append(brange) for line in fp.readlines(): tok = line.split() pname = " ".join(tok[:-1]) val = float(tok[-1]) if not pairs.has_key(pname): pairs[pname] = {} tots[pname] = 0.0 pairs[pname][brange] = val tots[pname] += val fp.close() tots = map (lambda x: (x[0], x[1]/len(pairs[x[0]])), tots.items()) tots.sort(cmpIncTuple) #print tots boardlist.sort(blistCmp) if index == 1: printText (pairs, tots, boardlist) else: printCsv (pairs, tots, boardlist) if __name__ == "__main__": main()