Skip to content

use core class slicing getter. Fix individual pixels mode #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions adafruit_pixelmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@ class PixelMap:

def __init__(self, strip, pixel_ranges, individual_pixels=False):
self._pixels = strip
if not isinstance(pixel_ranges, list) and not isinstance(pixel_ranges, tuple):
raise TypeError("pixel_ranges must be tuple or list")

if isinstance(pixel_ranges, list) or isinstance(pixel_ranges[0], list):
self._ranges = tuple(
tuple(item for item in sublist) for sublist in pixel_ranges
)
if not isinstance(pixel_ranges[0], int):
self._ranges = tuple(
tuple(item for item in sublist) for sublist in pixel_ranges
)
else:
self._ranges = tuple(pixel_ranges)

elif isinstance(pixel_ranges, tuple):
self._ranges = pixel_ranges
Expand All @@ -130,7 +136,7 @@ def _expand_ranges(self):
)
return
if isinstance(self._ranges[0], int):
self._ranges = [[n] for n in self._ranges]
self._ranges = tuple((n,) for n in self._ranges)

def __repr__(self):
return "[" + ", ".join([str(self[x]) for x in range(self.n)]) + "]"
Expand All @@ -148,10 +154,7 @@ def __setitem__(self, index, val):

def __getitem__(self, index):
if isinstance(index, slice):
out = []
for in_i in range(*index.indices(len(self._ranges))):
out.append(self._pixels[self._ranges[in_i][0]])
return out
return self._map[index]
if index < 0:
index += len(self)
if index >= self.n or index < 0:
Expand Down