-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeDecaySet.py
More file actions
63 lines (44 loc) · 1.81 KB
/
timeDecaySet.py
File metadata and controls
63 lines (44 loc) · 1.81 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from datetime import datetime
import time
class TimeDecaySet(object):
#a simple set that you initiate with a minutes value,
#that will only keep values that are younger than X minutes old.
#after X minutes, they will be removed from the set.
#set minute_decay = 0 for infinite persistence
def __init__(self, minute_decay=1):
self._minute_decay = minute_decay
self._list = []
def add(self, value):
#only add if not in set
if self.in_set(value):
return False
else:
#push value with unix timestamp
self._list.append({'val':value, \
'timestamp':time.mktime(datetime.now().timetuple())})
return True
def in_set(self, value):
self.remove_timed_out_values()
if (value in (x['val'] for x in self._list)):
return True
else:
return False
def remove_from_set(self, value):
self._list = [x for x in self._list if not x['val']==value]
def remove_timed_out_values(self):
#remove all expired values - internal function
#since they are appended chronologically, we can simply find
#the index where now-time>minutes and remove everything before that
index = 0
now = time.mktime(datetime.now().timetuple())
while (index<len(self._list) and ((now - self._list[index]['timestamp'])/60 > self._minute_decay)):
index = index + 1
#only remove values if our minute_decay value has been set to a positive value
if (self._minute_decay > 0):
self._list = self._list[index:]
def asList(self):
self.remove_timed_out_values()
return [x['val'] for x in self._list]
def size(self):
self.remove_timed_out_values()
return len(self._list)