-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjreport.py
executable file
·454 lines (397 loc) · 13.9 KB
/
injreport.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
#!/usr/bin/env python
#
# -a: active only
# -w <week>: select week
import os
import sys
import getopt
import psycopg2
import DB
from man import help
def usage():
print("usage: %s [flags]" % sys.argv[0])
help( __file__ )
sys.exit(1)
# dump environment and parameters for testing
# not really necessary, mostly for learning purposes
def dumpenv(form):
for (env, val) in list(os.environ.items()):
print("<br>", env + " : ", val)
print("<p>parameters")
for param in list(form.keys()):
print("<br>", param + " : ", end=' ')
for val in form.getlist(param):
print(val, end=' ')
print()
print("<p>")
return
# schedule
DAYS_OFF = ( 3, 5, 8, 11, 16, 18, 21, 23 )
LONG_SERIES = ( 4, 5, 12, 13, 24, 25 )
LAST_WEEK = 25
ASB = 13
# sql table injury codes
injured = 0
no_dtd = 1
suspended = 2
adjustment = 3
arm_trouble = 4
# display codes (bitwise)
ok = 0
off = 1
inj = 2
dtd = 4
sus = 8
adj = 16
arm = 32
def injdays( player, stop, type = inj ):
total = 0
for week in list(player.keys()):
if week <= stop:
for series in list(player[week].keys()):
for x in player[week][series]:
if ( x & (type + sus)) > 0:
total += 1
return total
def allstar( week ):
if week == ASB:
return True
else:
return False
def get_series( player, name, week, loc ):
if week in player[name]:
if loc in player[name][week]:
return player[name][week][loc]
else:
player[name][week][loc] = {}
else:
player[name][week] = {}
player[name][week][loc] = {}
if week == (LAST_WEEK + 1):
obj = []
for x in range(40):
obj.append(0)
else:
obj = [ 0, 0, 0 ]
if week in LONG_SERIES:
obj.append(0)
if week in DAYS_OFF:
obj.append(1)
return obj
def search( days, code ):
found = -1
for x in range( len(days) ):
if days[x] & code == code:
found = x
return found + 1
def totals( week, code ):
found = {}
for loc in list(week.keys()):
found[loc] = 0
for x in week[loc]:
if x & code == code:
found[loc] += 1
return list(found.items())
def update( days, code, length, day = 1 ):
served = 0
for x in range( day - 1, len(days) ):
if code == injured:
if length == 1 and days[x] & off == 0:
if days[x] & dtd == 0:
days[x] += dtd
length -= 1
served += 1
elif days[x] & inj == 0:
if length > 1:
days[x] += inj
length -= 1
served += 1
if code == no_dtd:
if days[x] & inj == 0:
if length > 0:
days[x] += inj
length -= 1
served += 1
if code == arm_trouble:
if days[x] & arm == 0:
if length > 0:
days[x] += arm
length -= 1
served += 1
if code == suspended:
if days[x] & (sus + off) == 0:
if length > 0:
days[x] += sus
length -= 1
served += 1
if code == adjustment:
if length > 0:
if days[x] & adj == 0:
days[x] += adj
length -= 1
served += 1
return served
def dcode( days ):
output = "["
dc = { off:'off', inj:'inj', dtd:'dtd', sus:'sus', adj:'adj', arm:'arm' }
for x in days:
if x == ok:
output += "( OK )"
else:
output += "( "
for val in list(dc.keys()):
if x & val != 0:
output += dc[val]
output += " "
output += ")"
output += "]"
return output
def kind ( code ):
if code == suspended:
return sus
elif code == adjustment:
return adj
elif code == arm_trouble:
return arm
else:
# default is injury
return inj
def space ( tigname ):
if tigname[2] == ' ':
return tigname[0:2] + ' ' + tigname[2:]
else:
return tigname
def main( player = {}, module = False, report_week = 0 ):
do_json = False
is_cgi = False
if not module and 'GATEWAY_INTERFACE' in os.environ:
import cgi
#import cgitb; cgitb.enable()
form = cgi.FieldStorage()
is_cgi = True
if 'json' in form:
import json
do_json = True
print("Content-Type: application/json")
print()
else:
do_json = False
print("Content-Type: text/html")
print()
if 'notitle' not in form:
print("<html><head><title>IBL Injury Report</title></head><body>")
#dumpenv(form)
# unset for actives only
do_all = 1
if is_cgi:
if 'week' in form:
report_week = int(form.getfirst('week'))
if 'active' in form:
do_all = 0
elif not module:
for (opt, arg) in opts:
if opt == '--help':
usage()
if opt == '-a':
do_all = 0;
elif opt == '-w':
report_week = int(arg)
else:
print("bad option:", opt)
usage()
db = DB.connect()
cursor = db.cursor()
if report_week == 0:
# no user supplied week, use latest week without all inj reported
sql = "select week, count(*) from %s where inj = 1\
group by week order by week desc;" % DB.sched
cursor.execute(sql)
for week, num in cursor.fetchall():
if num == 24:
report_week = week + 1
break
if is_cgi and not module:
print("<table>")
sql = "select week, home, away, day, type, ibl, ibl_team, status, \
i.tig_name, length, dtd, description from %s i \
left outer join rosters t on i.tig_name = t.tig_name\
order by ibl_team, i.tig_name, week;" % DB.inj
cursor.execute(sql)
for injury in cursor.fetchall():
week, home, away, day, code, inj_team, ibl, active, \
name, length, failed, desc = injury
##print injury
name = name.rstrip()
if active != 1:
active = 0
if name not in player:
player[name] = {}
if week == (LAST_WEEK + 1):
loc_q = [ 'playoffs' ]
elif inj_team == home:
loc_q = [ 'home', 'away' ]
else:
loc_q = [ 'away', 'home' ]
reported_day = day
reported_week = week
reported_loc = loc_q[0]
reported_length = length
# add 1 for dtd
if code == injured:
length += 1
served = 0
loc = loc_q[0]
series = get_series( player, name, week, loc )
# when day > series length inj time assessed next series
if day <= len(series):
served = update( series, code, length, day )
length -= served
player[name][week][loc] = series
##print( "week %2i %s: %i served (%3i) %s" % \
## (week, loc, served, length, dcode(player[name][week][loc])) )
if length > 0 and allstar( week ) and code != suspended:
# dtd expires during ASB
if code == injured and length <= 3:
code = no_dtd
length -= 1
loc = 'ASB'
if week in player[name] and \
loc in player[name][week]:
series = player[name][week][loc]
else:
series = [ 1, 1, 1 ]
served = update( series, code, length )
length -= served
player[name][week][loc] = series
##print( "week %2i %s: %i served (%3i) %s" % \
## (week, loc, served, length, dcode(player[name][week][loc])) )
while length > 0 and week < LAST_WEEK:
loc = loc_q[0]
week += 1
series = get_series( player, name, week, loc )
served = update( series, code, length )
length -= served
player[name][week][loc] = series
##print( "week %2i %s: %i served (%3i) %s" % \
## (week, loc, served, length, dcode(player[name][week][loc])) )
if length == 0:
break
loc_q.reverse()
loc = loc_q[0]
series = get_series( player, name, week, loc )
served = update( series, code, length )
length -= served
player[name][week][loc] = series
##print( "week %2i %s: %i served (%3i) %s" % \
## (week, loc, served, length, dcode(player[name][week][loc])) )
if length > 0 and allstar( week ) and code != suspended:
# dtd expires during ASB
if code == injured and length <= 3:
code = no_dtd
length -= 1
loc = 'ASB'
if week in player[name] and \
loc in player[name][week]:
series = player[name][week][loc]
else:
series = [ 1, 1, 1 ]
served = update( series, code, length )
length -= served
player[name][week][loc] = series
##print( "week %2i %s: %i served (%3i) %s" % \
## (week, loc, served, length, dcode(player[name][week][loc])) )
# END while length loop
if length > 0:
# post-season
if week == LAST_WEEK:
week += 1
loc = 'playoffs'
series = get_series( player, name, week, loc )
served = update( series, code, length )
length -= served
player[name][week][loc] = series
##print( "week %2i %s: %i served (%3i) %s" % \
## (week, loc, served, length, dcode(player[name][week][loc])) )
if not module and week >= report_week:
if not ibl:
ibl = inj_team
output = "%s %s " % (ibl, space(name) )
if code == suspended:
output += "suspended for %i game" % int(reported_length)
elif code == adjustment:
output += "adjustment for %i day" % int(reported_length)
elif code == arm_trouble:
output += "cannot pitch for %i day" % int(reported_length)
else:
# default is injury
output += "out for %i day" % int(reported_length)
if reported_length != 1:
output += "s"
output += ", "
if reported_length > 1:
output += "from "
rep_series = get_series(player, name, reported_week, reported_loc)
if reported_length > 0 and reported_day > len(rep_series):
# injury begins the following week
output += "week %s (%s day %s)" \
% ( reported_week + 1, reported_loc, \
reported_day - len(rep_series) )
else:
output += "week %s (%s day %s)" \
% ( reported_week, reported_loc, reported_day )
if length > 0:
output += " through end of season"
else:
days_out = totals( player[name][week], kind(code) )
days_out.sort( key = lambda s: s[1], reverse=True )
days_out.sort( key = lambda s: s[0] == 'ASB' )
week_tot = 0
for x in days_out:
week_tot += x[1]
if week_tot == 0:
# player available start of week, check previous
thru_week = week - 1
if thru_week not in player[name]:
player[name][thru_week] = {}
days_out = totals( player[name][thru_week], kind(code) )
days_out.sort( key = lambda s: s[1], reverse=True )
days_out.sort( key = lambda s: s[0] == 'ASB' )
elif reported_length > 0:
thru_week = week
if reported_length > 1:
output += " through week %i (" % thru_week
if reported_week == thru_week:
# ends same week it starts (check for ASB days)
days_out.sort( key = lambda s: s[1], reverse=True )
if days_out[0][0] == 'ASB' and days_out[0][1] > 0:
output += "%s day %i)" % ( 'ASB', days_out[0][1] )
else:
output += "%s day %i)" % ( reported_loc, \
reported_day + reported_length - 1 )
else:
# ends in future week
for x in days_out:
output += "%i %s, " % ( x[1], x[0] )
output = output[:-2] + ")"
if code == injured:
output += ", DTD (+%i) %s day %i" % \
(failed, loc, search(series, dtd))
if thru_week != week:
output += " week %i" % week
if do_all or active:
if is_cgi:
print('<tr><td>%s</td><td>"%s"</td></tr>' % \
( output + '.', desc ))
else:
print('%-80s\t"%s"' % ( output + '.', desc ))
# END if report_week
# END injury loop
if is_cgi and not module:
print("</table>")
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], 'aw:', ["help"])
except getopt.GetoptError as err:
print(str(err))
usage()
main()