Array?
George Sakkis
george.sakkis at gmail.com
Thu May 25 23:20:08 EDT 2006
Dr. Pastor wrote:
> I need a row of 127 bytes that I will use as a
> circular buffer. Into the bytes (at unspecified times)
> a mark (0<mark<128) will be written, one after the other.
> After some time the "buffer" will contain the last 127 marks
> and a pointer will point the next "usable" byte.
> What would be the Pythonic way to do the above?
> Thanks for any guidance.
This will do the trick:
import itertools as it
from array import array
class CircularBuffer(object):
def __init__(self, size=128):
self._buffer = array('c', it.repeat(chr(0),size))
self._next = 0
def putmark(self, mark):
self._buffer[self._next] = mark
self._next = (self._next+1) % len(self._buffer)
def __str__(self):
return '%s(%r)' % (self.__class__.__name__,
self._buffer.tostring())
if __name__ == '__main__':
import string
b = CircularBuffer()
for c in string.letters * 10:
b.putmark(c)
print b
HTH,
George
More information about the Python-list
mailing list