Source From Here
Question
I would like to know if there is a native datatype in Python that acts like a fixed-length FIFO buffer. For example, I want do create a length-5 FIFO buffer that is initialized with all zeros. Then, it might look like this:
Then, when I call the put function on the object, it will shift off the last zero and put the new value, say 1, into the left side:
If I put a 2, it would then shift and put to look like this:
...and so on. The new value goes at the front and the oldest one is shifted off. I understand that this would be very easy to implement myself, but I would like to use native python datatypes if at all possible. Does anyone know which datatype would be best for this?
How-To
See the docs for more about collections.deque; the method you call push is actually called appendleft in that type:
Note. The second parameter (maxlen, giving the maximum lengths) was added in Python 2.6; if you're using older versions of Python, it won't be available.
One simple implementation as below in case your Python version doesn't support above approach:
Demonstration:
Question
I would like to know if there is a native datatype in Python that acts like a fixed-length FIFO buffer. For example, I want do create a length-5 FIFO buffer that is initialized with all zeros. Then, it might look like this:
Then, when I call the put function on the object, it will shift off the last zero and put the new value, say 1, into the left side:
If I put a 2, it would then shift and put to look like this:
...and so on. The new value goes at the front and the oldest one is shifted off. I understand that this would be very easy to implement myself, but I would like to use native python datatypes if at all possible. Does anyone know which datatype would be best for this?
How-To
See the docs for more about collections.deque; the method you call push is actually called appendleft in that type:
Note. The second parameter (maxlen, giving the maximum lengths) was added in Python 2.6; if you're using older versions of Python, it won't be available.
One simple implementation as below in case your Python version doesn't support above approach:
- class FQueue(object):
- def __init__(self, l=2):
- self.l = l
- self.data = []
- def put(self, v):
- r'''
- Put element into queue
- '''
- ov = v
- if len(self.data) >= self.l:
- ov = self.data.pop(0)
- self.data.append(v)
- return self
- def push(self, v):
- return self.put(v)
- def add(self, v):
- return self.put(v)
- def pop(self):
- if len(self.data) > 0:
- return self.data.pop(0)
- else:
- return None
- def clear(self):
- r'''
- Remove all element(s)
- '''
- self.data.clear()
- def size(self):
- r'''
- Return the size of queue
- '''
- return len(self.data)
- def full(self):
- r'''
- Return True if the queue is full
- '''
- return len(self.data) >= self.l
- def tstr(self, sep='_'):
- r'''
- Return the string representation
- '''
- data = [str(e) for e in self.data]
- return sep.join(data)
- def __str__(self):
- return self.tstr()
- def __repr__(self):
- return self.tstr()
沒有留言:
張貼留言