Array? Please help.

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

    #1

    Array? Please help.

    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.
  • Diez B. Roggisch

    #2
    Re: Array? Please help.

    Dr. Pastor schrieb:[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.
    > (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.[/color]

    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

    Comment

    • George Sakkis

      #3
      Re: Array? Please help.

      Diez B. Roggisch wrote:[color=blue]
      > Dr. Pastor schrieb:[color=green]
      > > 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.[/color]
      >
      > 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[/color]

      You're not serious about this, are you ?

      Comment

      • Scott David Daniels

        #4
        Re: Array? Please help.

        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.[/color]

        Sounds a lot like homework.

        --
        --Scott David Daniels
        scott.daniels@a cm.org

        Comment

        • Dr. Pastor

          #5
          Re: Array? Please help.

          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:
          [color=blue]
          > Dr. Pastor wrote:
          >[color=green]
          >> 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.[/color]
          >
          >
          > Sounds a lot like homework.
          >[/color]

          Comment

          • Scott David Daniels

            #6
            Re: Array? Please help.

            Dr. Pastor wrote:[color=blue]
            > Scott David Daniels wrote:[color=green]
            >> Dr. Pastor wrote:[color=darkred]
            >>> 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.[/color]
            >> Sounds a lot like homework.[/color]
            > No it is not home work.[/color]

            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(resul t):
            if v:
            return result[n :]
            return result[: 0] # an empty array
            --
            --Scott David Daniels
            scott.daniels@a cm.org

            Comment

            • Scott David Daniels

              #7
              Re: Array? Please help.

              Scott David Daniels wrote:[color=blue]
              > Dr. Pastor wrote:[color=green]
              >> Scott David Daniels wrote:[color=darkred]
              >>> 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.[/color]
              >> No it is not home work.[/color]
              >
              > OK, taking you at your word, here's one way:
              > (and some untested code)[/color]

              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(resul t):
              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.daniels@a cm.org

              Comment

              • Dr. Pastor

                #8
                Re: Array? Please help.

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

                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.
                > (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.[/color]

                Comment

                • Diez B. Roggisch

                  #9
                  Re: Array? Please help.

                  George Sakkis schrieb:[color=blue]
                  > Diez B. Roggisch wrote:[color=green]
                  >> Dr. Pastor schrieb:[color=darkred]
                  >>> 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.[/color]
                  >> 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[/color]
                  >
                  > You're not serious about this, are you ?[/color]

                  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

                  Comment

                  • George Sakkis

                    #10
                    Re: Array? Please help.

                    Diez B. Roggisch wrote:
                    [color=blue]
                    > George Sakkis schrieb:[color=green]
                    > > Diez B. Roggisch wrote:[color=darkred]
                    > >> 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[/color]
                    > >
                    > > You're not serious about this, are you ?[/color]
                    >
                    > 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.[/color]

                    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

                    Comment

                    Working...