-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory_store.py
50 lines (33 loc) · 1.06 KB
/
history_store.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding:utf-8 -*-
# Copyright (c) 2014,
# Karlsruhe Institute of Technology, Institute of Telematics
#
# This code is provided under the BSD 2-Clause License.
# Please refer to the LICENSE.txt file for further information.
#
# Author: Mario Hock
from collections import deque
class HistoryStore:
def __init__(self, history_size):
self.history_size = history_size
self.store = deque()
def push(self, element):
"""
Stores the new |element|.
The oldest element is popped from the store if |self.history_size| is exceeded.
In this case, the popped element is returned.
"""
self.store.append(element)
popped = None
if ( len(self.store) > self.history_size ):
popped = self.store.popleft()
return popped
def flush(self):
"""
Return all stored elements. (This removes the elements from the store.)
"""
ret = self.store
self.store = deque()
return ret
def size(self):
return len( self.store )