Array? Please help.

Discussion in 'Python' started by Dr. Pastor, May 27, 2006.

  1. Dr. Pastor

    Dr. Pastor Guest

    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.
    Dr. Pastor, May 27, 2006
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, May 27, 2006
    #2
    1. Advertising

  3. 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 ?
    George Sakkis, May 27, 2006
    #3
  4. 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.


    Sounds a lot like homework.

    --
    --Scott David Daniels
    Scott David Daniels, May 27, 2006
    #4
  5. Dr. Pastor

    Dr. Pastor Guest

    No it is not home work.
    (I have not did any home work for more than 50 years.)
    I am a beginner, and just do not see a really proper
    way to program the question.
    Thanks anyhow.

    Scott David Daniels wrote:

    > 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.

    >
    >
    > Sounds a lot like homework.
    >
    Dr. Pastor, May 28, 2006
    #5
  6. Dr. Pastor wrote:
    > Scott David Daniels wrote:
    >> 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.

    >> Sounds a lot like homework.

    > No it is not home work.


    OK, taking you at your word, here's one way:

    class Circular(object):
    def __init__(self):
    self.data = array.array('b', [0] * 127)
    self.point = len(self.data) - 1

    def mark(self, value):
    self.point += 1
    if self.point >= len(self.data):
    self.point = 0
    self.data[self.point] = value

    def recent(self):
    result = self.data[self.point :] + self.data[: self.point]
    for n, v in enumerate(result):
    if v:
    return result[n :]
    return result[: 0] # an empty array
    --
    --Scott David Daniels
    Scott David Daniels, May 28, 2006
    #6
  7. Scott David Daniels wrote:
    > Dr. Pastor wrote:
    >> Scott David Daniels wrote:
    >>> 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.
    >>> Sounds a lot like homework.

    >> No it is not home work.

    >
    > OK, taking you at your word, here's one way:
    > (and some untested code)


    As penance for posting untested code, here is a real implementation:

    import array

    class Circular(object):
    '''A circular buffer, holds only non-zero entries'''
    def __init__(self, size=127):
    '''Create as N-long circular buffer

    .data is the circular data store.
    .point is the index of the next value to write
    '''
    self.data = array.array('b', [0] * size)
    self.point = 0

    def mark(self, value):
    '''Add a single value (non-zero) to the buffer'''
    assert value
    self.data[self.point] = value
    self.point += 1
    if self.point >= len(self.data):
    self.point = 0

    def recent(self):
    '''Return the most recent values saved.'''
    result = self.data[self.point :] + self.data[: self.point]
    for n, v in enumerate(result):
    if v:
    return result[n :]
    return result[: 0] # an empty array

    Tests:
    c = Circular(3)
    assert list(c.recent()) == []
    c.mark(12)
    assert list(c.recent()) == [12]
    c.mark(11)
    assert list(c.recent()) == [12, 11]
    c.mark(10)
    assert list(c.recent()) == [12, 11, 10]
    c.mark(9)
    assert list(c.recent()) == [11, 10, 9]


    --Scott David Daniels
    Scott David Daniels, May 28, 2006
    #7
  8. Dr. Pastor

    Dr. Pastor Guest

    Many thanks to you all. (Extra thanks to Mr. Daniels.)

    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.
    > (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.
    Dr. Pastor, May 28, 2006
    #8
  9. 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.


    Diez
    Diez B. Roggisch, May 28, 2006
    #9
  10. 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
    George Sakkis, May 29, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    492
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    528
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,723
    Mike Hewson
    Jan 7, 2005
  4. kun niu
    Replies:
    2
    Views:
    343
    kun niu
    Jun 5, 2009
  5. mera
    Replies:
    7
    Views:
    1,081
Loading...

Share This Page