🐒
タイムスケジュールを作るプログラムを作った
コード
from typing import List
class ScheduleGenerator:
def __init__(self, start: int, end: int, step: int = None, fill: bool = True, include: bool = True, delimiter: str = None) -> None:
hours = [i for i in range(start, end)]
self.step = 30 if step is None else step
self.fill = fill
self.delimiter = ":" if delimiter is None else delimiter
self.time_schedules = []
for hour in hours:
for minute in range(0, 60, self.step):
if fill:
hour = str(hour).zfill(2)
minute = str(minute).zfill(2)
self.time_schedules.append(f"{hour}{self.delimiter}{minute}")
if include:
if hours[-1] != 23:
if fill:
self.time_schedules.append(f"{hours[-1] + 1}{self.delimiter}00")
else:
self.time_schedules.append(f"{hours[-1] + 1}{self.delimiter}0")
def delete(self, delete_schedules: List[str] = None, start: str = None, end: str = None) -> List[str]:
start = "" if start is None else start
end = "" if end is None else end
delete_schedules = [] if delete_schedules is None else delete_schedules
if not delete_schedules:
[self.time_schedules.remove(delete_schedule) for delete_schedule in delete_schedules]
if start != "" and end != "":
try:
start_index = self.time_schedules.index(start)
end_index = self.time_schedules.index(end)
result = self.time_schedules[:start_index] + self.time_schedules[end_index:]
return result
except ValueError as ex:
raise ex
else:
return self.time_schedules
def addition(self, add_schedules: List[str]) -> List[str]:
self.time_schedules += add_schedules
self.reorder()
return self.time_schedules
def reorder(self) -> None:
zero_twentyfour = {k: [] for k in range(25)}
for ts in self.time_schedules:
ts_splited = ts.split(f"{self.delimiter}")
for hour in zero_twentyfour.keys():
if int(ts_splited[0]) == hour:
zero_twentyfour[hour].append(int(ts_splited[1]))
zero_twentyfour[hour].sort()
time_schedules = []
for hour, minutes in zero_twentyfour.items():
for minute in minutes:
if self.fill:
hour = str(hour).zfill(2)
minute = str(minute).zfill(2)
time_schedules.append(f"{hour}{self.delimiter}{minute}")
self.time_schedules = time_schedules
def __call__(self) -> List[str]:
return self.time_schedules
使い方
>>> ScheduleGenerator(9, 12, step=20)()
['09:00', '09:20', '09:40', '10:00', '10:20', '10:40', '11:00', '11:20', '11:40', '12:00']
>>> ScheduleGenerator(3, 6, delimiter="-")()
['03-00', '03-30', '04-00', '04-30', '05-00', '05-30', '6-00']
>>> ScheduleGenerator(8, 10, fill=False, include=False)()
['8:0', '8:30', '9:0', '9:30']
>>> ScheduleGenerator(9, 11).addition(add_schedules=['09:15'])
['09:00', '09:15', '09:30', '10:00', '10:30', '11:00']
>>> ScheduleGenerator(9, 15).delete(delete_schedules=['11:30', '12:00', '12:30'])
['09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00']
>>> ScheduleGenerator(9, 15).delete(start='11:30', end='13:00')
['09:00', '09:30', '10:00', '10:30', '11:00', '13:00', '13:30', '14:00', '14:30', '15:00']
schedule
ライブラリと使う場合
schedule
をインストール
$ pip3 install schedule
コメントアウトした時間にfunc
関数が実行される
import schedule
def func():
...
# ['09:00', '09:30', '10:00', '10:30', '11:00', '13:00', '13:30', '14:00', '14:30', '15:00']
time_schedules = ScheduleGenerator(9, 15).delete(start='11:30', end='13:00')
[schedule.every().day.at(i).do(func) for i in time_schedules]
while True:
schedule.run_pending()
time.sleep(1)
Discussion