Python circular buffer
Here is a simple circular buffer, or ring buffer, implementation in Python. It is a first-in, first-out (FIFO) buffer with a fixed size.
class RingBuffer:
def __init__(self, size):
self.data = [None for i in xrange(size)]
def append(self, x):
self.data.pop(0)
self.data.append(x)
def get(self):
return self.data
buf = RingBuffer(4)
for i in xrange(10):
buf.append(i)
print buf.get()
Here are the results:
[None, None, None, 0] [None, None, 0, 1] [None, 0, 1, 2] [0, 1, 2, 3] [1, 2, 3, 4] [2, 3, 4, 5] [3, 4, 5, 6] [4, 5, 6, 7] [5, 6, 7, 8] [6, 7, 8, 9]
References:
- http://mail.python.org/pipermail/python-dev/2003-April/thread.html#34790
- http://www.wayforward.net/pycontract/examples/circbuf.py
- http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68429
- http://docs.python.org/tut/node7.html#SECTION007120000000000000000
- http://docs.python.org/lib/module-Queue.html
- http://en.wikipedia.org/wiki/Circular_queue
Related posts
- How to sort a list of dicts in Python — posted 2010-04-02
- Python setdefault example — posted 2010-02-09
- How to conditionally replace items in a list — posted 2008-08-22
- How to use Python's enumerate and zip to iterate over two lists and their indices. — posted 2008-04-18
- How to invert a dict in Python — posted 2008-01-14
- How to find the intersection and union of two lists in Python — posted 2008-01-03
2
Comments
—
Comments feed for this post
#2 Jorge commented on 2011-10-12:
As a note to those coming here for a circular queue/buffer, you should use collections.deque with the maxlen parameter set.
"Poping" the first element of a list is a slow operation (linear with the lenght of the list), so you should avoid it. For d(ouble)e(nded)ques, it's fast (constant time).
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(5)
-
aws
(9)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(13)
-
conkeror
(2)
-
core
(2)
-
cygwin
(17)
-
datastructures
(14)
-
datetime
(4)
-
decorators
(4)
-
django
(40)
-
emacs
(22)
-
files_directories
(11)
-
git
(5)
-
hardware
(5)
-
install_setup
(8)
-
javascript
(3)
-
keyboard
(9)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
persistence
(5)
-
preferences
(7)
-
processes
(4)
-
pyqt
(18)
-
python
(144)
-
ratpoison
(3)
-
regexes
(6)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(14)
-
ssh
(10)
-
subversion
(6)
-
twisted
(7)
-
ubuntu
(65)
-
urxvt
(5)
-
vxworks
(25)
-
webdev
(5)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Marty Alchin
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 stopgo commented on 2011-01-18:
how do i print elements of buf.get?
I dont think Id be able to do
print buf.get[0] ?