campos20

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

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