import datetime
import itertools
class MyTimer:
def __init__(self, first_time_in_datetime, delta=10, target_time_in_datetime=None):
self.current = self.repair_first_time_type(_first_time=first_time_in_datetime)
self.target_time_in_datetime = self.repair_target_time_type(_target_time=target_time_in_datetime)
self.delta_in_minutes = delta
self.zero = self.target_time_in_datetime < self.current
def repair_first_time_type(self, _first_time):
return self._repair_time_type(_first_time)
def repair_target_time_type(self, _target_time):
if _target_time is not None:
return self._repair_time_type(time_data=_target_time)
else:
return datetime.time(23, 59)
@staticmethod
def _repair_time_type(time_data):
result = datetime.time(20)
if isinstance(time_data, datetime.time):
result = time_data
if isinstance(time_data, str) and ':' in time_data:
try:
_hour, _minute, *_ = time_data.split(':')
result = datetime.time(int(_hour), int(_minute))
except TypeError:
print('Need ISO format time. Time set to 20:00:00')
except ValueError:
print('only numbers and :. Time set to 20:00:00')
return result
def _add_time(self, tm=None):
if tm is None:
tm = self.current
fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)
fulldate = fulldate + datetime.timedelta(minutes=self.delta_in_minutes)
return fulldate.time()
def add_time(self):
self.current = self._add_time()
self.zero = self.zero and self.current > self.target_time_in_datetime
@property
def next(self):
return self._add_time()
@property
def check(self):
if not self.zero:
return self.current >= self.target_time_in_datetime
else:
return self.current < self.target_time_in_datetime
def __repr__(self):
return self.current.isoformat(timespec='minutes')
def write_file_from_data(data, filename):
with open(filename, 'wt') as target_file:
target_file.write('\n'.join(data))
def make_names():
return [input(f'введите {i}:') for i in 'abcv']
def make_data(_counter):
result_data = []
while not _counter.check:
time_from, time_to = _counter, _counter.next.isoformat(timespec='minutes')
result_string = f'{time_from}-{time_to} @{next(names)}'
result_data.append(result_string)
_counter.add_time()
return result_data
if __name__ == '__main__':
names = make_names()
# names = ['aaa', 'bbb', 'ccc', 'ddd']
names = itertools.cycle(names)
result_filename = 'result.txt'
first_time = '20:00'
target_time = '00:00'
delta_in_minutes = 10
counter = MyTimer(first_time, delta_in_minutes, target_time)
write_file_from_data(data=make_data(counter), filename=result_filename)