-
Notifications
You must be signed in to change notification settings - Fork 1
/
chooseplayers.py
621 lines (532 loc) · 25 KB
/
chooseplayers.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import pandas as pd # excel spreadsheet manipulation
from types import FunctionType # to type check function inputs that we expect to be functions
from datetime import datetime, timedelta, date # to get today's date
from filepath import Filepath
from bot import Bot
from config import ROOT, ignorePlayers, logEligiblePlayers, playerExceptions
from exception import NoPlayerFoundException, FailedAccountException, \
FailedUpdateException
from errorlogging import getLogger, logError, logFailedAccounts
def __get_date_formatted_for_excel(d):
"""
datetime.date -> string
d: datetime.date | date of interest
Produces the date of interest in month-day format. e.g july 7th -> 7-7
"""
return str(d.month) + '-' + str(d.day)
def __get_dataframes_for_choose_players(**kwargs):
"""
kwargs -> pd.DataFrame
sN: int | Strategy Number
vMN: int | Virtual Machine Number
num: int | number of unhandled accounts to return
activeDate: datetime.date | date for which we should parse the dataframe
Returns two pandas dataframes. One holds num of the unhandled accounts
corresponding to strategy sN and virtual machine vMN. The second holds
all unhandled accounts corresponding to strategy sN and virtual machine vMN
"""
import os
### Type check
assert type(kwargs['sN']) == int
assert type(kwargs['vMN']) == int
assert type(kwargs['num']) == int
assert type(kwargs['activeDate']) == date
### Read in the minion accounts file if available
minionPath = Filepath.get_minion_account_file(
sN=kwargs['sN'], vMN=kwargs['vMN'])
if os.path.isfile(minionPath):
dfPath = minionPath
### Otherwise get the master file
else:
dfPath = Filepath.get_accounts_file()
df = pd.read_excel( dfPath, sheetname='Production' )
### Let the user know what's up
print "--> Getting accounts file {}".format(dfPath)
### Parse it down to include only what we want
# If it's the master accounts file, only include
# those accounts with this strategy number and virtual machine number
if dfPath != minionPath:
df = df[df.Strategy == kwargs['sN']][df.VM == kwargs['vMN']]
# Only include those accounts that haven't yet been updated
dateFormatted = __get_date_formatted_for_excel(activeDate)
if dateFormatted in df.columns:
df = df[pd.isnull(df[dateFormatted])] # pd.isnull checks for NaNs (unhandled accounts)
# Only include the columns we want
df = df[['ID', 'Email', 'MLBPassword', 'Strategy', 'VM']]
return df[0:kwargs['num']], df
def __get_eligible_players(**kwargs):
"""
kwargs -> tupleOfTuples
activeDate: datetime.date | date of interest
funcDict: dict | dictionary with key, value pairs:
key: int
value: {'select_func': function,
'dist_func': function }
sN: int | strategy number
Returns the players eligible for distribution to accounts on date d, using
the selection function stored in funcDict[sN]['select_func']
"""
### Type check
assert type(kwargs['sN']) == int
assert type(kwargs['activeDate']) == date
assert type(kwargs['funcDict']) == dict
### Get the tuple of players
print "--> Getting {}'s eligible players".format(kwargs['activeDate'])
selectionFunction = kwargs['funcDict'][kwargs['sN']]['select_func']
if sN == 5:
eligiblePlayers = selectionFunction(kwargs['activeDate'])
elif sN in (6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17):
# Choose a pVal depending on the strategy
if sN in (6, 7, 8, 9, 10, 11):
pVal = 40
elif sN in (12, 13, 14, 15, 16, 17):
pVal = 80
# Choose a minERAVal depending on the strategy
if sN in (6, 7, 8, 12, 13, 14):
minERAVal = 4.0
elif sN in (9, 10, 11, 15, 16, 17):
minERAVal = 5.0
# Select the players
eligiblePlayers = selectionFunction( p=pVal,
activeDate=kwargs['activeDate'],
filt={'minERA': minERAVal})
### Let the user know what's up
if sN !=5:
print "today's top {} players after weeding:".format(pVal)
for index, player in enumerate(eligiblePlayers):
print " -->{}: {}".format(index + 1, player)
if sN ==5:
print "Today's eligible Players: {}".format(eligiblePlayers)
### Return it
return eligiblePlayers
def __report_no_more_selections(**kwargs):
"""
kwargs -> None
fulldf: pd.DataFrame | dataframe containing all the as-of-yet unhandled
accounts for sN and vMN
sN: int | strategy Number
vMN: int | virtual machine number
activeDate: date | activeDate
Assuming there are no more available selections today, puts the value
"DONE: NOELIGIBLE, NOELIGIBLE" in todays column of the minion account file
for strategy sN and virtualMachineNumber vMN
"""
### Type check
assert type(kwargs['fulldf']) == pd.DataFrame
assert type(kwargs['sN']) == int
assert type(kwargs['vMN']) == int
assert type(kwargs['activeDate']) == date
### Let the user know what's up
print "--> NO PLAYERS LEFT TODAY. LOGGING AND EXITING"
### Log the finished accounts
updatedAccounts = [ (username, ('NOELIGIBLE'), ('NOELIGIBLE')) for
dummyIndex, index, username, password, sN, vMN in
kwargs['fulldf'].itertuples() ]
log_updated_accounts( updatedAccounts, sN=kwargs['sN'],
vMN=kwargs['vMN'], activeDate=kwargs['activeDate'])
def __distribute_eligible_players(**kwargs):
"""
kwargs -> TuplesOfStrings TuplesOfStrings
funcDict: dict | dictionary with key, value pairs:
key: int
value: {'select_func': function,
'dist_func': function }
bot: Bot | bot to which we are distributing players
sN: int | strategy number
eligiblePlayers: tupleOfTupleOfStrings | players to be distributed
activeDate: datetime.date | date for which we are to select players
"""
### Type check
assert type(kwargs['funcDict']) == dict
assert type(kwargs['bot']) == Bot
assert type(kwargs['sN']) == int
assert type(kwargs['eligiblePlayers']) == tuple
## Distribute the players
distributionFunction = funcDict[kwargs['sN']]['dist_func']
if kwargs['sN'] in (5, 6, 9, 12, 15): # Random Double Down choices
p1, p2 = distributionFunction( bot=kwargs['bot'],
eligiblePlayers=kwargs['eligiblePlayers'])
elif kwargs['sN'] in (7, 10, 13, 16): # Double Down every time
p1, p2 = distributionFunction( bot=kwargs['bot'],
eligiblePlayers=kwargs['eligiblePlayers'],
doubleDown=True )
elif kwargs['sN'] in (8, 11, 14, 17): # Single Down every time
p1, p2 = distributionFunction( bot=kwargs['bot'],
eligiblePlayers=kwargs['eligiblePlayers'],
doubleDown=False )
return p1, p2
def get_num_accounts(sN=None, vMN=None, getRemaining=True, activeDate=None):
"""
int int bool -> int
sN: Strategy Number
vMN: virtual Machine Number
remaining: Indicates whether or not to count a player iff he hasn't
already been assigned to today.
Returns the number of accounts correspodning to strategy number sN
and virtual machine vMN. If required==True, then only returns the number
of accounts that have yet to be assigned to
"""
import os
## Type check
assert type(sN) == int
assert type(vMN) == int
assert type(getRemaining) == bool
## Assign initial variables
day = __get_date_formatted_for_excel(activeDate)
minionPath = Filepath.get_minion_account_file(sN=sN, vMN=vMN)
## If the minion account File exists, get it
if os.path.isfile(minionPath):
minionDF = pd.read_excel( minionPath,
sheetname="Production")
# If appropriate, parse out all accounts that have already been handled
if getRemaining and (day in minionDF.columns):
# pd.isnull checks for NaNs
minionDF = minionDF[pd.isnull(minionDF[day])]
## Otherwise get the accounts from the master accounts file
else:
fullDF = pd.read_excel( Filepath.get_accounts_file(),
sheetname='Production',
parse_cols= 'A:F' )
minionDF = fullDF[fullDF.Strategy == sN][fullDF.VM == vMN]
## return the length of the dataframe
return len(minionDF)
def log_updated_accounts(updatedAccounts, sN=None, vMN=None, activeDate=None):
"""
ListOfTuples -> None
updatedAccounts: ListOfTuples | A list of the accounts that were
updated in the choosePlayers function. Format:
(username, p1, p2)
where p2 and p2 are TuplesOfStrings of format
(firstName, lastName, teamAbbreviation)
sN: int | "strategy number" (see strategyNumber.txt)
vMN: int | virtual Machine Number.
Writes info about updated accounts to minion account files
"""
import os
## type check
assert type(updatedAccounts) == list
assert type(sN) == int
assert type(vMN) == int
assert type(activeDate) == date
## Let the user know which account file we are updating
minionAF = Filepath.get_minion_account_file(sN=sN, vMN=vMN)
print "--> Updating accounts file: {}".format(minionAF)
## If the minion spreadsheet hasn't been initalized yet, do so
if not os.path.isfile(minionAF):
fullDF = pd.read_excel( Filepath.get_accounts_file(),
sheetname='Production',
parse_cols= 'A:F' )
minionDF = fullDF[fullDF.Strategy == sN][fullDF.VM == vMN]
minionDF.to_excel( minionAF,
sheet_name='Production',
index=False # no extra column of row indices
)
## Get the minion spreadsheet corresponding to this sN and vMN
dateFormatted = __get_date_formatted_for_excel(activeDate)
minionDF = pd.read_excel( minionAF,
sheetname='Production' )
## Create the series corresponding to today's player selections
if dateFormatted in minionDF.columns:
accountInfoL = list(minionDF[dateFormatted])
del minionDF[dateFormatted]
else:
accountInfoL = ['' for i in range(len(minionDF))]
for account in updatedAccounts:
accountIndex = minionDF.Email[ minionDF.Email == \
account[0]].index[0]
accountInfoL[accountIndex] = 'Done. 1: {}, 2: {}'.format(
account[1], account[2])
## Put the dataframe together and print it to file
newDF = pd.concat( [minionDF,
pd.Series(accountInfoL, name=dateFormatted)],
axis=1)
newDF.to_excel( minionAF,
sheet_name='Production',
index=False # no extra column for row indices
)
def reportUnusedPlayers(sN, vMN, activeDate):
"""
int int -> None
Comapares the global list "eligiblePlayers" to the listed players
in the minion Account file for sN and vMN and logs player selection
rates to the log file
For example, if eligiblePlayers = (p1, p2, p3) and 3 accounts chose p1,
5 accounts chose p2 and 0 accounts chose p3, then it will write:
**** Player Selection Rates ****
p2: 5
p1: 3
p3: 0
"""
global logEligiblePlayers
global ignorePlayers
global playerExceptions
### Read in the minion accounts file
minionPath = Filepath.get_minion_account_file(sN=sN, vMN=vMN)
df = pd.read_excel( minionPath, sheetname='Production' )
### Let the user know what's up
print "--> Reporting Player selection rates for {}".format(minionPath)
### Only include column with today's selections
df = df[__get_date_formatted_for_excel(activeDate)]
### Compare to eligible players and construct selection counts
playerCounts = {}
for player in logEligiblePlayers:
playerCounts[player] = 0
for selection in df:
for player in logEligiblePlayers:
if str(player) in selection:
playerCounts[player] += 1
### Organize the player counts
sortedPlayerCounts = []
for player, count in playerCounts.iteritems():
sortedPlayerCounts.append((player, count))
sortedPlayerCounts.sort(key=lambda x: x[1])
### Log counts to file
logger = getLogger(activeDate=activeDate, sN=sN, vMN=vMN)
### Tell us what values the global variables had
logger.info("\n\n**** logEligiblePlayers ****\n" + str(logEligiblePlayers))
logger.info("\n\n**** ignorePlayers ****\n" + str(ignorePlayers))
logger.info("\n\n**** playerExceptions ****\n" + str(playerExceptions))
info = "\n\n**** Player Selection Rates ****\n"
for player, count in sortedPlayerCounts:
info = info + "\n --->{}: {}".format(player, count)
logger.info(info)
def choosePlayers(**kwargs):
"""
kwargs -> str|None
kwargs;
funcDict(REQUIRED): dict | a dictionary with key, value pairs of:
strategyNumber: (selection_func, distribution_func). See below
for descriptions of selection_func and distribution_func
selection_func: Function | Takes today's date,
returns a tuple of players eligible for distribution
to the various accounts.
distribution_func: Function | Takes a bot, and a list of
distribution-eligible players, and assigns the appropriate
players to that bot. Returns a tuple (p1, p2) of the
players that were assigned to the bot
sN(REQUIRED): int | "strategy number" (see strategyNumber.txt)
vMN(REQUIRED): int | Indicates which of n virtual machines responsible
for executing sN is currently calling choosePlayers. For example,
we might have 2 VMs responsible for executing strategy 3 for 1000
accounts. The first VM will be responsible for the first 500 accounts,
and the second Vm will be responsible for the last 500 accounts.
num(REQUIRED): int | Indicates how many accounts to assign to
activeDate(REQUIRED): datetime.date Indicates for which day we are
assigning players
Reads in the accounts from Filepath.get_accounts_file() that correspond
to sN, vMN, and then updates num of those accounts accordingly
Returns 'noneLeft' if no players are eligible anymore
"""
import os
import subprocess
global logEligiblePlayers # to log player selection rates
global ignorePlayers
###### Get our arguments: #####
funcDict = kwargs['funcDict']
sN = kwargs['sN']
vMN = kwargs['vMN']
num = kwargs['num']
activeDate = kwargs['activeDate']
###### get an error logger #####
print "\n--> Creating Error Logger"
logger = getLogger(kwargs['activeDate'], kwargs['sN'], kwargs['vMN'])
##### get list of accounts you need #####
df, fulldf = __get_dataframes_for_choose_players(
sN=sN, vMN=vMN, num=num, activeDate=activeDate)
##### Get today's eligible players ####
eligiblePlayers = None
while eligiblePlayers is None:
# wrap it in a try-except because sometimes the websites don't load
# and we need to try again
try:
eligiblePlayers = __get_eligible_players( activeDate=activeDate,
funcDict = funcDict,
sN=sN )
except Exception as e:
logger.error(e)
if len(ignorePlayers) == 0: # for player selection rate logging
logEligiblePlayers = [player for player in eligiblePlayers]
if len(eligiblePlayers) == 0: # report as much and exit gracefully
__report_no_more_selections( fulldf=fulldf,
sN=kwargs['sN'],
vMN=kwargs['vMN'],
activeDate=kwargs['activeDate'] )
return 'noneLeft'
###### update each of the accounts #####
numIters = 0 # if we iterate too many times, exit gracefully
lenDF = len(df) # for keeping track of how much we have left
failedAccounts = [] # in case we fail to update some accounts
updatedAccounts = [] # to keep track accounts to log to file
while len(updatedAccounts) != lenDF:
## Who have we already updated?
updatedUsernames = [ account[0] for account in updatedAccounts]
## If we've run the loop "too many" times, exit out
if numIters > (2 * lenDF):
logFailedAccounts( df=df, updatedUsernames=updatedUsernames,
logger=logger )
break
## Update those accounts baby!
for dummyIndex, index, username, password, sN, vMN in df.itertuples():
# log ps -A, for debugging purposes
# with open( Filepath.get_log_file(kwargs['activeDate'],
# kwargs['sN'], kwargs['vMN']), "a") as f:
# f.write('\n\n\n')
# f.write('ITER: {} with u, p: {}, {}\n'.format(
# numIters, username, password))
# f.write('TIME: {}\n'.format(datetime.now().time()))
# f.flush()
# subprocess.call(['ps', '-A'], stdout=f)
# don't update the same account twice
if username in updatedUsernames:
continue
# try to update the account
numIters += 1
try:
# print update information
print "\n--> Iteration {} of {}".format(numIters, 2 * lenDF)
print "--> Choosing players for account {} of {}. U: {}".format(
len(updatedAccounts)+1, lenDF, username)
print "------> Accounts Done: {0}({1:.2f}%)".format(
len(updatedAccounts),
float(len(updatedAccounts))/float(lenDF) * 100)
# make the appropriate bot and update him
bot, p1, p2 = (None, None, None) # in case we throw an exception before they get assigned
bot = Bot(str(username), str(password), activeDate)
print "-->Succesfully created Bot"
p1, p2 = __distribute_eligible_players(
funcDict=funcDict, bot=bot, sN=kwargs['sN'],
eligiblePlayers=eligiblePlayers)
# this should never happen. We now allow it to happen, because
# of ('Yadier', 'Molina', 'stl') on 07-18-2014. He is a catcher
#, and catchers get days off man!. We have to trust that our code
# isn't accidentally overlooking dudes
# except NoPlayerFoundException as e:
# logError( str(username), str(password), p1, p2, e, logger)
# if bot:
# bot.quit_browser() # closes display as well, if necessary
# raise
# if the user interferes, exit
except KeyboardInterrupt:
if bot:
bot.quit_browser() # closes display as well, if necessary
raise
# sometimes unstable browsers raise exceptions. Just try again
except Exception as e:
exc_type = sys.exc_info()[0]
print "------> Failure: {}".format(exc_type)
print "------> Logging to file"
logError( str(username), str(password), p1, p2, e, logger )
if bot:
bot.quit_browser() # closes display as well, if necessary
continue
# If it worked, record as much and keep going!
else:
print "-----> Success!"
updatedAccounts.append((username, p1, p2))
## Update the accounts file to reflect the updates
# we use the dictionary variables instead of the one's we retrieved
# at the top of the function because sN and vMN take on new values
# in the while loop
log_updated_accounts( updatedAccounts, sN=kwargs['sN'],
vMN=kwargs['vMN'], activeDate=kwargs['activeDate'] )
if __name__ == '__main__':
"""
Usage:
1) python chooseplayers.py -sN={strategyNumber} -vMN={virtualMachineNumber}
"""
import re
import sys
from selectfunctions import getRecommendedPicks, topPBatters
from distributionfunctions import randDownRandPlayers, staticDownRandPlayers
from pytz import timezone
## What strategyNumber virtualMachineNumber, and date are we using?
options = [arg for arg in sys.argv if '-' in arg]
# Get the strategy number
sNPattern = re.compile(r"""
-sN= # strategy number
[1-9] # 1 digit in 1-9
[0-7]? # 0 or 1 digits in (0,1,2,3,4,5,6,7)
""", re.VERBOSE)
matches = [ sNPattern.match(option) for option in options if
sNPattern.match(option) ]
assert len(matches) == 1
sN = int(matches[0].string[4:])
options.remove(matches[0].string)
# Get the virtualMachine Number
vMNPattern = re.compile(r"""
-vMN= # virtual machine number
[1-9] # first digit must be nonzero
[0-9]* # arbitrary number of digits
""", re.VERBOSE)
matches = [ vMNPattern.match(option) for option in options if
vMNPattern.match(option)]
assert len(matches) == 1
vMN = int(matches[0].string[5:])
options.remove(matches[0].string)
# Get the date
datePattern = re.compile(r"""
-d= # date "number"
[0-5] # digit must be 0-5)
""", re.VERBOSE)
matches = [ datePattern.match(option) for option in options if
datePattern.match(option) ]
assert len(matches) == 1
dateNUM = int(matches[0].string[3:])
options.remove(matches[0].string)
# e.g if dateNum == 4, activeDate is four days from now
activeDate = datetime.now(timezone('US/Eastern')).date() + timedelta(days=dateNUM)
## Check that we didn't get any bogus options:
if len(options) != 0:
raise KeyError("Invalid options: " + str(options))
## Assign players to accounts in chunks of 50 so that in case something
## bad happens, we finish as many players as possible
doneYet = ''
blockSize = 20
origCount = get_num_accounts(
sN=sN, vMN=vMN, getRemaining=True, activeDate=activeDate )
if origCount == 0:
print "We already done playboy!"
numLeft = origCount
funcDict = {
5: { 'select_func': getRecommendedPicks,
'dist_func' : randDownRandPlayers },
6: { 'select_func': topPBatters,
'dist_func' : randDownRandPlayers },
7: { 'select_func': topPBatters,
'dist_func' : staticDownRandPlayers },
8: { 'select_func': topPBatters,
'dist_func' : staticDownRandPlayers },
9: { 'select_func': topPBatters,
'dist_func' : randDownRandPlayers },
10: { 'select_func': topPBatters,
'dist_func' : staticDownRandPlayers},
11: { 'select_func': topPBatters,
'dist_func' : staticDownRandPlayers},
12: { 'select_func': topPBatters,
'dist_func' : randDownRandPlayers },
13: { 'select_func': topPBatters,
'dist_func' : staticDownRandPlayers},
14: { 'select_func': topPBatters,
'dist_func' : staticDownRandPlayers},
15: { 'select_func': topPBatters,
'dist_func' : randDownRandPlayers},
16: { 'select_func': topPBatters,
'dist_func' : staticDownRandPlayers},
17: { 'select_func': topPBatters,
'dist_func' : staticDownRandPlayers}
}
while numLeft > 0:
if doneYet == 'noneLeft': # if there are no eligible players left
numLeft = 0
break
else:
assert type(sN) == int
print "\n********** Assigning IN CHUNKS OF {}".format(blockSize) + \
":.Completed {} of {}".format(origCount-numLeft, origCount) + \
" Strategy, VM: {}, {} ***********".format(sN, vMN)
doneYet = choosePlayers( funcDict=funcDict, sN=sN, vMN=vMN,
num=blockSize, activeDate=activeDate)
numLeft = get_num_accounts(
sN=sN, vMN=vMN,
getRemaining=True, activeDate=activeDate )
reportUnusedPlayers(sN=sN, vMN=vMN, activeDate=activeDate)