campos20

WCA avg letter count by country

Feb 3rd, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. # Alexandre Campos, 02/03/2019
  2.  
  3. # program made for the WCA Statistics, requested by Marius Rombout van Riemsdijk
  4.  
  5. # place this program.py alongside with the tsv (unziped) export from
  6. # https://www.worldcubeassociation.org/results/misc/WCA_export.tsv.zip
  7. # and run it with
  8. # python program.py
  9.  
  10. import csv
  11.  
  12. def show_results(labels, results_list, list_of_lists, limit = float("inf"), direction = "asc", string_format = "%s"):
  13.     """'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."""
  14.  
  15.     pos = 1
  16.     prev = None
  17.    
  18.     out = "Pos"
  19.     for x in labels:
  20.         out += "\t"+x
  21.     print out
  22.    
  23.     temp = []
  24.     if direction == "asc":
  25.         temp = sorted(zip(results_list, range(len(results_list))))
  26.     elif direction == "des":
  27.         temp = sorted(zip(results_list, range(len(results_list))))[::-1]
  28.     else:
  29.         raise ValueError, "Invalid direction: %s"%direction
  30.  
  31.     for a, b in temp:
  32.         out = ""
  33.         if a != prev:
  34.             if pos > limit:
  35.                 break
  36.             out+="%s)\t"%pos
  37.         else:
  38.             out += "---\t"
  39.         out += format(a, string_format)+"\t"
  40.         for x in list_of_lists:
  41.             out += str(x[b])+"\t"
  42.         print out.strip()
  43.         prev = a
  44.         pos += 1
  45.  
  46. def just_letters(s):
  47.     out = ""
  48.     for x in s:
  49.         if x.isalpha(): out += x
  50.     return out
  51.  
  52. def avg(l):
  53.     if len(l) == 0: return 0.
  54.     return 1.0*sum(l)/len(l)
  55.  
  56. def main():
  57.     country_list = []
  58.     count = []
  59.    
  60.     header = True
  61.  
  62.     with open("WCA_export_Persons.tsv") as tsvfile:
  63.  
  64.         tsvreader = csv.reader(tsvfile, delimiter="\t")
  65.         for line in tsvreader:
  66.        
  67.             if header:
  68.                 header = False
  69.                 continue
  70.        
  71.             name = line[2].decode("utf-8")
  72.             if "(" in name:
  73.                 name = name[:name.index("(")]
  74.                
  75.             # remove spaces . and -
  76.             name = just_letters(name)
  77.  
  78.             name_count = sum(map(len, name.split()))
  79.             country = line[3]
  80.            
  81.             i = -1
  82.             try:
  83.                 i = country_list.index(country)
  84.             except:
  85.                 country_list.append(country)
  86.                 count.append([])
  87.             count[i].append(name_count)
  88.        
  89.     avg_list = map(avg, count)
  90.     no_of_competitors = map(len, count)
  91.     labels = ["Avg", "Country", "# of competitors"]
  92.     list_of_lists = [country_list, no_of_competitors]
  93.    
  94.     assert len(labels) == len(list_of_lists)+1
  95.    
  96.     limit = 1000
  97.     show_results(labels, avg_list, list_of_lists, limit, "des", ".2f")
  98.            
  99. main()
Advertisement
Add Comment
Please, Sign In to add comment