Array? Please help.

D

Dr. Pastor

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

Diez B. Roggisch

Dr. Pastor said:
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
 
G

George Sakkis

Diez said:
Dr. Pastor said:
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 ?
 
S

Scott David Daniels

Dr. Pastor said:
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.
 
D

Dr. Pastor

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

Scott David Daniels

Dr. Pastor said:
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
 
S

Scott David Daniels

Scott said:
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
(e-mail address removed)
 
D

Diez B. Roggisch

George said:
Diez said:
Dr. Pastor said:
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
 
G

George Sakkis

Diez said:
George said:
Diez said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Array? 1
Can't solve problems! please Help 0
Help please 8
Please help 2
Please, help me. 1
Help with array 4
Help with pointers 1
Need help again please 19

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top