Skip to content

Instantly share code, notes, and snippets.

@acnagy
Last active April 13, 2016 21:51
Show Gist options
  • Save acnagy/b5696cfac9d0ee102f68567870c868cf to your computer and use it in GitHub Desktop.
Save acnagy/b5696cfac9d0ee102f68567870c868cf to your computer and use it in GitHub Desktop.
*.csv
*.json
*.xlsx
"""
Scheduling Workshops
Minimize number of selected meetings, ensuring everyone can attend
Reqirements: JSON of agents, JSON of times, JSON of schedules formatted like:
{
"agents": ["a", "list", "of", "all", "agent", "names"]
}
{
"times": ["a", "list", "of", "all", "times", "possible"]
}
{
"agent": ["a", "list", "of", "times", "they", "can", "attend"],
"another_agent": ["another", "list", "of", "times"]
}
Usage: ./scheduling_workshops agents_file.json times_file.json schedules_file.json
Returns: file called 'meetings_export.json', which contains a pretty print of
the generated team meetings. This file is overwritten each time the script
runs.
"""
import json
import sys
agents = set()
times = set()
schedules = {}
attendance = {}
meetings = {}
accounted_for_attendees = set()
limited_attendees = set()
def main():
agents_file = sys.argv[1]
times_file = sys.argv[2]
schedules_file = sys.argv[3]
load_agents_times_schedules(agents_file, times_file, schedules_file)
find_attendance()
find_meetings(attendance)
if limited_attendees:
find_limited_agents()
find_meetings(attendance)
with open("meetings_export.json", "w") as outfile:
json.dump(meetings, outfile, sort_keys=True, indent=4, separators=(',', ': '))
print "GENERATED MEETINGS:"
print json.dumps(meetings, sort_keys=True, indent=4, separators=(',', ': '))
print "View meetings also in meetings_export.json file located in this directory."
print accounted_for_attendees
def load_agents_times_schedules(agents_file, times_file, schedules_file):
agents_loaded = open(agents_file)
times_loaded = open(times_file)
schedules_loaded = open(schedules_file)
agents_json = json.loads(agents_loaded.read())
times_json = json.loads(times_loaded.read())
schedules_json = json.loads(schedules_loaded.read())
agents.update(agents_json["agents"])
times.update(times_json["times"])
for agent in schedules_json:
schedules[agent] = schedules_json[agent]
def find_attendance():
for time in sorted(times):
attendance[time] = list()
for agent in schedules:
if time in schedules[agent]:
attendance[time].append(agent)
def find_limited_agents():
accounted_for_attendees.clear()
meetings.clear()
limited_attendance = dict()
new_attendees = set()
limited_attendance_original_length = len(limited_attendance)
for attendee in limited_attendees:
for time in schedules[attendee]:
limited_attendance[time] = attendance[time]
for attendee in limited_attendees:
best_meeting = max_attendance(limited_attendance)
for agent in attendance[best_meeting]:
new_attendees.add(agent)
meetings[best_meeting] = attendance[best_meeting]
accounted_for_attendees.update(new_attendees)
def find_meetings(valid_attendance):
attendance_options = dict(valid_attendance)
attendance_options_original_length = len(attendance_options)
new_attendees = set()
for date in range(0, attendance_options_original_length, 1):
best_meeting = max_attendance(attendance_options)
for agent in attendance[best_meeting]:
new_attendees.add(agent)
if len(new_attendees - accounted_for_attendees) > 3:
meetings[best_meeting] = attendance[best_meeting]
accounted_for_attendees.update(new_attendees)
del attendance_options[best_meeting]
limited_attendees = agents - accounted_for_attendees
def max_attendance(attendance_structure):
return max(attendance_structure, key=lambda x:attendance_structure[x])
if __name__ == ("__main__"):
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment