Skip to content

Instantly share code, notes, and snippets.

@schafon
Last active December 17, 2017 07:41
Show Gist options
  • Save schafon/f7fa27bd82b617d19ade8c74e33b90b8 to your computer and use it in GitHub Desktop.
Save schafon/f7fa27bd82b617d19ade8c74e33b90b8 to your computer and use it in GitHub Desktop.
class EmployeeProfile:
def __init__(self, name, max_shifts=5):
"""Return a Customer object whose name is *name* and starting
balance is *balance*."""
self.name = name
self.max_shifts = max_shifts
self.shift_count = 0
self.last_week_shifts = [False, False]
self.shifts = [False, False, False,
False, False, False,
False, False, False,
False, False, False,
False, False, False,
False, False, False,
False, False, False]
self.user_req = []
self.user_req_avl = False
def add_shift(self):
self.shift_count = self.shift_count + 1
def check_2_prev_shifts(self, on_shift):
if on_shift > 1:
if self.shifts[on_shift] is False and self.shifts[on_shift-1] is False and self.shifts[on_shift-2] is False:
return False
else:
return True
elif on_shift is 1:
print(self.name + " last week shifts: " + str(self.last_week_shifts))
if self.last_week_shifts[1] is False and self.shifts[0] is False:
return False
else:
return True
elif on_shift is 0:
if self.last_week_shifts[1] is False and self.last_week_shifts[0] is False:
return False
else:
return True
def reset_shifts(self):
self.shifts = [False, False, False,
False, False, False,
False, False, False,
False, False, False,
False, False, False,
False, False, False,
False, False, False]
self.shift_count = 0
def emp_can_work_on(self, shift_num):
if self.user_req_avl:
if self.user_req[shift_num]:
return True
else:
return False
else:
return False
def set_user_req(self, req):
self.user_req = req
def set_user_req_avl(self, avl):
self.user_req_avl = avl
def set_last_week_shifts(self, last_week_shifts1):
self.last_week_shifts[1] = last_week_shifts1[1]
self.last_week_shifts[0] = last_week_shifts1[0]
class CalnderClass:
def __init__(self, current_calnder=[]):
self.calender = current_calnder
def set_emp_to_index(self, index, emp):
self.calender[index] = emp
def get_cal(self):
return self.calender
import employee_profile
import random
import time
import os
shifts_count = 21
num_of_emp = 5
employees_array = []
shifts_options = []
calender = ["null", "null", "null",
"null", "null", "null",
"null", "null", "null",
"null", "null", "null",
"null", "null", "null",
"null", "null", "null",
"null", "null", "null"
]
def save_opt_to_memory(cal):
shifts_options.append(str(cal))
def load_last_week_shift():
f = open("/home/pi/Desktop/this_week_shifts/shifts.txt")
last_week_shifts = f.read()
print("Last week shifts:\n" + str(last_week_shifts))
last_week_shifts = last_week_shifts.replace("[", "")
last_week_shifts = last_week_shifts.replace("]", "")
last_week_shifts = last_week_shifts.split(',')
print(type(last_week_shifts))
for shift in last_week_shifts:
print(shift)
shift.strip()
shift = shift.translate(str.maketrans({"'": None}))
print(shift)
user_work_on_sat_evn = str(last_week_shifts[19])
user_work_on_sat_evn = user_work_on_sat_evn[1:]
user_work_on_sat_evn = user_work_on_sat_evn[:-1]
user_work_on_sat_evn = user_work_on_sat_evn.translate(str.maketrans({"'": None}))
user_work_on_sat_night = str(last_week_shifts[20])
user_work_on_sat_night = user_work_on_sat_night[1:]
user_work_on_sat_night = user_work_on_sat_night[:-1]
user_work_on_sat_night = user_work_on_sat_night.translate(str.maketrans({"'": None}))
for employee in employees_array:
print(str(user_work_on_sat_evn) + " == " + str(employee.name) + " ?")
if user_work_on_sat_evn in employee.name:
user_worked_weekend_evn = [True, False]
employee.set_last_week_shifts(user_worked_weekend_evn)
print(str(employee.name) + " worked on sat evn")
print(str(user_work_on_sat_night) + " == " + str(employee.name) + " ?")
if user_work_on_sat_night in employee.name:
user_worked_weekend_night = [False, True]
employee.set_last_week_shifts(user_worked_weekend_night)
print(str(employee.name) + " worked on sat night")
def load_users_requests():
for user in employees_array:
try:
f = open("/home/pi/Desktop/users_requests/" + str(user.name), 'r')
user_req = f.read()
user_req = user_req.replace('[', '')
user_req = user_req.replace(']', '')
user_req = user_req.split(',')
user.set_user_req(user_req)
print(user_req)
user.set_user_req_avl(True)
except FileNotFoundError:
print("Cant find request file for " + str(user.name))
user.set_user_req_avl(False)
def initilaize_emp():
nor = employee_profile.EmployeeProfile("Nor", 3)
employees_array.append(nor)
tomer = employee_profile.EmployeeProfile("Tomer", 5)
employees_array.append(tomer)
shachaf = employee_profile.EmployeeProfile("Shachaf", 5)
employees_array.append(shachaf)
yakir = employee_profile.EmployeeProfile("Yakir", 5)
employees_array.append(yakir)
stav = employee_profile.EmployeeProfile("Stav", 5)
employees_array.append(stav)
def start_assign(shifts_count, current_cal):
for index1 in range(shifts_count):
if 'null' in current_cal[index1]:
break_counter1 = 0
break_counter2 = 0
while True:
random_num = int(random.randrange(0, num_of_emp))
# print("Shift number = " + str(index1))
# print("Random number = " + str(random_num))
# print("employees_array[random_num].emp_can_work_on(i) = " + str(employees_array[random_num].emp_can_work_on(index1)))
# print("employees_array[random_num].check_2_prev_shifts(i) = " + str(employees_array[random_num].check_2_prev_shifts(index1)))
if employees_array[random_num].emp_can_work_on(index1):
if employees_array[random_num].check_2_prev_shifts(index1) is False:
# returnes False if emp dont work 2 shifts before
# print("Found emp for shift number " + str(index1))
# print(employees_array[random_num].name)
# cal[i] = (str(employees_array[random_num].name))
employees_array[random_num].shifts[index1] = True
current_cal[index1] = str(employees_array[random_num].name)
employees_array[random_num].add_shift()
break
else:
break_counter1 = break_counter1 + 1
if break_counter1 > 100:
current_cal[index1] = str("Error")
break_counter1 = 0
break
else:
break_counter2 = break_counter2 + 1
if break_counter2 > 100:
current_cal[index1] = str("Error")
break_counter2 = 0
break
return current_cal
error_count = 0
initilaize_emp()
print("initilized")
print("loading users requests...")
load_users_requests()
print("loading last week shifts...")
load_last_week_shift()
user_input = input("Please enter shifts first day of mount\n")
shift_date = str(user_input)
for i in range(5):
for emp in employees_array:
emp.reset_shifts()
# print("reset " + str(emp.name))
calender = ["null", "null", "null",
"null", "null", "null",
"null", "null", "null",
"null", "null", "null",
"null", "null", "null",
"null", "null", "null",
"null", "null", "null"]
#print(calender)
cal = start_assign(shifts_count, calender)
save_opt_to_memory(cal)
print("Option " + str(i+1) + " " + str(cal))
for calnder in cal:
if 'Error' in calnder:
error_count = error_count + 1
print("Errors: " + str(error_count))
error_count = 0
for index in employees_array:
print(str(index.name) + " got " + str(index.shift_count) + " shifts")
while True:
user_input = input("Please select shifts to use(1-5) c - cancel\n")
if user_input is '1':
f = open("/home/pi/Desktop/this_week_shifts/shifts_" + shift_date + ".txt", 'w')
f.write(shifts_options[0])
f.close()
print("Saved file named shifts_" + shift_date + ".txt")
break
elif user_input is '2':
f = open("/home/pi/Desktop/this_week_shifts/shifts_" + shift_date + ".txt", 'w')
f.write(shifts_options[1])
f.close()
print("Saved file named shifts_" + shift_date + ".txt")
break
elif user_input is '3':
f = open("/home/pi/Desktop/this_week_shifts/shifts_" + shift_date + ".txt", 'w')
f.write(shifts_options[2])
f.close()
print("Saved file named shifts_" + shift_date + ".txt")
break
elif user_input is '4':
f = open("/home/pi/Desktop/this_week_shifts/shifts_" + shift_date + ".txt", 'w')
f.write(shifts_options[3])
f.close()
print("Saved file named shifts_" + shift_date + ".txt")
break
elif user_input is '5':
f = open("/home/pi/Desktop/this_week_shifts/shifts_" + shift_date + ".txt", 'w')
f.write(shifts_options[4])
f.close()
print("Saved file named shifts_" + shift_date + ".txt")
break
elif user_input is 'c':
user_input = input("Delete the file if exist?(y/n)\n")
if user_input is 'y':
if os.path.isfile("/home/pi/Desktop/this_week_shifts/shifts_" + shift_date + ".txt"):
os.system("rm /home/pi/Desktop/this_week_shifts/shifts_" + shift_date + ".txt")
print("Deleted")
else:
print("File doesnt exist")
else:
print("Not touching file")
break
else:
print("This is not an option!\nTry again..\m")
user_input = input("Would you like to delete users requests files(y)?")
if user_input is 'y':
os.system("sudo -r rm /home/pi/Desktop/users_requests")
os.system("sudo mkdir /home/pi/Desktop/users_request")
print("Deleted directory and created a new one")
else:
print("You choose NOT to delete the files, the files will be automatically deleted in sunday morning")
pass
print("Done")
time.sleep(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment