-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
75 lines (63 loc) · 2.38 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 27 15:07:46 2020
@author: Davey Struijk
"""
import csv
import os
import random
from typing import Tuple
CSV_FILENAME = "keldertoplijst2022.csv"
fieldnames = ["Artiest", "Nummer", "Youtube-link", "Rating"]
def expect(rating: float, other_rating: float) -> float:
"""The "E" function in Elo. It calculates the expected score of the
first rating by the second rating.
"""
# http://www.chess-mind.com/en/elo-system
diff = other_rating - rating
return 1.0 / (1 + 10 ** (diff / 400))
def calc_elo(rating: float,
other_rating: float, K: int = 10) -> Tuple[float, float]:
return (rating + K * (1 - expect(rating, other_rating)),
other_rating + K * (0 - expect(other_rating, rating)))
# Load from CSV
with open(CSV_FILENAME) as csvfile:
reader = csv.DictReader(csvfile, fieldnames=fieldnames, restval="1000.0")
next(reader) # skip header
tracks = [row for row in reader]
# Comparison loop (Press Ctrl+C to exit)
while True:
try:
w = os.get_terminal_size().columns
a, b = random.sample(range(len(tracks)), 2)
print("=" * w)
print("(1)".center(w))
print("{Artiest} - {Nummer}".format(**tracks[a]).center(w))
print("{Youtube-link}".format(**tracks[a]).center(w))
print("")
print("(2)".center(w))
print("{Artiest} - {Nummer}".format(**tracks[b]).center(w))
print("{Youtube-link}".format(**tracks[b]).center(w))
print("=" * w)
choice = input("> ")
if choice.lower() == "1":
tracks[a]["Rating"], tracks[b]["Rating"] = calc_elo(
float(tracks[a]["Rating"]), float(tracks[b]["Rating"]))
elif choice.lower() == "2":
tracks[b]["Rating"], tracks[a]["Rating"] = calc_elo(
float(tracks[b]["Rating"]), float(tracks[a]["Rating"]))
else:
print("Invalid choice")
print("")
except KeyboardInterrupt:
break
# Save to CSV
with open(CSV_FILENAME, "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(
sorted(tracks, key=lambda t: float(t["Rating"]), reverse=True))
# Print ratings
for track in sorted(tracks, key=lambda t: float(t["Rating"]), reverse=True):
print("[{Rating}] {Artiest} - {Nummer}".format(**track))