473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.
May 27 '06 #1
9 970
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
May 27 '06 #2
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 ?

May 27 '06 #3
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
sc***********@acm.org
May 27 '06 #4
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.

May 28 '06 #5
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
sc***********@acm.org
May 28 '06 #6
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
sc***********@acm.org
May 28 '06 #7
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.

May 28 '06 #8
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
May 28 '06 #9
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

May 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roy J | last post by:
Hello everyone:) My name is Roy, I am new to Java and I have problem regarding to arrays. if you have time and like to help, please help. I am trying to make a program to deal with prime...
0
by: Stephen | last post by:
I was wondering if someone could please help me with an array I'd like to create in an asp.net page. I have to design an array which stores the values of addresses manually entered into textboxes...
13
by: Kevin | last post by:
Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I
1
by: Stephen | last post by:
Was wondering if someone could please help me with an array I'd like to create in an asp.net page. I have to design an array which stores the values of addresses manually entered into textboxes...
33
by: Peace | last post by:
I am having trouble writing code to turn an array into a delimited string. Dim splitout As String splitout = Join(DataArray(rows, columns), " ") rows and columns are integers representing rows...
0
by: sebascomeau | last post by:
Hi everyone, Hello my name is Sebastien and I need some help. He have one week past and I search to do one thing but I can't. If someone help me, I'll be so happy :). My problem is I want to...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
by: Sudzzz | last post by:
Hi, I'm trying to convert a string something like this "{201,23,240,56,23,45,34,23}" into an array in C++ Please help. Thanks, Sudzzz
6
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
Hey guys, I've searched high and low for a way to populate a vb array with data from a javascript array. I can find 50 ways to do it with ASP but not one for VB. I appreciate what help you can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.