Please explain the (list?) presented usig a colon : #11481
-
I am looking through an SPI TFT MicroPython driver and came upon the following Code (I am assuming it's a list, no?): ROTATE = {
0: 0x88,
90: 0xE8,
180: 0x48,
270: 0x28
} It is used to validate the rotation value given. But I dont understand the list's structure. What are the hex values for? They don't seem to serve any purpose. The list is used as follows: if rotation not in self.ROTATE.keys():
raise RuntimeError('Rotation must be 0, 90, 180 or 270.')
else:
self.rotation = self.ROTATE[rotation] are the 0, 90, 180, and 270 values what ".keys" returns? Or does it return the hex values, and that's what "rotation" must contain? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
The value assigned to ROTATE is a dictionary. That consists of a sequence of key:value pairs. |
Beta Was this translation helpful? Give feedback.
-
Also that code can be improved. The statement self.rotation = self.ROTATE.get(rotation)
if self.rotation is None:
raise RuntimeError(f'Rotation must be one of: {self.ROTATE.keys()}) Note also that I removed the second hard-coding of the ROTATE values which you had in the error text. |
Beta Was this translation helpful? Give feedback.
The value assigned to ROTATE is a dictionary. That consists of a sequence of key:value pairs.
ROTATE.keys() returns the list of keys. ROTATE][key] retrieves the value of the respective key:value pair. For more information about dictionaries please consult any Python book.