Created
September 12, 2018 03:24
-
-
Save MattEding/b7db9e4262edb9be7dc7c27602294c71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import itertools | |
import json | |
import os | |
import random | |
SEPARATOR = '=' * 25 + '\n' | |
DEFAULT = collections.defaultdict(str) | |
def generate_seats(week_num, period_num, *, mode='x'): | |
"""Generate a random seating chart for students. | |
Requires access to a student.json file where a student has a "first", "last", and an optional "nickname" keys. | |
""" | |
seats_path = f'wk{week_num} - per{period_num}.txt' | |
cwd = os.getcwd() | |
seats_io = os.path.join(cwd, seats_path) | |
students_io = os.path.join(cwd, 'students.json') | |
with open(students_io, 'r') as students_fp: | |
all_students = json.load(students_fp) | |
students = [st for st in all_students if st['period'] == period_num] | |
random.shuffle(students) | |
seats = itertools.product('ABCDEF', range(1, 7)) | |
seats = [f'{letter}{number}' for letter, number in seats] | |
with open(seats_io, mode) as week_fp: | |
week_fp.write(f"Period {period_num}\n") | |
week_fp.write(SEPARATOR) | |
for st, seat in itertools.zip_longest(students, seats, fillvalue=DEFAULT): | |
first_name = st.get('nickname', st['first']) | |
last_initials = '.'.join(last[0] for last in st['last'].split() + [' ']) | |
name = f'{first_name} {last_initials}'.strip() | |
st_seat = f'{seat} - {name}\n' | |
week_fp.write(st_seat) | |
attendance_marks = '\t___ ___ ___ ___\n' | |
week_fp.write(attendance_marks) | |
if seat.endswith('6'): | |
week_fp.write(SEPARATOR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment