Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # acampos@worldcubeassociation.org
- # program made after a request from Felipe Cardim
- # place this program alongside with the tsv (unziped) export from
- # https://www.worldcubeassociation.org/results/misc/WCA_export.tsv.zip
- # and run it with
- # python program.py
- # it takes about 24 min on my PC
- import csv
- from datetime import date
- from math import sin, cos, sqrt, atan2, radians
- class Location:
- def __init__(self, latitude, longitude):
- self.latitude = latitude
- self.longitude = longitude
- def get_latitude(self):
- return self.latitude
- def get_longitude(self):
- return self.longitude
- class Competition:
- def __init__(self, name, date, location):
- self.name = name
- self.date = date
- self.location = location
- def get_name(self):
- return self.name
- def get_date(self):
- return self.date
- def get_location(self):
- return self.location
- def __lt__(self, other):
- return self.date < other.date
- def __str__(self):
- return self.name
- def dist(loc1, loc2):
- # https://stackoverflow.com/questions/19412462/getting-distance-between-two-points-based-on-latitude-longitude
- R = 6373.0
- lat1, lon1 = loc1.get_latitude(), loc1.get_longitude()
- lat2, lon2 = loc2.get_latitude(), loc2.get_longitude()
- dlon = lon2 - lon1
- dlat = lat2 - lat1
- a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
- c = 2 * atan2(sqrt(a), sqrt(1 - a))
- return R*c
- def dis_comp(competition1, competition2):
- return dist(competition1.get_location(), competition2.get_location())
- def new_competition(name):
- with open("WCA_export_Competitions.tsv") as tsvfile:
- tsvreader = csv.reader(tsvfile, delimiter="\t")
- for line in tsvreader:
- if name == line[0]:
- year = int(line[5])
- month = int(line[6])
- day = int(line[7])
- this_date = date(year, month, day)
- latitude = radians(float(line[18])/pow(10,6))
- longitude = radians(float(line[19])/pow(10,6))
- location = Location(latitude, longitude)
- return Competition(name, date, location)
- raise ValueError('Name for competition not found.')
- def show_results(labels, results_list, list_of_lists, limit = float("inf"), direction = "asc"):
- """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]
- for a, b in temp:
- out = ""
- if a != prev:
- if pos > limit:
- break
- out+="%s)\t"%pos
- else:
- out += "---\t"
- out += str(a)+"\t"
- for x in list_of_lists:
- out += str(x[b])+"\t"
- print out.strip()
- prev = a
- pos += 1
- def main():
- with open("WCA_export_Results.tsv") as tsvfile:
- tsvreader = csv.reader(tsvfile, delimiter="\t")
- ids = []
- names = [] # competitors names
- competitions = []
- prev = None
- competition = None
- header = True
- for line in tsvreader:
- if header:
- header = False
- continue
- name = line[0] # competition name
- if name != prev:
- competition = new_competition(name)
- prev = name
- this_id = line[7]
- i = -1
- try:
- i = ids.index(this_id)
- except:
- this_name = line[6]
- ids.append(this_id)
- names.append(this_name)
- competitions.append([competition])
- continue
- flag = True
- for x in competitions[i]: # perhaps I could define the method __eq__ for the class and skip a few lines here
- if competition.get_name() == x.get_name():
- flag = False
- break
- if flag:
- competitions[i].append(competition)
- for i in range(len(ids)):
- competitions[i] = sorted(competitions[i])
- dist_list = []
- name_list = []
- comp1_list = []
- comp2_list = []
- for i in range(len(ids)):
- if len(competitions[i]) < 2:
- continue
- comp1 = competitions[i][0]
- comp2 = competitions[i][1]
- dist_list.append(dis_comp(comp1, comp2))
- name_list.append(names[i])
- comp1_list.append(str(comp1))
- comp2_list.append(str(comp2))
- labels = ["Dist (km)", "Name", "Competition 1", "Competition 2"]
- list_of_lists = [name_list, comp1_list, comp2_list]
- limit = 1000
- show_results(labels, dist_list, list_of_lists, limit, "des")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement