From 8edbf4cffce47904b5102f8e3572cc4c657684ea Mon Sep 17 00:00:00 2001 From: s-light Date: Sun, 25 Nov 2018 00:15:43 +0100 Subject: [PATCH 1/2] added __getitem__ for iterable behavior --- adafruit_fancyled/adafruit_fancyled.py | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/adafruit_fancyled/adafruit_fancyled.py b/adafruit_fancyled/adafruit_fancyled.py index 83b2b5b..aa5b4c1 100644 --- a/adafruit_fancyled/adafruit_fancyled.py +++ b/adafruit_fancyled/adafruit_fancyled.py @@ -114,6 +114,21 @@ def __repr__(self): def __str__(self): return "(%s, %s, %s)" % (self.red, self.green, self.blue) + def __len__(self): + """Retrieve total number of color-parts available.""" + return 3 + + def __getitem__(self, key): + """Retrieve red, green or blue value as iterable.""" + if key is 0: + return self.red + elif key is 1: + return self.green + elif key is 2: + return self.blue + else: + raise IndexError + def pack(self): """'Pack' a `CRGB` color into a 24-bit RGB integer. @@ -164,6 +179,21 @@ def __repr__(self): def __str__(self): return "(%s, %s, %s)" % (self.hue, self.saturation, self.value) + def __len__(self): + """Retrieve total number of 'color-parts' available.""" + return 3 + + def __getitem__(self, key): + """Retrieve hue, saturation or value as iterable.""" + if key is 0: + return self.hue + elif key is 1: + return self.saturation + elif key is 2: + return self.value + else: + raise IndexError + def pack(self): """'Pack' a `CHSV` color into a 24-bit RGB integer. From afb1d9bd9d6e73c3c6bd823ca4b77a7597cd5cae Mon Sep 17 00:00:00 2001 From: s-light Date: Sun, 25 Nov 2018 00:25:49 +0100 Subject: [PATCH 2/2] fix comparison --- adafruit_fancyled/adafruit_fancyled.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_fancyled/adafruit_fancyled.py b/adafruit_fancyled/adafruit_fancyled.py index aa5b4c1..6ce7f57 100644 --- a/adafruit_fancyled/adafruit_fancyled.py +++ b/adafruit_fancyled/adafruit_fancyled.py @@ -120,11 +120,11 @@ def __len__(self): def __getitem__(self, key): """Retrieve red, green or blue value as iterable.""" - if key is 0: + if key == 0: return self.red - elif key is 1: + elif key == 1: return self.green - elif key is 2: + elif key == 2: return self.blue else: raise IndexError @@ -185,11 +185,11 @@ def __len__(self): def __getitem__(self, key): """Retrieve hue, saturation or value as iterable.""" - if key is 0: + if key == 0: return self.hue - elif key is 1: + elif key == 1: return self.saturation - elif key is 2: + elif key == 2: return self.value else: raise IndexError