Advertisement
campos20

WCA 4BLD > 12 min to 5BLD < 12 min

Feb 11th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.55 KB | None | 0 0
  1. # Software by Alexandre Campos
  2. # camposalexandreh@gmail.com
  3. # 12/30/2018
  4. # Software made for WDC purpose. It compares when a competitor got a 4BLD result > 12 min to when s/he got a 5BLD < 12 min.
  5.  
  6. # Place this program.py alongside with the tsv export of WCA db
  7. # https://www.worldcubeassociation.org/results/misc/export.html
  8. # and execute it with
  9. # python program.py
  10.  
  11. import csv
  12. from datetime import date
  13.  
  14. def get_competition_date(competition_id):
  15.  
  16.     with open('WCA_export_Competitions.tsv','rb') as tsvin:
  17.         tsvin = csv.reader(tsvin, delimiter='\t')
  18.        
  19.         for line in tsvin:
  20.             if competition_id == line[0]:
  21.  
  22.                 year = int(line[5])
  23.                 month = int(line[6])
  24.                 day = int(line[7])
  25.  
  26.                 return date(year, month, day)
  27.    
  28.         return None
  29.  
  30. def show_results(labels, results_list, list_of_lists, limit = float("inf"), direction = "asc"):
  31.     """Labels is intuitive. Results_list is the main order list and list_of_lists are other lists that are gonna be ordered as well (eg.: country, name, etc). You can set limit if you wish. Direction = 'asc' if you wanna the lower results first; direction = 'des' if you wanna bigger results first."""
  32.  
  33.     pos = 1
  34.  
  35.     prev = None
  36.    
  37.     out = "Pos"
  38.     for x in labels:
  39.         out += "\t"+x
  40.     print out
  41.    
  42.     temp = []
  43.     if direction == "asc":
  44.         temp = sorted(zip(results_list, range(len(results_list))))
  45.     elif direction == "des":
  46.         temp = sorted(zip(results_list, range(len(results_list))))[::-1]
  47.  
  48.     for a, b in temp:
  49.         out = ""
  50.         if a != prev:
  51.             if pos > limit:
  52.                 break
  53.             out+="%s)\t"%pos
  54.         else:
  55.             out += "---\t"
  56.         out += str(a)+"\t"
  57.         for x in list_of_lists:
  58.             out += str(x[b])+"\t"
  59.         print out.strip()
  60.         prev = a
  61.         pos += 1
  62.  
  63. def time_format(time):
  64.     m = int(time/60)
  65.     time -= m*60
  66.    
  67.     s = int(time)
  68.     time -= s
  69.    
  70.     d = int(time*100)
  71.    
  72.     return "%s:%s.%s"%(str(m).zfill(2), str(s).zfill(2), str(d).zfill(2))
  73.  
  74. def get_results(ev):
  75.  
  76.     results = []
  77.     id_list = []
  78.     competition_id = []
  79.     competition_date = []
  80.  
  81.     with open('WCA_export_Results.tsv','rb') as tsvin:
  82.         tsvin = csv.reader(tsvin, delimiter='\t')
  83.        
  84.         prev_competition = None
  85.         date = None
  86.        
  87.         for line in tsvin:
  88.  
  89.             event = line[1]
  90.            
  91.             if event == ev:
  92.  
  93.                 best = line[4]
  94.                 if best == -1: # competitor got at least one right
  95.                     continue
  96.                
  97.                 person_id = line[7]
  98.                 i = -1
  99.                 try:
  100.                     i = id_list.index(person_id)
  101.                 except:
  102.                     id_list.append(person_id)
  103.                     results.append([])
  104.                     competition_id.append([])
  105.                     competition_date.append([])
  106.                
  107.                 competition = line[0]
  108.                
  109.                 # this saves time since looking for competition date is expensive
  110.                 if competition != prev_competition:
  111.                     date = get_competition_date(competition)
  112.                 prev_competition = competition
  113.                
  114.                 for x in line[10:13]:
  115.                     if x not in ["0", "-1", "-2"]:
  116.                         results[i].append(float(x)/100)
  117.                         competition_date[i].append(date)
  118.                         competition_id[i].append(competition)
  119.  
  120.     return (id_list, results, competition_id, competition_date)
  121.  
  122. def main():
  123.  
  124.     LIMIT = 1000000
  125.  
  126.     id_list_4, results_4, competition_id_4, bld_date_4 = get_results("444bf")
  127.     id_list_5, results_5, competition_id_5, bld_date_5 = get_results("555bf")
  128.    
  129.     days = []
  130.     id_list = []
  131.     result4 = []
  132.     comp4 = []
  133.     date4 = []
  134.     result5 = []
  135.     comp5 = []
  136.     date5 = []
  137.    
  138.     prev_comp_4 = None
  139.     prev_comp_5 = None
  140.     prev_id = None
  141.    
  142.     for i in range(len(id_list_4)):
  143.  
  144.         j = None
  145.         try:
  146.             j = id_list_5.index(id_list_4[i])
  147.         except:
  148.             continue
  149.        
  150.         for k in range(len(results_4[i])):
  151.             if results_4[i][k] < 12*60:
  152.                 continue
  153.             for l in range(len(results_5[j])):
  154.                 if results_5[j][l] > 12*60:
  155.                     continue
  156.  
  157.                 delta = bld_date_5[j][l] - bld_date_4[i][k]
  158.                 d = delta.days
  159.                
  160.                 this_comp_4 = competition_id_4[i][k]
  161.                 this_comp_5 = competition_id_5[j][l]
  162.                 this_id = id_list_5[j]
  163.                
  164.                 # people who got multiple results at same comps
  165.                 if this_comp_4 == prev_comp_4 and this_comp_5 == prev_comp_5 and this_id == prev_id:
  166.                     continue
  167.                
  168.                 days.append(d)
  169.                 id_list.append(this_id)
  170.                 result4.append(time_format(results_4[i][k]))
  171.                 comp4.append(this_comp_4)
  172.                 date4.append(bld_date_4[i][k])
  173.                 result5.append(time_format(results_5[j][l]))
  174.                 comp5.append(this_comp_5)
  175.                 date5.append(bld_date_5[j][l])
  176.                
  177.                 prev_comp_4 = this_comp_4
  178.                 prev_comp_5 = this_comp_5
  179.                 prev_id = this_id
  180.    
  181.     list_of_lists = [id_list, result4, comp4, date4, result5, comp5, date5]
  182.     labels = ["Days", "ID", "Result4", "Comp of 4", "Date of 4", "Result5", "Comp of 5", "Date of 5"]
  183.    
  184.     show_results(labels, days, list_of_lists, LIMIT)
  185.                
  186. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement