-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineup.py
97 lines (89 loc) · 3.3 KB
/
lineup.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
import itertools
import math
import random
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SPREADSHEET_ID = '1ZM7_0IXk5sfDY1YsTJSPVNJQWYV_H8gnjm7OwN3zAsc'
RANGE_NAME = 'Offense!B2:M13'
def get_credentials():
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
return creds
def get_players():
creds = get_credentials()
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print("No data found")
else:
players = {}
for row in values:
player = str(row.pop(0))
players[player] = []
for innings in row:
players[player].append(int(innings))
return players
def prune_players(players):
for player in list(players.keys()):
answer = str(input("Include {} [y/n]? ".format(player))).lower().strip()
if not answer[0] == 'y':
del players[player]
return players
def main():
players = get_players()
players = prune_players(players)
bestsum = float('inf')
bestmax = float('inf')
bestfirst = float('inf')
bestlast = float('inf')
print("Processing {:,} lineups of {} players: ".format(math.factorial(len(players)), len(players)) + str(list(players.keys())))
for curlineup in itertools.permutations(players):
innings = []
for pos, player in enumerate(curlineup):
innings.append(players[player][pos])
innings.append(players.get(curlineup[-1])[-1])
cursum = sum(innings)
curmax = max(innings)
curlast = innings[-1]
curfirst = innings[0]
if cursum > bestsum:
continue
elif cursum == bestsum:
if curmax > bestmax:
continue
elif curmax == bestmax:
if curlast > bestlast:
continue
elif curlast == bestlast:
if curfirst > bestfirst:
continue
elif curfirst == bestfirst:
if random.randint(0, 1):
continue
bestsum = cursum
bestmax = curmax
bestlast = curlast
bestfirst = curfirst
bestlineup = curlineup
print("New Best Lineup: " + str(bestlineup) + " Sum: " + str(bestsum) + " Max: " + str(bestmax) + " Last: " + str(bestlast) + " First: " + str(bestfirst))
if __name__ == '__main__':
main()