Advertisement
campos20

WCA avg letter count by country

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