-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
executable file
·449 lines (397 loc) · 12.8 KB
/
util.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
#!/usr/bin/env python
#
# -B: batters only
# -P: batters only
# -A: all teams
# -p: platoon differential
# -v: vs average
# -o: opponent data
# -w: weekly data
# -M: MLB usage
# -a: MLB usage (actives only)
# -t: usage total
# -s <week>: start week
# -e <week>: end week
# -y <year>: override season
import sys
import getopt
import psycopg2
import DB
from man import help
from card import p_split, p_hash, cardpath, batters, pitchers, wOBA
from usage import mlb_usage
from injreport import LAST_WEEK
# identifiers
pit = 1
bat = 2
left = 0
right = 1
def usage():
print("usage: %s [flags] <team>" % sys.argv[0])
help( __file__ )
sys.exit(1)
def trim(string):
if string and len(string) > 0:
return string.rstrip()
else:
return ' '
def power( rating ):
if rating == "Ex":
return 0.5
elif rating == "Vg":
return 0.4
elif rating == "Av":
return 0.3
elif rating == "Fr":
return 0.2
else:
return 0.1
def vsL(p, kind):
# pitcher
if kind == 1:
return( [ int(p[24]), int(p[25]), int(p[26]), int(p[18]) ] )
else:
return( [ int(p[21]), int(p[22]), int(p[23]), power(p[24]) ] )
def vsR(p, kind):
# pitcher
if kind == 1:
return( [ int(p[36]), int(p[37]), int(p[38]), int(p[30]) ] )
else:
return( [ int(p[33]), int(p[34]), int(p[35]), power(p[36]) ] )
def zero( stat ):
stat['vL'] = []
stat['vR'] = []
for d in range(0,6):
stat['vL'].append( 0.0 )
stat['vR'].append( 0.0 )
def b_avg( stat ):
# if split is zero (no PAs) simply return the opposite
if stat['vL'][5] == 0 and stat['vL'][0] == 1:
return( stat['vR'][5] / stat['vR'][0] )
if stat['vR'][5] == 0 and stat['vR'][0] == 1:
return( stat['vL'][5] / stat['vL'][0] )
# woba weighted average (for offense)
return( ( stat['vL'][5] + stat['vR'][5] ) / \
( stat['vL'][0] + stat['vR'][0] ) )
def p_avg( stat ):
# woba modified harmonic mean (for pitching)
wL = stat['vL'][5] / stat['vL'][0]
wR = stat['vR'][5] / stat['vR'][0]
mean = ( wL + wR ) / 2.0
harm = 2.0 * wL * wR / ( wL + wR )
return( mean + abs(mean - harm) )
def bE_avg( stat ):
# batter woba estimated using generic 27/73 (vLHP/vRHP) split
wL = stat['vL'][5] / stat['vL'][0]
wR = stat['vR'][5] / stat['vR'][0]
return( wL * 0.27 + wR * 0.73 )
def pE_avg( stat ):
# pitcher woba estimated using generic 43/57 (vLHB/vRHB) split
wL = stat['vL'][5] / stat['vL'][0]
wR = stat['vR'][5] / stat['vR'][0]
return( wL * 0.43 + wR * 0.57 )
def bdump( team, stat, opt, afunc ):
if stat['vL'][0] > 0 or stat['vR'][0] > 0:
if stat['vL'][0] == 0:
stat['vL'][0] += 1
if stat['vR'][0] == 0:
stat['vR'][0] += 1
stat['avg'] = afunc( stat )
print("%s" % ( team ), end=' ')
for d in 1, 2, 3:
print(" %5.1f" % ( stat['vL'][d] / stat['vL'][0] ), end=' ')
print(" %4.3f" % ( stat['vL'][4] / stat['vL'][0] ), end=' ')
print(" %3d" % ( stat['vL'][5] / stat['vL'][0] + 0.5 ), end=' ')
print(".", end=' ')
for d in 1, 2, 3:
print(" %5.1f" % ( stat['vR'][d] / stat['vR'][0] ), end=' ')
print(" %4.3f" % ( stat['vR'][4] / stat['vR'][0] ), end=' ')
print(" %3d" % ( stat['vR'][5] / stat['vR'][0] + 0.5 ), end=' ')
print(".", end=' ')
if opt == 0:
print("%5.1f" % ( 0.0 ))
elif opt == overall:
print(" %3d" % ( stat['avg'] + 0.5 ))
elif opt == platoon:
# platoon differential
print("%+5.1f" % ( stat['vL'][5] / stat['vL'][0] -
stat['vR'][5] / stat['vR'][0] ))
else:
# vs average
print("%+5.1f" % ( stat['avg'] - opt ))
def pdump( team, stat, opt, afunc ):
if stat['vL'][0] > 0 or stat['vR'][0] > 0:
if stat['vL'][0] == 0:
stat['vL'][0] += 1
if stat['vR'][0] == 0:
stat['vR'][0] += 1
stat['avg'] = afunc( stat )
print("%s" % ( team ), end=' ')
for d in 1, 2, 3, 4:
print(" %5.1f" % ( stat['vL'][d] / stat['vL'][0] ), end=' ')
print(" %3d" % ( stat['vL'][5] / stat['vL'][0] + 0.5 ), end=' ')
print(".", end=' ')
for d in 1, 2, 3, 4:
print(" %5.1f" % ( stat['vR'][d] / stat['vR'][0] ), end=' ')
print(" %3d" % ( stat['vR'][5] / stat['vR'][0] + 0.5 ), end=' ')
print(".", end=' ')
if opt == 0:
print("%5.1f" % ( 0.0 ))
elif opt == overall:
print(" %3d" % ( stat['avg'] + 0.5 ))
elif opt == platoon:
# platoon differential
print("%+5.1f" % ( stat['vR'][5] / stat['vR'][0] -
stat['vL'][5] / stat['vL'][0] ))
else:
# vs average
print("%+5.1f" % ( opt - stat['avg'] ))
# batter array: pa, h, ob, tb, pwr, woba
def b_total( team, mlb, name ):
if (mlb, name) in b_cards:
vl = vsL(b_cards[mlb, name], bat)
vr = vsR(b_cards[mlb, name], bat)
#print mlb, name, paL, vl
#print mlb, name, paR, vr
ibl[team]['vL'][0] += paL
ibl[team]['vR'][0] += paR
for d in 1, 2, 3, 4:
ibl[team]['vL'][d] += vl[d - 1] * paL
ibl[team]['vR'][d] += vr[d - 1] * paR
ibl[team]['vL'][5] += wOBA(b_cards[mlb, name], bat, left) * paL
ibl[team]['vR'][5] += wOBA(b_cards[mlb, name], bat, right) * paR
# pitcher array: bf, h, ob, tb, df, woba
def p_total( team, mlb, name ):
if (mlb, name) in p_cards:
vl = vsL(p_cards[mlb, name], pit)
vr = vsR(p_cards[mlb, name], pit)
#print mlb, name, bf, vl
#print mlb, name, bf, vr
ibl[team]['vL'][0] += bf
ibl[team]['vR'][0] += bf
for d in 1, 2, 3, 4:
ibl[team]['vL'][d] += vl[d - 1] * bf
ibl[team]['vR'][d] += vr[d - 1] * bf
ibl[team]['vL'][5] += wOBA(p_cards[mlb, name], pit, left) * bf
ibl[team]['vR'][5] += wOBA(p_cards[mlb, name], pit, right) * bf
# globals
ibl = {}
tot = {}
MLB_B = {}
MLB_P = {}
do_bat = True
do_pit = True
do_tot = False
do_opp = False
do_weekly = False
do_mlb = False
do_pt = False
do_all = False
overall = 1
platoon = 2
avg = 3
display = overall
start = 1
end = LAST_WEEK
s_arg = ''
e_arg = ''
active = ''
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'ABPMatopvws:e:y:', ["help"])
except getopt.GetoptError as err:
print(str(err))
usage()
for (opt, arg) in opts:
if opt == '--help':
usage()
if opt == '-B':
do_pit = False
elif opt == '-P':
do_bat = False
elif opt == '-o':
do_opp = True
elif opt == '-p':
display = platoon
elif opt == '-v':
display = avg
elif opt == '-s':
s_arg = arg
elif opt == '-e':
e_arg = arg
elif opt == '-w':
do_weekly = True
elif opt == '-y':
DB.usage = 'usage' + arg
elif opt == '-A':
do_tot = True
do_all = True
elif opt == '-M':
do_mlb = True
mlb_usage( MLB_P, MLB_B )
elif opt == '-a':
active = ' and status = 1'
elif opt == '-t':
do_pt = True
do_tot = False
else:
print("bad option:", opt)
usage()
if s_arg and s_arg.isdigit():
start = int(s_arg)
if e_arg and e_arg.isdigit():
end = int(e_arg)
if do_weekly and ( s_arg or e_arg or do_opp or display == avg ):
usage()
if do_mlb and ( do_weekly or do_opp ):
usage()
db = DB.connect()
cursor = db.cursor()
b_cards = p_hash( cardpath() + '/' + batters )
p_cards = p_hash( cardpath() + '/' + pitchers )
sql_weeks = "where week >= %d and week <= %d and " % ( start, end )
if do_all:
cursor.execute("select distinct(ibl_team) from rosters \
where ibl_team != 'FA';")
args += [ row[0] for row in sorted(cursor.fetchall()) ]
if do_mlb:
# current UC designation
sql = "select max(uncarded) from rosters;"
cursor.execute(sql)
( UCyy, ) = cursor.fetchone()
if do_bat:
if do_pit:
print("BATTERS")
sql_select = "select trim(mlb), trim(name), sum(vl), sum(vr) from %s "\
% ( DB.usage )
zero( tot )
if do_mlb:
avg_B = bE_avg
else:
avg_B = b_avg
for arg in args:
team = arg.upper()
ibl[team] = {}
zero( ibl[team] )
if do_weekly:
sql = sql_select + sql_weeks + \
" week = (%s) and ibl = (%s) and bf = 0 group by mlb, name;"
for week in range(1,28):
cursor.execute(sql, (week, team, ) )
for mlb, name, paL, paR in cursor.fetchall():
b_total( team, mlb, name )
if display == platoon:
bdump( team, ibl[team], platoon, avg_B )
else:
bdump( team, ibl[team], overall, avg_B )
zero( ibl[team] )
print()
continue
if do_mlb:
sql = "select trim(tig_name) from rosters where \
ibl_team = (%s) and uncarded < (%s) and item_type & 2 = 2"
cursor.execute(sql + active, (team, UCyy ) )
elif do_opp:
sql = sql_select + sql_weeks + \
" (home = (%s) or away = (%s)) and ibl != (%s) and bf = 0\
group by mlb, name;"
cursor.execute(sql, (team, team, team, ) )
else:
sql = sql_select + sql_weeks + \
" ibl = (%s) and bf = 0 group by mlb, name;"
cursor.execute(sql, (team, ) )
if do_mlb:
for tig_name in cursor.fetchall():
tigname = tig_name[0]
mlb, name = tigname.split()
paL = paR = MLB_B[tigname]
b_total( team, mlb, name )
else:
for mlb, name, paL, paR in cursor.fetchall():
b_total( team, mlb, name )
for d in range(0,6):
tot['vL'][d] += ibl[team]['vL'][d]
tot['vR'][d] += ibl[team]['vR'][d]
#end arg loop
for t in sorted(ibl):
if do_mlb and do_pt:
print("%s %d" % (t, ibl[t]['vL'][0]) )
elif display == avg:
tot['avg'] = avg_B( tot )
bdump( t, ibl[t], tot['avg'], avg_B )
else:
bdump( t, ibl[t], display, avg_B )
if do_tot:
if display == avg:
bdump( '---', tot, 0, avg_B )
else:
bdump( '---', tot, display, avg_B )
if do_pit:
if do_bat:
if not do_weekly:
print()
print("PITCHERS")
sql_select = "select trim(mlb), trim(name), sum(bf) from %s "\
% ( DB.usage )
zero( tot )
if do_mlb:
avg_P = pE_avg
else:
avg_P = p_avg
for arg in args:
team = arg.upper()
ibl[team] = {}
zero( ibl[team] )
if do_weekly:
sql = sql_select + sql_weeks + \
" week = (%s) and ibl = (%s) and bf > 0 group by mlb, name;"
for week in range(1,28):
cursor.execute(sql, (week, team, ) )
for mlb, name, bf in cursor.fetchall():
p_total( team, mlb, name )
if display == platoon:
pdump( team, ibl[team], platoon, avg_P )
else:
pdump( team, ibl[team], overall, avg_P )
zero( ibl[team] )
print()
continue
if do_mlb:
sql = "select trim(tig_name) from rosters where \
ibl_team = (%s) and uncarded < (%s) and item_type & 1 = 1"
cursor.execute(sql + active, (team, UCyy ) )
elif do_opp:
sql = sql_select + sql_weeks + \
" (home = (%s) or away = (%s)) and ibl != (%s) and bf > 0\
group by mlb, name;"
cursor.execute(sql, (team, team, team, ) )
else:
sql = sql_select + sql_weeks + \
" ibl = (%s) and bf > 0 group by mlb, name;"
cursor.execute(sql, (team, ) )
if do_mlb:
for tig_name in cursor.fetchall():
tigname = tig_name[0]
mlb, name = tigname.split()
bf = MLB_P[tigname]
p_total( team, mlb, name )
else:
for mlb, name, bf in cursor.fetchall():
p_total( team, mlb, name )
for d in range(0,6):
tot['vL'][d] += ibl[team]['vL'][d]
tot['vR'][d] += ibl[team]['vR'][d]
#end arg loop
for t in sorted(ibl):
if do_mlb and do_pt:
print("%s %d" % (t, ibl[t]['vL'][0]) )
elif display == avg:
tot['avg'] = avg_P( tot )
pdump( t, ibl[t], tot['avg'], avg_P )
else:
pdump( t, ibl[t], display, avg_P )
if do_tot:
if display == avg:
pdump( '---', tot, 0, avg_P )
else:
pdump( '---', tot, display, avg_P )
db.close()