def test_stats(explicit_initial): subject = explicit_initial print dump(subject) assert stats(subject) == [("Alice-Bob", 2, 17), ("Charlie-Dave", 2, 42)]
def write_excel(state): """ @type state: seating.State """ wb = Workbook(encoding="utf8") groups = wb.add_sheet('Groups') groups.col(0).width = 7000 for person, name in enumerate(state.names): groups.write(person + 1, 0, name) group_col_count = 1 for group_name, (i, j), weight in zip(state.group_names, state.group_indexes, state.group_weights): if weight > 1: group_name += " (%d)" % weight groups.col(group_col_count).width = 600 groups.write(0, group_col_count, group_name, style=ROTATED) for person in range(state.persons): seated = state.seating[person, i:j].any() if seated: groups.write(person + 1, group_col_count, 1) group_col_count += 1 tables = wb.add_sheet('Tables') table_row_count = 0 for group_name, (i, j), weight in zip(state.group_names, state.group_indexes, state.group_weights): if i + 1 < j: tables.write(table_row_count, 0, group_name, style=BOLD) table_col_count = 1 for position in range(i, j): persons = numpy.where(state.seating[:, position] == 1)[0] tables.write(table_row_count, table_col_count, len(persons)) tables.col(table_col_count).width = 1000 table_col_count += 1 table_row_count += 1 placement = wb.add_sheet('Placement') placement_col_count = 0 for group_name, (i, j), weight in zip(state.group_names, state.group_indexes, state.group_weights): if i + 1 < j: placement.write(0, placement_col_count, group_name, style=BOLD) placement_row_count = 2 for position in range(i, j): persons = numpy.where(state.seating[:, position] == 1)[0] for person in persons: fixed = state.fixed[person, position] label = state.names[person] if fixed: label = "*" + label placement.write(placement_row_count, placement_col_count, label, style=BOLD if fixed else NORMAL) placement_row_count += 1 placement_row_count += 1 placement_col_count += 1 statistics = wb.add_sheet('Statistics') statistics.write(0, 0, "Pair", style=BOLD) statistics.write(0, 1, "Meal closeness", style=BOLD) statistics.write(0, 2, "Group closeness", style=BOLD) for index, (pair, meal_closeness, group_closeness) in enumerate(stats(state)): statistics.write(index + 1, 0, pair) statistics.write(index + 1, 1, meal_closeness) statistics.write(index + 1, 2, group_closeness) result = BytesIO() wb.save(result) return result.getvalue()