-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimers.py
35 lines (28 loc) · 866 Bytes
/
timers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import time
from misc import Some
class Timers:
def __init__(self):
self.timers = {}
def settimer(self, name, duration):
self.timers[name] = time.time() + duration
def setfirst(self, name, duration):
if not name in self.timers:
self.settimer(name, duration)
def remaining(self, name):
if name in self.timers:
timer = self.timers[name]
return Some(int(timer - time.time()))
else:
return None
def expired(self, name):
remain = self.remaining(name)
if remain:
return remain.v < 0
else:
return False
def dec(self, name):
if name in self.timers:
self.timers[name] -= 1
def remove(self, name):
if name in self.timers:
del self.timers[name]