Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Alexandre Campos, 02/03/2019
- # acampos@worldcubeassociation.org
- # program made for the WCA Statistics, requested by Marius Rombout van Riemsdijk
- # place this program.py alongside with the tsv (unziped) export from
- # https://www.worldcubeassociation.org/results/misc/WCA_export.tsv.zip
- # and run it with
- # python program.py
- import csv
- def show_results(labels, results_list, list_of_lists, limit = float("inf"), direction = "asc", string_format = "%s"):
- """'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."""
- pos = 1
- prev = None
- out = "Pos"
- for x in labels:
- out += "\t"+x
- print out
- temp = []
- if direction == "asc":
- temp = sorted(zip(results_list, range(len(results_list))))
- elif direction == "des":
- temp = sorted(zip(results_list, range(len(results_list))))[::-1]
- else:
- raise ValueError, "Invalid direction: %s"%direction
- for a, b in temp:
- out = ""
- if a != prev:
- if pos > limit:
- break
- out+="%s)\t"%pos
- else:
- out += "---\t"
- out += format(a, string_format)+"\t"
- for x in list_of_lists:
- out += str(x[b])+"\t"
- print out.strip()
- prev = a
- pos += 1
- def just_letters(s):
- out = ""
- for x in s:
- if x.isalpha(): out += x
- return out
- def avg(l):
- if len(l) == 0: return 0.
- return 1.0*sum(l)/len(l)
- def main():
- country_list = []
- count = []
- header = True
- with open("WCA_export_Persons.tsv") as tsvfile:
- tsvreader = csv.reader(tsvfile, delimiter="\t")
- for line in tsvreader:
- if header:
- header = False
- continue
- name = line[2].decode("utf-8")
- if "(" in name:
- name = name[:name.index("(")]
- # remove spaces . and -
- name = just_letters(name)
- name_count = sum(map(len, name.split()))
- country = line[3]
- i = -1
- try:
- i = country_list.index(country)
- except:
- country_list.append(country)
- count.append([])
- count[i].append(name_count)
- avg_list = map(avg, count)
- no_of_competitors = map(len, count)
- labels = ["Avg", "Country", "# of competitors"]
- list_of_lists = [country_list, no_of_competitors]
- assert len(labels) == len(list_of_lists)+1
- limit = 1000
- show_results(labels, avg_list, list_of_lists, limit, "des", ".2f")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement