Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Array? Please help.

Reply
Thread Tools

Array? Please help.

 
 
Dr. Pastor
Guest
Posts: n/a
 
      05-27-2006
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<12 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.
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      05-27-2006
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<12 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
 
Reply With Quote
 
 
 
 
George Sakkis
Guest
Posts: n/a
 
      05-27-2006
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<12 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 ?

 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      05-27-2006
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<12 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Dr. Pastor
Guest
Posts: n/a
 
      05-28-2006
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<12 will be written, one after the other.
>> After some time the "buffer" will contain the last 127 marks.

>
>
> Sounds a lot like homework.
>

 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      05-28-2006
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<12 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
(E-Mail Removed)
 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      05-28-2006
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<12 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
(E-Mail Removed)
 
Reply With Quote
 
Dr. Pastor
Guest
Posts: n/a
 
      05-28-2006
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<12 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.

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      05-28-2006
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<12 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
 
Reply With Quote
 
George Sakkis
Guest
Posts: n/a
 
      05-29-2006
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<12 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please please please help this guy with his open source java app casioculture@gmail.com Java 4 05-05-2005 08:24 AM
Console profile for Windows app in VC++ - PLEASE PLEASE PLEASE HELP! MuZZy C++ 7 01-07-2005 08:40 PM
Computer problems please please please help Nick Computer Support 0 06-04-2004 08:49 PM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments