1

I want to do this with a circular buffer(that is x as deque)

i = 0
x = []

while True:
    accel_data = sensor.get_accel()
    d = datetime.utcnow().strftime('%Y-%m-%d')
    t = datetime.utcnow().strftime('%H:%M:%S.%f')
    x.append(accel_data + (d, t))
    i = i + 1

I know how to implement a simple circular buffer:

from collections import deque
import time

d = deque(maxlen=4)
bool = True
i = 1
y = 0
while bool:
    d.append(i)
    i = i + 1
    print(d)
    time.sleep(1) 

But I can’t use it to reproduce the first code.

1
  • 5
    Why can’t you reproduce it? Commented Sep 19, 2017 at 9:26

1 Answer 1

2

Does something like this work?

from collections import deque

container = deque(maxlen=4)
while True:
    accel_data = sensor.get_accel()
    curr_date = datetime.utcnow().strftime('%Y-%m-%d')
    curr_time = datetime.utcnow().strftime('%H:%M:%S.%f')
    entry = accel_data + (curr_date, curr_time)
    container.append(entry)
    print(container)  # this is not strictly necessary

A few tips:

  1. Use sensible names for the variables you are using.
  2. Do not initialize / declare variables you are not going to use.
  3. Be more specific in what you are not managing.
Sign up to request clarification or add additional context in comments.

9 Comments

"Do not declare variables you are not going to use." : i forgot to add few lines before "i=i+1": for y in range(4): print x[i][y]
Be careful in how you introduce that code though, as x from your code became container whose maximum length is fixed (4 in the example), so i will go out of range if you keep adding it.
mmm I couldn't try your code because I need my raspberry, but I think there has been a misunderstanding,anyway i will explain better my problem With first code I've an array ("x"),and in evry step of while I add all those information.So after 4 steps,for example it will be: 0.48 , 0.89 , 1.2 16/12/17 20.45.34 ==> x[0] -----------0.48 , 0.90 , 1.4 16/12/17 20.45.35 ==> x[1] ---------- 0.48 , 0.91 , 1.2 16/12/17 20.45.36 ==>x[2] ------0.48 , 0.94 , 1.2 16/12/17 20.45.37==> x[3]
So the "maxlen" is not referred to "j"( the elements in x[i],that are always 5),but to "i"
This code works also if you change container = deque(maxlen=4) with container = [] as you (ideally) originally had. The list version will let container grow by 1 at each iteration, hence if you can access container with an ever growing index like container[i] (assuming you have i += 1 or similar somewhere inside the loop and you had initialized i = 0 outside the loop). On the other hand, if container is now a deque, accessing it when i is larger than its max size, will cause the container[i] to get out of range for some value of i, regardless of what is in entry.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.