<commit_before>import pingo

import time
import threading


class Led(object):
    """A single LED"""

    def __init__(self, pin, lit_state=pingo.HIGH):
        """Set lit_state to pingo.LOW to turn on led by bringing
           cathode to low state.

        :param lit_state: use pingo.HI for anode control, pingo.LOW
                          for cathode control
        """

        pin.mode = pingo.OUT
        self.pin = pin
        self.lit_state = lit_state
        self.blink_task = None

    def on(self):
        if self.lit_state == pingo.HIGH:
            self.pin.high()
        else:
            self.pin.low()

    def off(self):
        if self.lit_state == pingo.HIGH:
            self.pin.low()
        else:
            self.pin.high()

    @property
    def lit(self):
        return self.pin.state == self.lit_state

    @lit.setter
    def lit(self, new_state):
        if new_state:
            self.on()
        else:
            self.off()

    @property
    def blinking(self):
        return self.blink_task is not None and self.blink_task.active

    def toggle(self):
        self.pin.toggle()

    def blink(self, times=3, on_delay=.5, off_delay=None):
        """
        :param times: number of times to blink (0=forever)
        :param on_delay: delay while LED is on
        :param off_delay: delay while LED is off
        """
        if self.blinking:
            self.stop()
        self.blink_task = BlinkTask(self, times, on_delay, off_delay)
        threading.Thread(target=self.blink_task.run).start()

    def stop(self):
        """Stop blinking"""
        if self.blinking:
            self.blink_task.terminate()
            self.blink_task = None


PURE_COLORS = [
    ('RED', [1, 0, 0]),
    ('YELLOW', [1, 1, 0]),
    ('GREEN', [0, 1, 0]),
    ('CYAN', [0, 1, 1]),
    ('BLUE', [0, 0, 1]),
    ('PURPLE', [1, 0, 1]),
    ('WHITE', [1, 1, 1]),
    ('BLACK', [0, 0, 0]),
]


class RGBLed(object):

    pure_colors_map = dict(PURE_COLORS)

    def __init__(self, red_pin, green_pin, blue_pin,
                 lit_state=pingo.LOW):
        self._leds = [Led(red_pin, lit_state), Led(green_pin, lit_state),
                      Led(blue_pin, lit_state)]

        self.color = 'BLACK'

    @property
    def color(self):
        return self._color

    @color.setter
    def color(self, new_color):
        new_color = new_color.upper()
        if new_color in RGBLed.pure_colors_map:
            self._color = new_color
            states = RGBLed.pure_colors_map[new_color]
            for led, state in zip(self._leds, states):
                led.lit = state
        else:
            raise ValueError('Unknown color %s', new_color)

    def cycle(self, delay=.15):
        colors = PURE_COLORS[:6]  # exclude white and black
        for color, _ in colors:
            self.color = color
            time.sleep(delay)


class BlinkTask(object):

    def __init__(self, led, times, on_delay, off_delay):
        """
        :param led: Led instance to to blink
        :param times: number of times to blink (0=forever)
        :param on_delay: delay while LED is on
        :param off_delay: delay while LED is off
        """
        self.led = led
        self.active = True
        self.forever = times == 0
        self.times_remaining = times
        self.on_delay = on_delay
        self.off_delay = off_delay if off_delay is not None else on_delay
        self.led.off()

    def terminate(self):
        self.active = False

    def run(self):
        while self.active and (self.forever or self.times_remaining):
            self.led.toggle()
            if self.led.lit:
                time.sleep(self.on_delay)
                if not self.forever:
                    self.times_remaining -= 1
            else:
                time.sleep(self.off_delay)
        else:
            self.led.off()
            self.active = False


DIGIT_MAP = {
    0: '1111110',
    1: '0110000',
    2: '1101101',
    3: '1111001',
    4: '0110011',
    5: '1011011',
    6: '1011111',
    7: '1110000',
    8: '1111111',
    9: '1111011',
    10: '1110111',
    11: '0011111',
    12: '1001110',<commit_msg>Stop blinking shouldn't always switch off led<commit_after>import pingo

import time
import threading


class Led(object):
    """A single LED"""

    def __init__(self, pin, lit_state=pingo.HIGH):
        """Set lit_state to pingo.LOW to turn on led by bringing
           cathode to low state.

        :param lit_state: use pingo.HI for anode control, pingo.LOW
                          for cathode control
        """

        pin.mode = pingo.OUT
        self.pin = pin
        self.lit_state = lit_state
        self.blink_task = None

    def on(self):
        if self.lit_state == pingo.HIGH:
            self.pin.high()
        else:
            self.pin.low()

    def off(self):
        if self.lit_state == pingo.HIGH:
            self.pin.low()
        else:
            self.pin.high()

    @property
    def lit(self):
        return self.pin.state == self.lit_state

    @lit.setter
    def lit(self, new_state):
        if new_state:
            self.on()
        else:
            self.off()

    @property
    def blinking(self):
        return self.blink_task is not None and self.blink_task.active

    def toggle(self):
        self.pin.toggle()

    def blink(self, times=3, on_delay=.5, off_delay=None):
        """
        :param times: number of times to blink (0=forever)
        :param on_delay: delay while LED is on
        :param off_delay: delay while LED is off
        """
        if self.blinking:
            self.stop()
        self.blink_task = BlinkTask(self, times, on_delay, off_delay)
        threading.Thread(target=self.blink_task.run).start()

    def stop(self):
        """Stop blinking"""
        if self.blinking:
            self.blink_task.terminate()
            self.blink_task = None


PURE_COLORS = [
    ('RED', [1, 0, 0]),
    ('YELLOW', [1, 1, 0]),
    ('GREEN', [0, 1, 0]),
    ('CYAN', [0, 1, 1]),
    ('BLUE', [0, 0, 1]),
    ('PURPLE', [1, 0, 1]),
    ('WHITE', [1, 1, 1]),
    ('BLACK', [0, 0, 0]),
]


class RGBLed(object):

    pure_colors_map = dict(PURE_COLORS)

    def __init__(self, red_pin, green_pin, blue_pin,
                 lit_state=pingo.LOW):
        self._leds = [Led(red_pin, lit_state), Led(green_pin, lit_state),
                      Led(blue_pin, lit_state)]

        self.color = 'BLACK'

    @property
    def color(self):
        return self._color

    @color.setter
    def color(self, new_color):
        new_color = new_color.upper()
        if new_color in RGBLed.pure_colors_map:
            self._color = new_color
            states = RGBLed.pure_colors_map[new_color]
            for led, state in zip(self._leds, states):
                led.lit = state
        else:
            raise ValueError('Unknown color %s', new_color)

    def cycle(self, delay=.15):
        colors = PURE_COLORS[:6]  # exclude white and black
        for color, _ in colors:
            self.color = color
            time.sleep(delay)


class BlinkTask(object):

    def __init__(self, led, times, on_delay, off_delay):
        """
        :param led: Led instance to to blink
        :param times: number of times to blink (0=forever)
        :param on_delay: delay while LED is on
        :param off_delay: delay while LED is off
        """
        self.led = led
        self.led_pin_state_initial = self.led.pin.state
        self.active = True
        self.forever = times == 0
        self.times_remaining = times
        self.on_delay = on_delay
        self.off_delay = off_delay if off_delay is not None else on_delay
        self.led.off()

    def terminate(self):
        self.active = False

    def run(self):
        while self.active and (self.forever or self.times_remaining):
            self.led.toggle()
            if self.led.lit:
                time.sleep(self.on_delay)
                if not self.forever:
                    self.times_remaining -= 1
            else:
                time.sleep(self.off_delay)
        else:
            self.led.pin.state = self.led_pin_state_initial
            self.active = False


DIGIT_MAP = {
    0: '1111110',
    1: '0110000',
    2: '1101101',
    3: '1111001',
    4: '0110011',
    5: '1011011',
    6: '1011111',
    7: '1110000',
    8: '1111111',
    9: '1111011',
    10: '1110111',
    11: '0011111',
    12: '1001110',
{'fraction_non_alphanumeric': 0.11683074368402324, 'fraction_numerical': 0.0332107697781995, 'mean_word_length': 1.820066889632107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3992649', 'n_tokens_mistral': 2954, 'n_tokens_neox': 2706, 'n_words': 708}