Diez B. Roggisch wrote: > George Sakkis schrieb: > > Diez B. Roggisch wrote: > >> Dr. Pastor schrieb: > >>> 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. > >>> (A pointer will point to the next byte to write to.) > >>> What would be the Pythonic way to do the above? > >>> Thanks for any guidance. > >> Use a list, use append and slicing on it: > >> > >> > >> max_size = 10 > >> buffer = [] > >> > >> for i in xrange(100): > >> buffer.append(i) > >> buffer[:] = buffer[-max_size:] > >> print buffer > >> > >> > >> Diez > > > > You're not serious about this, are you ? > > Tell me why I shouldn't. I presumed he's after a ringbuffer. Ok, the > above lacks a way to determine the amount of bytes added since the last > read. Add a counter if you want. And proper synchronization in case of a > multithreaded environment. But as the OP was sketchy about what he > actually needs, I thought that would at least give him a start. > > Maybe I grossly misunderstood his request. But I didn't see your better > implementation so far. So - enlighten me.
Strange; there are two threads on this and my reply was sent to the first one: http://tinyurl.com/lm2ho. In short, adding a new mark should be a simple O(1) operation, not an O(buf_size). This is textbook material, that's why I wasn't sure if you meant it. George -- http://mail.python.org/mailman/listinfo/python-list