簡體   English   中英

如何在python中制作一個可暫停的計時器?

[英]How to make a pausable timer in python?

我想在 python 中創建一個具有以下功能的計時器:

timer.start() - 應該啟動計時器

timer.pause() - 應該暫停計時器

timer.resume() - 應該恢復計時器

timer.get() - 應該返回當前時間

計時器應該從 0 向上運行。 它用於測量時間,而不是觸發回調函數。

所以如果你啟動它,它應該開始像 0 1 2 3 這樣的秒數,如果你暫停它,它應該是靜止的 3,但不會更進一步。 在它恢復之后,它會繼續 4 5 6 等等

我怎樣才能做到這一點?


計時器的暫停/恢復功能不是重復的,因為我不關心回調。

# mytimer.py
from datetime import datetime
import time

class MyTimer():
    """
    timer.start() - should start the timer
    timer.pause() - should pause the timer
    timer.resume() - should resume the timer
    timer.get() - should return the current time
    """

    def __init__(self):
        print('Initializing timer')
        self.timestarted = None
        self.timepaused = None
        self.paused = False

    def start(self):
        """ Starts an internal timer by recording the current time """
        print("Starting timer")
        self.timestarted = datetime.now()

    def pause(self):
        """ Pauses the timer """
        if self.timestarted is None:
            raise ValueError("Timer not started")
        if self.paused:
            raise ValueError("Timer is already paused")
        print('Pausing timer')
        self.timepaused = datetime.now()
        self.paused = True

    def resume(self):
        """ Resumes the timer by adding the pause time to the start time """
        if self.timestarted is None:
            raise ValueError("Timer not started")
        if not self.paused:
            raise ValueError("Timer is not paused")
        print('Resuming timer')
        pausetime = datetime.now() - self.timepaused
        self.timestarted = self.timestarted + pausetime
        self.paused = False

    def get(self):
        """ Returns a timedelta object showing the amount of time
            elapsed since the start time, less any pauses """
        print('Get timer value')
        if self.timestarted is None:
            raise ValueError("Timer not started")
        if self.paused:
            return self.timepaused - self.timestarted
        else:
            return datetime.now() - self.timestarted

if __name__ == "__main__":
    t = MyTimer()
    t.start()
    print('Waiting 2 seconds'); time.sleep(2)
    print(t.get())
    print('Waiting 1 second'); time.sleep(1)
    t.pause()
    print('Waiting 2 seconds [paused]'); time.sleep(2)
    print(t.get())
    print('Waiting 1 second [paused]'); time.sleep(1)
    print(t.get())
    print('Waiting 1 second [paused]'); time.sleep(1)
    t.resume()
    print('Waiting 1 second'); time.sleep(1)
    print(t.get())

python mytimer.py

輸出

Initializing timer
Starting timer
Waiting 2 seconds
Get timer value
0:00:02.001523
Waiting 1 second
Pausing timer
Waiting 2 seconds [paused]
Get timer value
0:00:03.004724
Waiting 1 second [paused]
Get timer value
0:00:03.004724
Waiting 1 second [paused]
Resuming timer
Waiting 1 second
Get timer value
0:00:04.008578

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM