import blinkypattern
import blinkylib.blinkycolor

class Gradient(blinkypattern.BlinkyPattern):
    def __init__(self, blinkytape, start_color, end_color):
        super(Gradient, self).__init__(blinkytape)
        self._start_color = start_color
        self._end_color = end_color

    def setup(self):
        super(Gradient, self).setup()
        pixels = self._make_rgb_gradient(self._start_color, self._end_color)
        self._blinkytape.set_pixels(pixels)
        self._blinkytape.update()

    def _make_rgb_gradient(self, start_color, end_color):
        pixel_count = self._blinkytape.pixel_count
        red_delta = self._make_delta(start_color.red, end_color.red, pixel_count)
        green_delta = self._make_delta(start_color.green, end_color.green, pixel_count)
        blue_delta = self._make_delta(start_color.blue, end_color.blue, pixel_count)
        pixels = []
        for index in range(0, pixel_count):
            red = start_color.red + (red_delta * index)
            green = start_color.green + (green_delta * index)
            blue = start_color.blue + (blue_delta * index)
            pixels.append(blinkylib.blinkycolor.BlinkyColor(red, green, blue))
        return pixels

    def _make_delta(self, start, end, step):
        return (end - start) / float(step)

import blinkypattern
import blinkylib.blinkycolor

class Gradient(blinkypattern.BlinkyPattern):
    def __init__(self, blinkytape, start_color, end_color):
        super(Gradient, self).__init__(blinkytape)
        self._pixels = self._make_rgb_gradient(start_color, end_color)

    def setup(self):
        super(Gradient, self).setup()
        self._blinkytape.set_pixels(self._pixels)
        self._blinkytape.update()

    def _make_rgb_gradient(self, start_color, end_color):
        pixel_count = self._blinkytape.pixel_count
        red_gradient = self._make_gradient(start_color.red, end_color.red, pixel_count)
        green_gradient = self._make_gradient(start_color.green, end_color.green, pixel_count)
        blue_gradient = self._make_gradient(start_color.blue, end_color.blue, pixel_count)
        rgb_gradient = zip(red_gradient, green_gradient, blue_gradient)
        return [blinkylib.blinkycolor.BlinkyColor(*rgb) for rgb in rgb_gradient]

    def _make_gradient(self, start, end, count):
        delta = (end - start) / float(count)
        return [start + (delta * index) for index in range(0, count)]

Refactor Gradient to clean up loops and redundancy
Refactor Gradient to clean up loops and redundancy

  import blinkypattern
  import blinkylib.blinkycolor
  
  class Gradient(blinkypattern.BlinkyPattern):
      def __init__(self, blinkytape, start_color, end_color):
          super(Gradient, self).__init__(blinkytape)
+         self._pixels = self._make_rgb_gradient(start_color, end_color)
-         self._start_color = start_color
-         self._end_color = end_color
  
      def setup(self):
          super(Gradient, self).setup()
-         pixels = self._make_rgb_gradient(self._start_color, self._end_color)
-         self._blinkytape.set_pixels(pixels)
+         self._blinkytape.set_pixels(self._pixels)
          self._blinkytape.update()
  
      def _make_rgb_gradient(self, start_color, end_color):
          pixel_count = self._blinkytape.pixel_count
-         red_delta = self._make_delta(start_color.red, end_color.red, pixel_count)
+         red_gradient = self._make_gradient(start_color.red, end_color.red, pixel_count)
-         green_delta = self._make_delta(start_color.green, end_color.green, pixel_count)
+         green_gradient = self._make_gradient(start_color.green, end_color.green, pixel_count)
-         blue_delta = self._make_delta(start_color.blue, end_color.blue, pixel_count)
+         blue_gradient = self._make_gradient(start_color.blue, end_color.blue, pixel_count)
+         rgb_gradient = zip(red_gradient, green_gradient, blue_gradient)
+         return [blinkylib.blinkycolor.BlinkyColor(*rgb) for rgb in rgb_gradient]
-         pixels = []
-         for index in range(0, pixel_count):
-             red = start_color.red + (red_delta * index)
-             green = start_color.green + (green_delta * index)
-             blue = start_color.blue + (blue_delta * index)
-             pixels.append(blinkylib.blinkycolor.BlinkyColor(red, green, blue))
-         return pixels
  
-     def _make_delta(self, start, end, step):
+     def _make_gradient(self, start, end, count):
-         return (end - start) / float(step)
+         delta = (end - start) / float(count)
+         return [start + (delta * index) for index in range(0, count)]
  
Refactor Gradient to clean up loops and redundancy
## Code Before:
import blinkypattern
import blinkylib.blinkycolor

class Gradient(blinkypattern.BlinkyPattern):
    def __init__(self, blinkytape, start_color, end_color):
        super(Gradient, self).__init__(blinkytape)
        self._start_color = start_color
        self._end_color = end_color

    def setup(self):
        super(Gradient, self).setup()
        pixels = self._make_rgb_gradient(self._start_color, self._end_color)
        self._blinkytape.set_pixels(pixels)
        self._blinkytape.update()

    def _make_rgb_gradient(self, start_color, end_color):
        pixel_count = self._blinkytape.pixel_count
        red_delta = self._make_delta(start_color.red, end_color.red, pixel_count)
        green_delta = self._make_delta(start_color.green, end_color.green, pixel_count)
        blue_delta = self._make_delta(start_color.blue, end_color.blue, pixel_count)
        pixels = []
        for index in range(0, pixel_count):
            red = start_color.red + (red_delta * index)
            green = start_color.green + (green_delta * index)
            blue = start_color.blue + (blue_delta * index)
            pixels.append(blinkylib.blinkycolor.BlinkyColor(red, green, blue))
        return pixels

    def _make_delta(self, start, end, step):
        return (end - start) / float(step)

## Instruction:
Refactor Gradient to clean up loops and redundancy
## Code After:
import blinkypattern
import blinkylib.blinkycolor

class Gradient(blinkypattern.BlinkyPattern):
    def __init__(self, blinkytape, start_color, end_color):
        super(Gradient, self).__init__(blinkytape)
        self._pixels = self._make_rgb_gradient(start_color, end_color)

    def setup(self):
        super(Gradient, self).setup()
        self._blinkytape.set_pixels(self._pixels)
        self._blinkytape.update()

    def _make_rgb_gradient(self, start_color, end_color):
        pixel_count = self._blinkytape.pixel_count
        red_gradient = self._make_gradient(start_color.red, end_color.red, pixel_count)
        green_gradient = self._make_gradient(start_color.green, end_color.green, pixel_count)
        blue_gradient = self._make_gradient(start_color.blue, end_color.blue, pixel_count)
        rgb_gradient = zip(red_gradient, green_gradient, blue_gradient)
        return [blinkylib.blinkycolor.BlinkyColor(*rgb) for rgb in rgb_gradient]

    def _make_gradient(self, start, end, count):
        delta = (end - start) / float(count)
        return [start + (delta * index) for index in range(0, count)]
