Guest User

asdfOu

a guest
Jan 6th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.46 KB | None | 0 0
  1. import os
  2. import xlrd
  3. import xlwt
  4. import math
  5. import sys
  6. import Tkinter, tkFileDialog
  7. import tkMessageBox
  8. import tkSimpleDialog
  9. from Tkinter import *
  10. from itertools import repeat
  11. #import csv
  12. from random import randrange, sample, randint
  13. import unicodedata
  14.  
  15.  
  16.  
  17.  
  18.  
  19. #HEAT SORTER DESIGNED BY HUMPHREY WITTINGTONSWORTH 2015
  20.  
  21. if __name__ == "__main__":
  22.  
  23. root = Tk()
  24. root.wm_title("Heat Sorter")
  25. root.config(background = "#FFFFFF")
  26.  
  27.  
  28.  
  29. #BEFORE SORTING
  30. rawcompetitorlist = []
  31. wcaprofile = []
  32.  
  33.  
  34. #AFTER SORTING
  35. competitorlist = []
  36. wcaprofile2 = []
  37.  
  38. nubletlist = []
  39.  
  40.  
  41.  
  42. #DATA FILE NAMES
  43. global wcadatabase
  44. global competitionfile
  45. global namecolumn
  46. global heatcolumn
  47. global workbook
  48. global newsheet
  49.  
  50. wcadatabase = "string"
  51. competitionfile = "string"
  52.  
  53. #OTHER VARIABLES
  54. heats = 0
  55. dispersenewcomers = "string"
  56. currenttab = 0
  57. population = -4
  58. counter = 1
  59. namecolumn = 0
  60. heatcolumn = 2
  61. profilecolumn = 1
  62.  
  63. workbook = xlwt.Workbook(encoding="utf-8")
  64. newsheet= workbook.add_sheet("New Sheet", cell_overwrite_ok=True)
  65.  
  66.  
  67.  
  68. def DataLoader1():
  69. global competitionfile
  70. global counter
  71. global population
  72. competitionfile = tkFileDialog.askopenfilename(parent=root, title="Select Competition File")
  73. book = xlrd.open_workbook(competitionfile)
  74. sheet = book.sheet_by_index(counter)
  75. event = (sheet.cell(2,0)).value
  76. column = [sheet.row_values(i)[1] for i in range(sheet.nrows)]
  77.  
  78.  
  79.  
  80. for kekx in column:
  81. population = population +1
  82.  
  83. trueevent = 0
  84.  
  85. if event == float(event):
  86. trueevent = (math.trunc(event))
  87. else:
  88. trueevent = event
  89.  
  90. graphCanvas.create_text( 100, 40 , text = str(trueevent), font=("Arial", 30), tags="text")
  91. graphCanvas.create_text( 100, 70 , text = "Total Competitors in Round:", font=("Arial", 10), tags="text2")
  92. graphCanvas.create_text( 100, 90 , text = str(population), font=("Arial", 10), tags="text3")
  93. graphCanvas.create_text( 100, 10 , text = "WCA Competition Event:", font=("Arial", 10), tags="text4")
  94.  
  95.  
  96.  
  97. def DataLoader2():
  98. global wcadatabase
  99. wcadatabase = tkFileDialog.askopenfilename(parent=root, title="Select WCA Ranks-Average File")
  100.  
  101.  
  102.  
  103. def Process():
  104. global heats
  105. global dispersenewcomers
  106. global wcadatabase
  107. global competitionfile
  108. global sheet
  109. global counter
  110. global namecolumn
  111. global heatcolumn
  112. global population
  113. global profilecolumn
  114.  
  115.  
  116. heats = tkSimpleDialog.askinteger(title = "Heats", prompt = "How many heats?")
  117. dispersenewcomers = tkMessageBox.askquestion(title = "Newcomers", message = "Do you want to spread out newcomers?") #RETURNS "yes" OR "no"
  118. print counter
  119. #GETS COMPETITORS AND SORTS INTO WHO ISN'T NEW
  120. book = xlrd.open_workbook(competitionfile)
  121. sheet = book.sheet_by_index(counter)
  122. event = (sheet.cell(2,0)).value
  123. column = [sheet.row_values(i)[1] for i in range(sheet.nrows) ]
  124. column2 = [sheet.row_values(i)[3] for i in range(sheet.nrows) ]
  125.  
  126. rawcompetitorlist = [] #contains names
  127. for kek in column:
  128. rawcompetitorlist.append(kek)
  129.  
  130. profilelist = []
  131. for kek2 in column2:
  132. if len(kek2) > 1:
  133. profilelist.append(kek2)
  134. else:
  135. profilelist.append("new")
  136.  
  137. competitorlist = []
  138. newcomerlist = []
  139. newprofilelist = []
  140.  
  141.  
  142. for kek3 in range(4 , len(rawcompetitorlist)):
  143. if profilelist[kek3] == "new":
  144. newcomerlist.append(str(unicodedata.normalize("NFKD",(rawcompetitorlist[kek3])).encode("ascii","ignore")))
  145. else:
  146. competitorlist.append(rawcompetitorlist[kek3])
  147.  
  148. for kek22 in range(4, len(profilelist)):
  149. if profilelist[kek22] == "new":
  150. pass
  151. else:
  152. newprofilelist.append(profilelist[kek22])
  153.  
  154.  
  155. #newprofilelist contains profiles of competitors
  156. #competitorlist contains competitor names
  157. #newcomerlist contains people without profiles
  158.  
  159.  
  160. #MATCHES THEM WITH THEIR RECORDS
  161.  
  162.  
  163. book2 = xlrd.open_workbook(wcadatabase)
  164. sheet2 = book2.sheet_by_index(0) #WCA DATA
  165. column3 = [sheet2.row_values(i)[0] for i in range(sheet2.nrows) ] #PROFILE CODE
  166. column4 = [sheet2.row_values(i)[1] for i in range(sheet2.nrows) ] #EVENT
  167. column5 = [sheet2.row_values(i)[4] for i in range(sheet2.nrows) ] #RANK
  168.  
  169. eventstart = 0
  170. eventlength = 0
  171.  
  172. firsttime = []
  173. previous = []
  174. previousrank = []
  175.  
  176. for kek4 in column4:
  177. if kek4 == event:
  178. eventlength = eventlength + 1
  179.  
  180.  
  181. for kek5 in column4:
  182. if kek5 != event:
  183. eventstart = eventstart + 1
  184. elif kek5 == event:
  185. break
  186.  
  187. for kek6 in newprofilelist:
  188. for kek7 in range(eventstart, (eventstart+eventlength)):
  189.  
  190. if str(sheet2.cell(kek7,0).value) == str(kek6):
  191.  
  192. previous.append(str(kek6))
  193. previousrank.append((math.trunc(sheet2.cell(kek7,3).value)))
  194.  
  195.  
  196. for doublekek in newprofilelist:
  197. firsttime.append(doublekek)
  198.  
  199. for kek8 in previous:
  200. if kek8 in firsttime:
  201. firsttime.remove(kek8)
  202.  
  203.  
  204.  
  205. #FIRSTTIME CONTAINS THE PROFILE CODES OF FIRST TIME PEOPLE IN EVENTS
  206. #PREVIOUS contains the profile codes of people who have competed in the event previously
  207. #Previous rank contains the rank of the people in previous in the same order
  208.  
  209. #FIND NEWCOMER NAMES MATCHED WITH THEIR PROFILES
  210.  
  211.  
  212. firsttimenames = []
  213.  
  214.  
  215.  
  216.  
  217. for kek11 in range(0, len(competitorlist)):
  218. for kek12 in firsttime:
  219. if str(newprofilelist[kek11]) == str(kek12):
  220. firsttimenames.append(competitorlist[kek11])
  221.  
  222.  
  223. #GETS NAMES OF PREVIOUSLY COMPETED PEOPLE
  224.  
  225. previousnames = []
  226.  
  227.  
  228. for kek10 in range(0,len(newprofilelist)):
  229. for kek122 in previous:
  230. if str(kek122) == str(newprofilelist[kek10]):
  231. previousnames.append(str(competitorlist[kek10]))
  232. break
  233.  
  234.  
  235. #SORTS NAMES BY RANK
  236.  
  237. tupleranks = zip(previousnames, previous, previousrank)
  238. sortedtuple = sorted( tupleranks, key=lambda tup: tup[2] )
  239.  
  240. rankednames = [seq[0] for seq in sortedtuple]
  241. rankedcodes = [seq[1] for seq in sortedtuple]
  242. rankedranks = [seq[2] for seq in sortedtuple]
  243.  
  244.  
  245.  
  246. #ALL ranked variables have been sorted according to rank
  247.  
  248.  
  249.  
  250.  
  251. #FOR WRITING TO THE EXCEL SHEET TO BE EXPORTED
  252.  
  253.  
  254.  
  255. global workbook
  256. global newsheet
  257.  
  258. rearrangednames = []
  259. rearrangedcodes = []
  260.  
  261.  
  262. peopleperheat = math.trunc(population/heats)
  263.  
  264.  
  265. newsheet.write( 0, namecolumn, "Event:" )
  266. newsheet.write( 0, profilecolumn, str(event))
  267. newsheet.write( 0, heatcolumn, "Heat:" )
  268.  
  269. if dispersenewcomers == "no":
  270. for kek14 in range(0,len(firsttimenames)):
  271. rankednames.append(str(firsttimenames[kek14]))
  272. rankedcodes.append(str(firsttime[kek14]))
  273. for kek13 in range(0, len(newcomerlist)):
  274. rankednames.append(newcomerlist[kek13])
  275. rankedcodes.append("N/A")
  276.  
  277.  
  278.  
  279. #print len(firsttimenames)
  280. #print len(newcomerlist)
  281. #print (population)
  282. #print len(rankedcodes)
  283.  
  284. for allkekz in range(2, (population+2)):
  285. newsheet.write( allkekz, namecolumn, str(rankednames[(allkekz-2)]))
  286. newsheet.write( allkekz, profilecolumn, str(rankedcodes[(allkekz-2)]))
  287. heatcheck = 0
  288. heatcount = 1
  289. for omgshit in range(2, (population + 2)):
  290. if heatcheck != peopleperheat:
  291. newsheet.write( omgshit, heatcolumn, heatcount)
  292. heatcheck = heatcheck + 1
  293. else:
  294. heatcount = heatcount + 1
  295. heatcheck = heatcheck - peopleperheat
  296. newsheet.write( omgshit, heatcolumn, heatcount)
  297.  
  298.  
  299.  
  300. else:
  301. for asdf in rankednames:
  302. rearrangednames.append(asdf)
  303. for asdf2 in rankedcodes:
  304. rearrangedcodes.append(asdf2)
  305. for shit in firsttimenames:
  306. newcomerlist.append(shit)
  307. for randomthings in newcomerlist:
  308. rearrangednames.insert(randint(0,len(rankednames)),randomthings)
  309.  
  310.  
  311. for allkekz2 in range(2, (population+2)):
  312. newsheet.write( allkekz2, namecolumn, str(rearrangednames[(allkekz2-2)]))
  313.  
  314.  
  315. heatcheck2 = 0
  316. heatcount2 = 1
  317. for omgshit2 in range(2, (population + 2)):
  318. if heatcheck2 != peopleperheat:
  319. newsheet.write( omgshit2, heatcolumn, heatcount2)
  320. heatcheck2 = heatcheck2 + 1
  321. else:
  322. heatcount2 = heatcount2 + 1
  323. heatcheck2 = heatcheck2 - peopleperheat
  324. newsheet.write( omgshit2, heatcolumn, heatcount2)
  325.  
  326.  
  327.  
  328.  
  329. #UPDATES CANVAS FOR NEXT EVENT
  330.  
  331. graphCanvas.delete("all")
  332.  
  333.  
  334.  
  335. counter = counter + 1
  336. population = -4
  337. heatcolumn = heatcolumn + 5
  338. namecolumn = namecolumn + 5
  339. profilecolumn = profilecolumn +5
  340.  
  341. sheet = book.sheet_by_index(counter)
  342. event = (sheet.cell(2,0)).value
  343. column = [sheet.row_values(i)[1] for i in range(sheet.nrows) ]
  344. column2 = [sheet.row_values(i)[3] for i in range(sheet.nrows) ]
  345.  
  346.  
  347. for kekx22 in column:
  348. population = population +1
  349.  
  350. trueevent = 0
  351.  
  352. if event == float(event):
  353. trueevent = (math.trunc(event))
  354. else:
  355. trueevent = event
  356.  
  357. graphCanvas.create_text( 100, 40 , text = str(trueevent), font=("Arial", 30), tags="text")
  358. graphCanvas.create_text( 100, 70 , text = "Total Competitors in Round:", font=("Arial", 10), tags="text2")
  359. graphCanvas.create_text( 100, 90 , text = str(population), font=("Arial", 10), tags="text3")
  360. graphCanvas.create_text( 100, 10 , text = "WCA Competition Event:", font=("Arial", 10), tags="text4")
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367. def Export():
  368.  
  369. global woorkbook
  370. workbook.save("Processed Heats.xls")
  371.  
  372.  
  373. #FRAMES:
  374.  
  375. kekframe = Frame(root, width = 600, height = 400)
  376. kekframe.grid(row=0, column = 0, padx = 10, pady = 2)
  377.  
  378. innerframe = Frame(kekframe, width = 200, height = 100)
  379. innerframe.grid(row = 0, column = 0, padx = 10, pady= 2)
  380.  
  381. graphCanvas = Canvas(innerframe, width = 200, height = 100)
  382. graphCanvas.grid(row = 0, column = 0)
  383.  
  384. btnFrame = Frame(kekframe, width=200, height = 200)
  385. btnFrame.grid(row=1, column=0, padx=10, pady=2)
  386.  
  387. #BUTTONS:
  388.  
  389. redBtn = Button(btnFrame, text="Select Comp. File", command=DataLoader1)
  390. redBtn.grid(row=0, column=0, padx=10, pady=2)
  391.  
  392. StartBtn = Button(btnFrame, text="Select WCA File", command=DataLoader2)
  393. StartBtn.grid(row=1, column=0, padx=10, pady=2)
  394.  
  395. StopBtn = Button(btnFrame, text="Process Heats", command=Process)
  396. StopBtn.grid(row=0, column=1, padx=10, pady=2)
  397.  
  398. nextBtn = Button(btnFrame, text="Export Heats", command=Export)
  399. nextBtn.grid(row=1, column=1, padx=10, pady=2)
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment