Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import xlrd
- import xlwt
- import math
- import sys
- import Tkinter, tkFileDialog
- import tkMessageBox
- import tkSimpleDialog
- from Tkinter import *
- from itertools import repeat
- #import csv
- from random import randrange, sample, randint
- import unicodedata
- #HEAT SORTER DESIGNED BY HUMPHREY WITTINGTONSWORTH 2015
- if __name__ == "__main__":
- root = Tk()
- root.wm_title("Heat Sorter")
- root.config(background = "#FFFFFF")
- #BEFORE SORTING
- rawcompetitorlist = []
- wcaprofile = []
- #AFTER SORTING
- competitorlist = []
- wcaprofile2 = []
- nubletlist = []
- #DATA FILE NAMES
- global wcadatabase
- global competitionfile
- global namecolumn
- global heatcolumn
- global workbook
- global newsheet
- wcadatabase = "string"
- competitionfile = "string"
- #OTHER VARIABLES
- heats = 0
- dispersenewcomers = "string"
- currenttab = 0
- population = -4
- counter = 1
- namecolumn = 0
- heatcolumn = 2
- profilecolumn = 1
- workbook = xlwt.Workbook(encoding="utf-8")
- newsheet= workbook.add_sheet("New Sheet", cell_overwrite_ok=True)
- def DataLoader1():
- global competitionfile
- global counter
- global population
- competitionfile = tkFileDialog.askopenfilename(parent=root, title="Select Competition File")
- book = xlrd.open_workbook(competitionfile)
- sheet = book.sheet_by_index(counter)
- event = (sheet.cell(2,0)).value
- column = [sheet.row_values(i)[1] for i in range(sheet.nrows)]
- for kekx in column:
- population = population +1
- trueevent = 0
- if event == float(event):
- trueevent = (math.trunc(event))
- else:
- trueevent = event
- graphCanvas.create_text( 100, 40 , text = str(trueevent), font=("Arial", 30), tags="text")
- graphCanvas.create_text( 100, 70 , text = "Total Competitors in Round:", font=("Arial", 10), tags="text2")
- graphCanvas.create_text( 100, 90 , text = str(population), font=("Arial", 10), tags="text3")
- graphCanvas.create_text( 100, 10 , text = "WCA Competition Event:", font=("Arial", 10), tags="text4")
- def DataLoader2():
- global wcadatabase
- wcadatabase = tkFileDialog.askopenfilename(parent=root, title="Select WCA Ranks-Average File")
- def Process():
- global heats
- global dispersenewcomers
- global wcadatabase
- global competitionfile
- global sheet
- global counter
- global namecolumn
- global heatcolumn
- global population
- global profilecolumn
- heats = tkSimpleDialog.askinteger(title = "Heats", prompt = "How many heats?")
- dispersenewcomers = tkMessageBox.askquestion(title = "Newcomers", message = "Do you want to spread out newcomers?") #RETURNS "yes" OR "no"
- print counter
- #GETS COMPETITORS AND SORTS INTO WHO ISN'T NEW
- book = xlrd.open_workbook(competitionfile)
- sheet = book.sheet_by_index(counter)
- event = (sheet.cell(2,0)).value
- column = [sheet.row_values(i)[1] for i in range(sheet.nrows) ]
- column2 = [sheet.row_values(i)[3] for i in range(sheet.nrows) ]
- rawcompetitorlist = [] #contains names
- for kek in column:
- rawcompetitorlist.append(kek)
- profilelist = []
- for kek2 in column2:
- if len(kek2) > 1:
- profilelist.append(kek2)
- else:
- profilelist.append("new")
- competitorlist = []
- newcomerlist = []
- newprofilelist = []
- for kek3 in range(4 , len(rawcompetitorlist)):
- if profilelist[kek3] == "new":
- newcomerlist.append(str(unicodedata.normalize("NFKD",(rawcompetitorlist[kek3])).encode("ascii","ignore")))
- else:
- competitorlist.append(rawcompetitorlist[kek3])
- for kek22 in range(4, len(profilelist)):
- if profilelist[kek22] == "new":
- pass
- else:
- newprofilelist.append(profilelist[kek22])
- #newprofilelist contains profiles of competitors
- #competitorlist contains competitor names
- #newcomerlist contains people without profiles
- #MATCHES THEM WITH THEIR RECORDS
- book2 = xlrd.open_workbook(wcadatabase)
- sheet2 = book2.sheet_by_index(0) #WCA DATA
- column3 = [sheet2.row_values(i)[0] for i in range(sheet2.nrows) ] #PROFILE CODE
- column4 = [sheet2.row_values(i)[1] for i in range(sheet2.nrows) ] #EVENT
- column5 = [sheet2.row_values(i)[4] for i in range(sheet2.nrows) ] #RANK
- eventstart = 0
- eventlength = 0
- firsttime = []
- previous = []
- previousrank = []
- for kek4 in column4:
- if kek4 == event:
- eventlength = eventlength + 1
- for kek5 in column4:
- if kek5 != event:
- eventstart = eventstart + 1
- elif kek5 == event:
- break
- for kek6 in newprofilelist:
- for kek7 in range(eventstart, (eventstart+eventlength)):
- if str(sheet2.cell(kek7,0).value) == str(kek6):
- previous.append(str(kek6))
- previousrank.append((math.trunc(sheet2.cell(kek7,3).value)))
- for doublekek in newprofilelist:
- firsttime.append(doublekek)
- for kek8 in previous:
- if kek8 in firsttime:
- firsttime.remove(kek8)
- #FIRSTTIME CONTAINS THE PROFILE CODES OF FIRST TIME PEOPLE IN EVENTS
- #PREVIOUS contains the profile codes of people who have competed in the event previously
- #Previous rank contains the rank of the people in previous in the same order
- #FIND NEWCOMER NAMES MATCHED WITH THEIR PROFILES
- firsttimenames = []
- for kek11 in range(0, len(competitorlist)):
- for kek12 in firsttime:
- if str(newprofilelist[kek11]) == str(kek12):
- firsttimenames.append(competitorlist[kek11])
- #GETS NAMES OF PREVIOUSLY COMPETED PEOPLE
- previousnames = []
- for kek10 in range(0,len(newprofilelist)):
- for kek122 in previous:
- if str(kek122) == str(newprofilelist[kek10]):
- previousnames.append(str(competitorlist[kek10]))
- break
- #SORTS NAMES BY RANK
- tupleranks = zip(previousnames, previous, previousrank)
- sortedtuple = sorted( tupleranks, key=lambda tup: tup[2] )
- rankednames = [seq[0] for seq in sortedtuple]
- rankedcodes = [seq[1] for seq in sortedtuple]
- rankedranks = [seq[2] for seq in sortedtuple]
- #ALL ranked variables have been sorted according to rank
- #FOR WRITING TO THE EXCEL SHEET TO BE EXPORTED
- global workbook
- global newsheet
- rearrangednames = []
- rearrangedcodes = []
- peopleperheat = math.trunc(population/heats)
- newsheet.write( 0, namecolumn, "Event:" )
- newsheet.write( 0, profilecolumn, str(event))
- newsheet.write( 0, heatcolumn, "Heat:" )
- if dispersenewcomers == "no":
- for kek14 in range(0,len(firsttimenames)):
- rankednames.append(str(firsttimenames[kek14]))
- rankedcodes.append(str(firsttime[kek14]))
- for kek13 in range(0, len(newcomerlist)):
- rankednames.append(newcomerlist[kek13])
- rankedcodes.append("N/A")
- #print len(firsttimenames)
- #print len(newcomerlist)
- #print (population)
- #print len(rankedcodes)
- for allkekz in range(2, (population+2)):
- newsheet.write( allkekz, namecolumn, str(rankednames[(allkekz-2)]))
- newsheet.write( allkekz, profilecolumn, str(rankedcodes[(allkekz-2)]))
- heatcheck = 0
- heatcount = 1
- for omgshit in range(2, (population + 2)):
- if heatcheck != peopleperheat:
- newsheet.write( omgshit, heatcolumn, heatcount)
- heatcheck = heatcheck + 1
- else:
- heatcount = heatcount + 1
- heatcheck = heatcheck - peopleperheat
- newsheet.write( omgshit, heatcolumn, heatcount)
- else:
- for asdf in rankednames:
- rearrangednames.append(asdf)
- for asdf2 in rankedcodes:
- rearrangedcodes.append(asdf2)
- for shit in firsttimenames:
- newcomerlist.append(shit)
- for randomthings in newcomerlist:
- rearrangednames.insert(randint(0,len(rankednames)),randomthings)
- for allkekz2 in range(2, (population+2)):
- newsheet.write( allkekz2, namecolumn, str(rearrangednames[(allkekz2-2)]))
- heatcheck2 = 0
- heatcount2 = 1
- for omgshit2 in range(2, (population + 2)):
- if heatcheck2 != peopleperheat:
- newsheet.write( omgshit2, heatcolumn, heatcount2)
- heatcheck2 = heatcheck2 + 1
- else:
- heatcount2 = heatcount2 + 1
- heatcheck2 = heatcheck2 - peopleperheat
- newsheet.write( omgshit2, heatcolumn, heatcount2)
- #UPDATES CANVAS FOR NEXT EVENT
- graphCanvas.delete("all")
- counter = counter + 1
- population = -4
- heatcolumn = heatcolumn + 5
- namecolumn = namecolumn + 5
- profilecolumn = profilecolumn +5
- sheet = book.sheet_by_index(counter)
- event = (sheet.cell(2,0)).value
- column = [sheet.row_values(i)[1] for i in range(sheet.nrows) ]
- column2 = [sheet.row_values(i)[3] for i in range(sheet.nrows) ]
- for kekx22 in column:
- population = population +1
- trueevent = 0
- if event == float(event):
- trueevent = (math.trunc(event))
- else:
- trueevent = event
- graphCanvas.create_text( 100, 40 , text = str(trueevent), font=("Arial", 30), tags="text")
- graphCanvas.create_text( 100, 70 , text = "Total Competitors in Round:", font=("Arial", 10), tags="text2")
- graphCanvas.create_text( 100, 90 , text = str(population), font=("Arial", 10), tags="text3")
- graphCanvas.create_text( 100, 10 , text = "WCA Competition Event:", font=("Arial", 10), tags="text4")
- def Export():
- global woorkbook
- workbook.save("Processed Heats.xls")
- #FRAMES:
- kekframe = Frame(root, width = 600, height = 400)
- kekframe.grid(row=0, column = 0, padx = 10, pady = 2)
- innerframe = Frame(kekframe, width = 200, height = 100)
- innerframe.grid(row = 0, column = 0, padx = 10, pady= 2)
- graphCanvas = Canvas(innerframe, width = 200, height = 100)
- graphCanvas.grid(row = 0, column = 0)
- btnFrame = Frame(kekframe, width=200, height = 200)
- btnFrame.grid(row=1, column=0, padx=10, pady=2)
- #BUTTONS:
- redBtn = Button(btnFrame, text="Select Comp. File", command=DataLoader1)
- redBtn.grid(row=0, column=0, padx=10, pady=2)
- StartBtn = Button(btnFrame, text="Select WCA File", command=DataLoader2)
- StartBtn.grid(row=1, column=0, padx=10, pady=2)
- StopBtn = Button(btnFrame, text="Process Heats", command=Process)
- StopBtn.grid(row=0, column=1, padx=10, pady=2)
- nextBtn = Button(btnFrame, text="Export Heats", command=Export)
- nextBtn.grid(row=1, column=1, padx=10, pady=2)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment