IndexError: list index out of range
perrycannon opened this issue · 5 comments
Type of Raspberry Pi
I am using a Raspberry Pi B Plus and a Raspberry Pi Zero. Same error on each.
Linux Kernel version
5.4.51+ #1333 Mon Aug 10 16:38:02 BST 2020 armv6l GNU/Linux
Expected behaviour
This script gets the RSS feed from CNN. I cycle through to make one string of the 10 top headline titles. Save this as msg to print to the matrix led. The script has been running for 3 days with no issues. I have the same exception on the Raspberry Pi B Plus and Raspberry Pi Zero.
Actual behaviour
Traceback (most recent call last):
File "matrixTime.py", line 75, in <module>
demo(args.cascaded, args.block_orientation, args.rotate, args.reverse_order)
File "matrixTime.py", line 46, in demo
show_message(device, msg, fill="white", font=proportional(CP437_FONT), scroll_delay=0.1)
File "/usr/local/lib/python3.7/dist-packages/luma/core/legacy/__init__.py", line 81, in show_message
w, h = textsize(msg, font)
File "/usr/local/lib/python3.7/dist-packages/luma/core/legacy/__init__.py", line 29, in textsize
src = [c for ascii_code in txt for c in font[ord(ascii_code)]]
File "/usr/local/lib/python3.7/dist-packages/luma/core/legacy/__init__.py", line 29, in <listcomp>
src = [c for ascii_code in txt for c in font[ord(ascii_code)]]
File "/usr/local/lib/python3.7/dist-packages/luma/core/legacy/font.py", line 44, in __getitem__
bitmap = self.font[ascii_code]
IndexError: list index out of range
The script will of course stop after the exception. When I restart the script on both Raspberry Pi's, the exception occurs at the first instance of show_message for the CNN RSS string. The prior show_message in my script scrolls the date and time which works, no exception is returned.
I do not understand why the exception occurs?
the font you use probably doesn't contain all characters you're trying to display, hence the IndexError
?
@thijstriemstra we could probably throw a more user-friendly error message that indicates the character that is not supported in the font
Sounds good.
@perrycannon i had a look at your script, and it is likely that there are unicode characters that are not supported by the bitmap font. There is a font wrapper class called tolerant
which will substitute a replacement character if the relevant one is not found. Use as follows:
from luma.core.legacy.font import proportional, tolerant, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT
...
show_message(device, msg, fill="white", font=proportional(tolerant(CP437_FONT)), scroll_delay=0.1)
See the docs for more information: https://luma-core.readthedocs.io/en/latest/legacy.html#luma.core.legacy.font.tolerant