Skip to content

Commit 48b9a55

Browse files
authored
Merge pull request #151 from RaspberryPiFoundation/adjust-pixel-length-checks
Adjust pixel length checks
2 parents d294277 + 61a0ee9 commit 48b9a55

2 files changed

Lines changed: 7 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Sense HAT
1+
# Sense HAT
22

33
Python module to control the `Raspberry Pi` Sense HAT used in the `Astro Pi` mission - an education outreach programme for UK schools sending code experiments to the International Space Station.
44

sense_hat/sense_hat.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ def set_pixels(self, pixel_list):
306306
and 255
307307
"""
308308

309-
if len(pixel_list) != 64:
309+
if not hasattr(pixel_list, '__len__') or len(pixel_list) != 64:
310310
raise ValueError('Pixel lists must have 64 elements')
311311

312312
for index, pix in enumerate(pixel_list):
313-
if len(pix) != 3:
313+
if not hasattr(pix, '__len__') or len(pix) != 3:
314314
raise ValueError('Pixel at index %d is invalid. Pixels must contain 3 elements: Red, Green and Blue' % index)
315315

316316
for element in pix:
@@ -351,16 +351,15 @@ def set_pixel(self, x, y, *args):
351351
ap.set_pixel(x, y, pixel)
352352
"""
353353

354-
pixel_error = 'Pixel arguments must be given as (r, g, b) or r, g, b'
355-
356354
if len(args) == 1:
357355
pixel = args[0]
358-
if len(pixel) != 3:
359-
raise ValueError(pixel_error)
360356
elif len(args) == 3:
361357
pixel = args
362358
else:
363-
raise ValueError(pixel_error)
359+
pixel = None
360+
361+
if not hasattr(pixel, '__len__') or len(pixel) != 3:
362+
raise ValueError('Pixel arguments must be given as (r, g, b) or r, g, b')
364363

365364
if x > 7 or x < 0:
366365
raise ValueError('X position must be between 0 and 7')

0 commit comments

Comments
 (0)