noiftimer.stopwatch
1import threading 2import time 3 4from printbuddies import print_in_place 5 6from noiftimer import Timer 7 8QUIT = False 9 10 11class Stopwatch: 12 def __init__(self): 13 self.quit = False 14 self.pause = False 15 self.timer = Timer(subsecond_resolution=False) 16 17 @property 18 def current_time(self) -> str: 19 return f" {self.timer.elapsed_str} " 20 21 def process_input(self): 22 value = input() 23 if value == "q": 24 self.quit = True 25 elif value == "r": 26 self.timer.reset() 27 elif self.timer.is_paused: 28 self.timer.unpause() 29 else: 30 self.timer.pause() 31 32 def intro(self): 33 lines = [ 34 "", 35 "Press enter to pause and unpause the timer.", 36 "Enter 'r' to restart the timer.", 37 "Enter 'q' to quit.", 38 ] 39 print(*lines, sep="\n") 40 41 def run(self): 42 input_thread = threading.Thread(target=self.process_input, daemon=True) 43 self.timer.start() 44 input_thread.start() 45 while input_thread.is_alive() and not self.quit: 46 if not self.timer.is_paused: 47 print_in_place(self.current_time) 48 time.sleep(1) 49 50 51def main(): 52 stopwatch = Stopwatch() 53 stopwatch.intro() 54 while not stopwatch.quit: 55 stopwatch.run() 56 57 58if __name__ == "__main__": 59 main()
class
Stopwatch:
12class Stopwatch: 13 def __init__(self): 14 self.quit = False 15 self.pause = False 16 self.timer = Timer(subsecond_resolution=False) 17 18 @property 19 def current_time(self) -> str: 20 return f" {self.timer.elapsed_str} " 21 22 def process_input(self): 23 value = input() 24 if value == "q": 25 self.quit = True 26 elif value == "r": 27 self.timer.reset() 28 elif self.timer.is_paused: 29 self.timer.unpause() 30 else: 31 self.timer.pause() 32 33 def intro(self): 34 lines = [ 35 "", 36 "Press enter to pause and unpause the timer.", 37 "Enter 'r' to restart the timer.", 38 "Enter 'q' to quit.", 39 ] 40 print(*lines, sep="\n") 41 42 def run(self): 43 input_thread = threading.Thread(target=self.process_input, daemon=True) 44 self.timer.start() 45 input_thread.start() 46 while input_thread.is_alive() and not self.quit: 47 if not self.timer.is_paused: 48 print_in_place(self.current_time) 49 time.sleep(1)
def
main():