Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
No Oven is left behind! solution in Clear category for Microwave Ovens by Stensen
from datetime import date, datetime, time, timedelta
from dataclasses import dataclass
from typing import Any
@dataclass
class MicrowaveBase():
time: Any = time(0)
display = lambda self, t, m=0: ['_' + t[1:], t[:-1] + '_'][m]
def parse_time(self, time):
m = int(time[:-1]) if 'm' in time else 0
s = int(time[:-1]) if 's' in time else 0
return m, s
def set_time(self, duration):
m, s = list(map(int, duration.split(':')))
self.time = (datetime.combine(date(1,1,1), time(0)) + timedelta(minutes=m, seconds=s)).time()
def add_time(self, duration):
m, s = self.parse_time(duration)
add = (datetime.combine(date(1,1,1), self.time) + timedelta(minutes=m, seconds=s)).time()
if add >= time(1, 30): self.time = time(1, 30)
else: self.time = add
def del_time(self, duration):
m, s = self.parse_time(duration)
try: sub = (datetime.combine(date(1,1,1), self.time) - timedelta(minutes=m, seconds=s)).time()
except OverflowError: sub = time(0)
finally: self.time = sub
def show_time(self):
if self.time < time(1, 0): return self.time.strftime('%M:%S')
else: return f'{self.time.minute+self.time.hour*60}:' + self.time.strftime('%S')
class Microwave1(MicrowaveBase):
show_time = lambda self: self.display(super().show_time())
class Microwave2(MicrowaveBase):
show_time = lambda self: self.display(super().show_time(), 1)
class Microwave3(MicrowaveBase):
pass
@dataclass
class RemoteControl:
microwave: Any
set_time = lambda self, duration: self.microwave.set_time(duration)
add_time = lambda self, duration: self.microwave.add_time(duration)
del_time = lambda self, duration: self.microwave.del_time(duration)
show_time = lambda self: self.microwave.show_time()
Oct. 11, 2020
Comments: