collections.deque doesn't do anything useful

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
hlovatt
Posts: 68
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

collections.deque doesn't do anything useful

Post by hlovatt » Sat Aug 21, 2021 2:48 am

It looks like `deque` only has `append` and `popleft` and nothing else, therefore it doesn't do anything useful:

Code: Select all

MicroPython v1.16 on 2021-06-18; PYBD-SF2W with STM32F722IEK
Type "help()" for more information.
>>> from collections import deque
>>> d = deque((), 2)
>>> dir(d)
['__class__', 'append', 'popleft']
>>> d.append(1)
>>> d.popleft()
1
>>> d.append(2)
>>> d[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'deque' object isn't subscriptable
>>> d
<deque>
>>> for e in d:
...     print(e)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'deque' object isn't iterable
  1. You can't subscript it!
  2. The `__repr__` for a `deque` isn't very helpful, just says "<deque>"!
  3. You can't iterate it!

hlovatt
Posts: 68
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Re: collections.deque doesn't do anything useful

Post by hlovatt » Sat Aug 21, 2021 2:54 am

Re-reading my post I think I'm a bit harsh on deque; it does act as a FIFO, which is useful. However it is a long way from what you would expect a deque to do.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: collections.deque doesn't do anything useful

Post by pythoncoder » Sat Aug 21, 2021 9:19 am

See https://github.com/micropython/micropyt ... s/deque.py. There is nothing to stop you from extending its functionality further.
Peter Hinch
Index to my micropython libraries.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: collections.deque doesn't do anything useful

Post by mattyt » Sun Aug 22, 2021 1:25 am

This post reminded me that I had a minor patch to address some of the MicroPython-Lib deque shortcomings - so I've now opened a PR (that adds maxlen support). But I also want to add more deque features; feel free to help! :)

Post Reply