import collections import fileinput import string # https://docs.google.com/spreadsheets/d/1FzYvO825t1de0UpzL3ovzkXE3cRiXGZetLt1zCzzWjE/edit#gid=0 class Color: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' _COLORS = { 1: Color.OKBLUE + '%s' + Color.ENDC, 2: Color.OKGREEN + '%s' + Color.ENDC, 3: Color.WARNING + '%s' + Color.ENDC, 3: Color.FAIL + '%s' + Color.ENDC, 4: Color.FAIL + '%s' + Color.ENDC, 5: Color.FAIL + '%s' + Color.ENDC, 6: Color.FAIL + '%s' + Color.ENDC, 7: Color.FAIL + '%s' + Color.ENDC, 8: Color.FAIL + '%s' + Color.ENDC, 9: Color.FAIL + '%s' + Color.ENDC, 10: Color.FAIL + '%s' + Color.ENDC, } _HRS = { 'N': (21, 9), } _IGNORED = { 'R', 'ML', } _LEAVE_CODES = { 'AL', 'A/L', 'SD', 'BH', } _COLUMNS = list(string.ascii_uppercase[2:]) + ['AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH'] _DRIVERS = { 'John Garbo PPIM/SN', 'Katie Coyle SCW', 'Stanford Sokole SCW', 'Keith Duffy HCA', } _DOWS = { 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', } def _parse_roster(columns): if columns[0]: return {columns[0]: columns[1:]} elif columns[1].isdigit() or columns[1].startswith('-'): return {'dates': [int(x) for x in columns[1:] if x]} elif columns[1]: return {'dows': columns[1:]} else: return {} def _parse_staff(columns): return { 'name': columns[0], 'hours': float(columns[1]), 'driver': columns[2].lower() == 'yes', } def _parse_reqs(columns): if columns[1] in _DOWS: return {} elif columns[1].startswith('-') or columns[1].isdigit(): return {} else: return {i: v for i, v in enumerate(columns[1:]) if v} def parse(): lines = [x for x in fileinput.input()] lines.reverse() roster = {} staff = [] reqs = collections.defaultdict(list) func = None while lines: line = lines.pop().rstrip('\n') if not line.strip(): continue if line.startswith('Roster'): func = _parse_roster elif line.startswith('Name'): func = _parse_staff elif line.startswith('Shift requirements'): func = _parse_reqs else: columns = line.split('\t') if func == _parse_reqs: for i, v in func(columns).iteritems(): reqs[i].append(v) elif func == _parse_roster: roster.update(_parse_roster(columns)) elif func == _parse_staff: staff.append(_parse_staff(columns)) reqs = [reqs[i] for i in range(max(reqs.keys())+1)] print roster print staff print reqs return staff, reqs, roster def _get_req_deltas(full_reqs, roster, names): missing = [] extra = [] reqs_copy = {} for reqs, date in zip(full_reqs, roster['dates']): if date > 0: reqs_copy[date] = [req for req in reqs] for name in names: for date, shift in zip(roster['dates'], roster[name]): if date > 0 and shift: if shift in _IGNORED or shift in _LEAVE_CODES: continue if shift.startswith('T/'): continue if shift in reqs_copy.get(date, []): reqs_copy[date].remove(shift) else: extra.append((date, shift)) missing = [] for date, reqs in reqs_copy.iteritems(): missing.extend([(date, req) for req in reqs]) return sorted(extra), sorted(missing) def evaluate(staff, reqs, roster): # staff_deltas = _get_staff_deltas(staff) names = [x['name'] for x in staff] extra, missing = _get_req_deltas(reqs, roster, names) print 'extra:' for date, v in extra: print '\t', date, v print 'missing:' for date, v in missing: print '\t', date, v staff, reqs, roster = parse() evaluate(staff, reqs, roster) def old(): totals_by_day = collections.defaultdict(int) totals_by_hours = {} drivers_by_hours = {} for line in fileinput.input(): if line.startswith('\t'): continue fs = line.split('\t') fs = [x.strip() for x in fs] if not any(fs): continue try: target = float(fs[1]) except ValueError, e: print 'bad target hrs: %s' % fs continue name = fs[0].strip() driver = name in _DRIVERS total = 0 prev_end = None errors = [] for i, k in enumerate(fs[2:]): if not k: prev_end = None continue start = 0 end = 0 if k in _LEAVE_CODES: total += 8 prev_end = None continue elif k in _IGNORED: prev_end = None continue elif k in _HRS: start, end = _HRS[k] elif k.startswith('T/'): hrs = float(k[2:]) total += hrs continue elif '/' in k: start, end = k.split('/') start = int(start.strip()) end = int(end.strip()) else: errors.append('\tUnrecognized code: %s' % k) prev_end = None continue if end < start: end += 24 total += end - start if end <= 24: hrs = [(i, j) for j in range(start, end)] else: hrs = [(i, j) for j in range(start, 24)] + [(i+1, j) for j in range(0, end-24)] for day, hr in hrs: totals_by_day[day] += 1 if day not in totals_by_hours: totals_by_hours[day] = collections.defaultdict(int) drivers_by_hours[day] = collections.defaultdict(int) totals_by_hours[day][hr] += 1 if driver: drivers_by_hours[day][hr] += 1 if prev_end == 23 and start == 7: pass elif prev_end and (start + 24) < prev_end + 10: errors.append('\tWARNING: Possible bad start time for day %s' % _COLUMNS[i]) prev_end = end delta = total-target delta_str = ('+' if delta > 0 else '') + str(delta) if -3 <= delta <= 4: print Color.OKGREEN + name + ': %s' % delta_str elif -6 <= delta <= 8: print Color.WARNING + name + ': %s' % delta_str else: print Color.FAIL + name + ': %s' % delta_str #print name # print '\tTarget: ', target # print '\tTotal: ', total # print '\tDelta: %s%s' % ('+' if delta > 0 else '', delta) print Color.ENDC if errors: print Color.FAIL for error in errors: print error print Color.ENDC print '' print ' Cover by hour Driver cover by hour Cover by day' for k, v in sorted(totals_by_day.items()): by_hr = ''.join([COLORS.get(totals_by_hours[k].get(hr), '%s') % totals_by_hours[k].get(hr, ' ') for hr in range(24)]) drivers_by_hr = ''.join([COLORS.get(drivers_by_hours[k].get(hr), '%s') % drivers_by_hours[k].get(hr, ' ') for hr in range(24)]) print ' '.join([ _COLUMNS[k] + (' ' if len(_COLUMNS[k]) == 1 else ''), # '%02d' % v, by_hr, drivers_by_hr, '.' * (v/2), ]) # print '%s%s %02d %s %s %s' % (_COLUMNS[k], v, by_hr, drivers_by_hr, '.' * (v/2))