Array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr. Pastor

    #1

    Array?

    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.
  • George Sakkis

    #2
    Re: Array?

    Dr. Pastor wrote:
    [color=blue]
    > 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.[/color]

    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._buffe r)

    def __str__(self):
    return '%s(%r)' % (self.__class__ .__name__,
    self._buffer.to string())


    if __name__ == '__main__':
    import string
    b = CircularBuffer( )
    for c in string.letters * 10:
    b.putmark(c)
    print b


    HTH,
    George

    Comment

    Working...