Advertisement
Guest User

elo.py

a guest
Jun 5th, 2018
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. //Program by: Jarrod Servilla
  2. //June 3rd, 2018
  3.  
  4. def calc_e(rate_a: int, rate_b: int) -> float:
  5. return 1 / (1 + 10 ** ( (rate_b - rate_a) / 400) )
  6.  
  7. def calc_rating(old_rating: int, e: float, is_win: bool) -> int:
  8. s = 0
  9. if is_win:
  10. s = 1
  11. return int(old_rating + 32 * (s - e))
  12.  
  13. def import_ratings() -> dict:
  14. file = open("ratings.txt", "r")
  15. d = {}
  16. for line in file:
  17. if line != '':
  18. L = line.split(" ")
  19. d[L[0]] = int(L[1][:4])
  20. file.close()
  21. return(d)
  22.  
  23. def interpret(d: dict, tournament: str) -> dict:
  24. file = open(tournament, "r")
  25. for line in file:
  26. if line != '' :
  27. L = line.split(" ")
  28. is_win = False
  29.  
  30. if L[2] == 'T\n' or L[2] == 'T':
  31. is_win = True
  32.  
  33. old_a = d.get(L[0], 1700)
  34. old_b = d.get(L[1], 1700)
  35.  
  36. d[L[0]] = calc_rating(old_a, calc_e(old_a, old_b), is_win)
  37. d[L[1]] = calc_rating(old_b, calc_e(old_b, old_a), not is_win)
  38.  
  39. file.close()
  40.  
  41. return d
  42.  
  43. def save_to_file(file_name: str, d: dict):
  44. file = open(file_name, 'w')
  45. for (key, value) in d.items():
  46. file.write(key + ' ' + str(value) + '\n')
  47. file.close()
  48.  
  49. def update(tournament: str):
  50. d = import_ratings()
  51. d = interpret(d, tournament)
  52.  
  53. save_to_file('ratings.txt', d)
  54. save_to_file(tournament, d)
  55.  
  56. def record_elos(file_name: str, d: dict):
  57. file = open(file_name, 'r')
  58. for line in file:
  59. if line != '':
  60. L = line.split(" ")
  61.  
  62. if L[0] not in d:
  63. d[L[0]] = [int(L[1][:4])]
  64. else:
  65. d[L[0]].append(int(L[1][:4]))
  66. file.close()
  67.  
  68. file2 = open('graph.txt', 'w')
  69. for (key, value) in d.items():
  70. file2.write(key + ' ' + str(value) + '\n')
  71. file2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement