rite python in full to have a gui that generate roster.
For instance i have 8 staffs (SN-SSN) and 6 staffs (EN-SEN).
Name: SN1, SN2, SN3 and so on for SN-SSN.
EN1, EN2, and so on for EN-SEN.
General Rules:
Roster rountine timing is '8.30am - 5.30pm' Monday to Friday (weekdays)
Roster can be assigned 'Early' or 'OverTime' which is out of routine time
Rules for each week:
Department -
everyday, requires only one from SN-SSN and one from EN-SEN to do 'Early',
everyday, Requires only one from SN-SSN and two from EN-SEN to do 'Overtime'
Staff -
every week, Cannot assign 2 consective days 'Early' or 'Overtime' for each staffs.
every week, one staff can have 2 out of routine hour (early or overtime). for EN-SEN only, when everyone has filled up max 2 out of routine hour, either 'Early' and 'Overtime', exception can be given to +1 out of routine hour.
Present the roster assignment in console.
Assistant
Here's a Python script that generates a roster for the given staff and department rules using a GUI:
python
import tkinter as tk
from tkinter import messagebox
# Staff and department rules
staffs = {
"SN": ["SN1", "SN2", "SN3", "SN4", "SN5", "SN6", "SN7", "SN8"],
"EN": ["EN1", "EN2", "EN3", "EN4", "EN5", "EN6"]
}
department_rules = {
"Early": {
"SN-SSN": 1,
"EN-SEN": 1
},
"Overtime": {
"SN-SSN": 1,
"EN-SEN": 2
}
}
# Helper functions
def check_rule(staff, rule_type, day):
if rule_type == "Early":
count = department_rules[rule_type]["SN-SSN"] if staff.startswith("SN") else department_rules[rule_type]["EN-SEN"]
elif rule_type == "Overtime":
count = department_rules[rule_type]["SN-SSN"] if staff.startswith("SN") else department_rules[rule_type]["EN-SEN"]
if count == 0:
return True
days = roster[day].values()
return staff not in days
def check_consecutive_days(staff, day):
if day == 0:
return True
prev_staff = roster[day - 1].get(staff, None)
return prev_staff is None or prev_staff != staff
def check_max_out_of_routine_hours(staff):
early_count = sum(1 for day in roster.values() if staff in day and "Early" in day[staff])
overtime_count = sum(1 for day in roster.values() if staff in day and "Overtime" in day[staff])
if staff.startswith("EN"):
return early_count + overtime_count <= 2
else:
return early_count + overtime_count <= 1
def generate_roster():
roster = {}
for day in range(5):
roster[day] = {}
for staff_type, staffs_list in staffs.items():
for staff in staffs_list:
if check_rule(staff, "Early", day) and check_rule(staff, "Overtime", day) \
and check_consecutive_days(staff, day) and check_max_out_of_routine_hours(staff):
roster[day][staff] = "Early" if staff_type == "SN" else "Overtime"
break
return roster
# GUI implementation
root = tk.Tk()
root.title("Roster Generator")
def show_roster():
roster = generate_roster()
print("Roster:")
for day in roster:
print(f"Day {day + 1}:")
for staff, role in roster[day].items():
print(f"{staff}: {role}")
print()
btn_generate = tk.Button(root, text="Generate Roster", command=show_roster)
btn_generate.pack()
root.mainloop()
This script creates a simple Tkinter GUI with a "Generate Roster" button. When the button is clicked, it generates the roster based on the given staff and department rules and prints it in the console.
Note: This script assumes that the roster should be generated for 5 weekdays (Monday to Friday). If you need to generate the roster for a different number of days, you can modify the range in the for day in range(5): loop accordingly.