init: first version

This commit is contained in:
alban 2021-12-03 22:34:34 +01:00
commit 966d4819b4
5 changed files with 326 additions and 0 deletions

55
lib/planning.py Normal file
View file

@ -0,0 +1,55 @@
from debuggable import Debuggable
from schedule import Schedule
def check_schedules(schedules):
for schedule in schedules:
if isinstance(schedule, Schedule):
continue
raise Exception("Invalid argument provided: {}".format(schedule))
class Planning(Debuggable):
schedules: list = []
def __init__(self):
super(Planning, self).__init__()
def add(self, schedules):
try:
# check if all are valid schedules
check_schedules(schedules)
# clean the schedules for elapsed
self.clean_schedules()
# add the schedules
self.debug("planning_add_schedules", "Adding {} schedules".format(len(schedules)))
self.schedules.extend(schedules)
self.debug("planning_add_schedules", "Got {} schedules after".format(len(self.schedules)))
# sort
self.schedules.sort(key=lambda x: x.start_at)
except Exception as exc:
print(exc)
def clean_schedules(self):
removed = 0
self.debug("planning_clean_schedules","Got {} schedules before cleaning".format(len(self.schedules)))
schedule_length = len(self.schedules)
for j in range(0,schedule_length):
i = schedule_length - j - 1
schedule = self.schedules[i]
if schedule.elapsed():
del(self.schedules[i])
self.debug("planning_clean_schedules_indexes", "Removed index {}/{}".format(i,schedule_length))
removed += 1
self.debug("planning_clean_schedules","Removed {} schedules".format(removed))
def get_last_schedule(self):
end_at_ordered = self.schedules.copy()
if not end_at_ordered:
return None
end_at_ordered.sort(key=lambda x: x.end_at)
return end_at_ordered.pop() if len(self.schedules) else None
# EOF