Skip to content

Conversation

@lff5
Copy link

@lff5 lff5 commented Nov 24, 2022

self.open_spi_handles looks like this: {7: 7}

calling reversed(self.open_spi_handles) throws:

Traceback (most recent call last):
File "", line 1, in
TypeError: 'dict' object is not reversible

self.open_spi_handles looks like this: {7: 7}

calling reversed(self.open_spi_handles) throws:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: 'dict' object is not reversible
"""Close all open pigpio SPI handles and stop pigpio connection
"""
for handle in reversed(self.open_spi_handles):
for handle in reversed(list(self.open_spi_handles.values())):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume that even before Python 3.6, the reversed() built-in-function would work without turning the dict.values() (or, rather dict.keys()) iterator into a list first.

Can you please check if the following works:
for handle in reversed(self.open_spi_handles.keys()):

@ul-gh
Copy link
Owner

ul-gh commented Nov 24, 2022

Hi,

thank you for looking into this!

You seem to be using a Python version before 3.6.

Since Python 3.6, Dictionaries are ordered by default and they also have a .__reversed__() method, which means you can run the built-in function reversed() on the dictionary itself:

Python 3.10.7 (main, Nov  2 2022, 18:49:29) [GCC 12.2.0]
In [1]: d = {"a": 4, "b": 5, "c": 6}

In [2]: d.__reversed__()
Out[2]: <dict_reversekeyiterator at 0x7f230e4e5530>

In [3]: list(reversed(d))
Out[3]: ['c', 'b', 'a']

In [4]: dict(reversed(d.items()))
Out[4]: {'c': 6, 'b': 5, 'a': 4}

https://docs.python.org/3/library/functions.html#reversed
https://softwaremaniacs.org/blog/2020/02/05/dicts-ordered/

I admit that for backwards-compatibility, your fix might still be helpful.
Can you please check that you can use the compatibility fix without the list() constructor like in the comment: #32 (comment)_

Please let me know if that works for you.

@lff5
Copy link
Author

lff5 commented Nov 25, 2022

Hi,

Running on Raspberry Pi 3 with old Raspbian GNU/Linux 10 (buster).
Python 3.7.3 (default, Jul 25 2020, 13:03:44)

strangely there is no reversed() function in dict object despite being > 3.7 version.
I tested, reversed(self.open_spi_handles.keys()) it also throws.

Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> reversed({"a": 1, "b": 2})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict' object is not reversible

>>> reversed({"a": 1, "b": 2}.keys())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object is not reversible

>>> reversed(list({"a": 1, "b": 2}.keys()))
<list_reverseiterator object at 0x7669cb90>
>>> 

I think i will just update the machine. Up to you if you want to make it backwards compatible.

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

Successfully merging this pull request may close these issues.

2 participants