Skip to content

deque object isn't iterable #4776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spacemanspiff2007 opened this issue May 10, 2019 · 12 comments
Closed

deque object isn't iterable #4776

spacemanspiff2007 opened this issue May 10, 2019 · 12 comments

Comments

@spacemanspiff2007
Copy link
Contributor

MicroPython v1.10 on 2019-01-25; ESP32 module with ESP32

from ucollections import deque

samples = deque((), 64)
samples.append(1)
samples.append(2)
for i in samples:
    print(i)

fails with TypeError: 'deque' object isn't iterable

print(dir(samples))

prints
['__class__', 'append', 'popleft']

Documentation:
Deques (double-ended queues) are a list-like container, however lists behave different:

test_list = [1,2,3]
print(dir(test_list))
for i in test_list:
    print(i)
['__class__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
1
2
3
@pfalcon
Copy link
Contributor

pfalcon commented May 10, 2019

If it's written that it's list-like, that doesn't mean that it behaves all like list, and docs are explicit in what functionality it provides. If you need list, use list. The purpose of objects like ucollections.deque is to do what lists can't do, in the minimal code size.

@spacemanspiff2007
Copy link
Contributor Author

But wouldn't it be useful to access the contents of the deque?
I would have expected that there is at least one possibility to access them.
The only possibility is to popleft all items and then append them again which makes no sense tbh.

@pbtsrc
Copy link

pbtsrc commented Jan 13, 2021

The collections.deque from CPython makes it very easy to implement a fast moving average for real-time data (for example, for averaging data from some sensor):

deq = collections.deque(window_size)
while true:
    sample = sensor.get_data()
    deq.append(sample)
    mov_aver = sum(deq) / len(deq)

So it would be very useful to make the ucollections.deque iterable also.

@davewood
Copy link

davewood commented Feb 6, 2021

I just ran into the exact same issue.

              return sum(self.queue)/len(self.queue)

@Spinnaker-design
Copy link

This would make deque much more useful

@peterhinch
Copy link
Contributor

The version here in micropython-lib is iterable. The class has an __iter__ method, and it works as expected:

>>> d = deque((5,1,9,2))
>>>  sum(d) 
17
>>> max(d)
9
>>> for n in d:
...     print(n)
... 
5
1
9
2
>>> 

@beetlegigg
Copy link

beetlegigg commented Sep 5, 2022

thanks @peterhinch for linking to the deque.py in the micropython-lib. An hour back I imported collections.deque on my rpi pico and got a different version with no iter. ( v1.19.1 on 2022-06-18). to use it for the first time. I'd be quite lost without this info. (very helpful indeed)
I hope that this version deque.py will replace the current lib on my pico when I next update it to a new version, but the date on the much improved deque.py seems to indicate is was created 16 months ago, so a bit mystified as to why I don't get this version on import with V1.19.1

@mattytrentini
Copy link
Contributor

For some background: There is a built-in version of deque written in C that supports append and popleft...and not much else. It's lean and fast but, obviously, doesn't have many features.

There's a more full-featured version of deque in micropython-lib that is written in Python and wraps up a list. micropython/micropython-lib#440 will add more features. This version is slower, takes more space but is becoming close to feature-complete.

The C built-in is packaged with each release of MicroPython but the MicroPython-lib version needs to be manually installed.

Moving forward, I'd like to see the MicroPython-lib version reach feature parity with CPython as well as having tests added. In parallel we should judiciously add more features to the built-in; taking care since there is a cost in terms of flash for every build.

Another alternative that has been discussed is to add deque as a native module. No work has started on this effort yet, to my knowledge.

@plumonito
Copy link

Ran into this issue as well trying to implement a running average :(

@peterhinch
Copy link
Contributor

I would consider a circular buffer for this task: it avoids repeated allocation.

@dpgeorge
Copy link
Member

Link to #10724.

@dpgeorge
Copy link
Member

As of 7dff38f, deque is iterable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants