util.py 937 B

1234567891011121314151617181920212223242526272829303132
  1. import collections
  2. def parse_shift(shift):
  3. if shift in ['ML', 'AL', 'A/L', 'BH', 'SD']:
  4. return None, None, 8
  5. if shift.startswith('T/'):
  6. return None, None, float(shift[2:])
  7. if shift == 'N':
  8. return 21, 21 + 12, 12
  9. if shift == 'R':
  10. return None, None, None
  11. if shift[0].isdigit():
  12. start, end = shift.split('/')
  13. return float(start), float(end), float(end) - float(start)
  14. return None, None, None
  15. def get_cover(staff, roster):
  16. cover = collections.defaultdict(int)
  17. for member in staff:
  18. for date, shift in zip(roster['dates'], roster[member['name']]):
  19. if shift:
  20. start, end, _ = parse_shift(shift)
  21. if start is not None:
  22. for hr in range(int(start), int(end)):
  23. _date = date if hr < 24 else date + 1
  24. cover[_date, hr % 24] += 1
  25. return cover