<commit_before>from time import sleep
from threading import Lock

from RPi import GPIO

from .devices import GPIODeviceError, GPIODevice, GPIOThread


class OutputDeviceError(GPIODeviceError):
    pass


class OutputDevice(GPIODevice):
    """
    Generic GPIO Output Device (on/off).
    """
    def __init__(self, pin=None):
        super(OutputDevice, self).__init__(pin)
        GPIO.setup(pin, GPIO.OUT)

    def on(self):
        """
        Turns the device on.
        """
        GPIO.output(self.pin, True)

    def off(self):
        """
        Turns the device off.
        """
        GPIO.output(self.pin, False)


class DigitalOutputDevice(OutputDevice):
    """
    Generic Digital GPIO Output Device (on/off/blink/toggle/flash).
    """
    def __init__(self, pin=None):
        super(DigitalOutputDevice, self).__init__(pin)
        self._blink_thread = None
        self._lock = Lock()

    def on(self):
        """
        Turn the device on.
        """
        self._stop_blink()
        super(DigitalOutputDevice, self).on()

    def off(self):
        """
        Turn the device off.
        """
        self._stop_blink()
        super(DigitalOutputDevice, self).off()

    def blink(self, on_time=1, off_time=1):
        """
        Make the device turn on and off repeatedly in the background.

        on_time: 1
            Number of seconds on

        off_time: 1
            Number of seconds off
        """
        self._stop_blink()
        self._blink_thread = GPIOThread(
            target=self._blink_led, args=(on_time, off_time)
        )
        self._blink_thread.start()

    def toggle(self):
        """
        Reverse the state of the device.
        If it's on, turn it off; if it's off, turn it on.
        """
        with self._lock:
            if self.is_active:
                self.off()
            else:
                self.on()

    def flash(self, on_time=1, off_time=1, n=1):
        """
        Turn the device on and off a given number of times.

        on_time: 1
            Number of seconds on

        off_time: 1
            Number of seconds off

        n: 1
            Number of iterations
        """
        for i in range(n):
            self.on()
            sleep(on_time)
            self.off()
            if i+1 < n:  # don't sleep on final iteration
                sleep(off_time)

    def _stop_blink(self):
        if self._blink_thread:
            self._blink_thread.stop()
            self._blink_thread = None

    def _blink_led(self, on_time, off_time):
        while True:
            super(DigitalOutputDevice, self).on()
            if self._blink_thread.stopping.wait(on_time):
                break
            super(DigitalOutputDevice, self).off()
            if self._blink_thread.stopping.wait(off_time):
                break


class LED(DigitalOutputDevice):
    """
    An LED (Light Emmitting Diode) component.
    """
    def on(self):
        """
        Turn the LED on.
        """
        super(LED, self).on()

    def off(self):
        """
        Turn the LED off.
        """
        super(LED, self).off()

    def blink(self, on_time=1, off_time=1):
        """
        Make the LED turn on and off repeatedly in the background.

        on_time: 1
            Number of seconds on

        off_time: 1
            Number of seconds off
        """
        super(LED, self).blink()

    def toggle(self):
        """
        Reverse the state of the LED.
        If it's on, turn it off; if it's off, turn it on.
        """
        super(LED, self).toggle()<commit_msg>Ensure args are passed to flash()<commit_after>from time import sleep
from threading import Lock

from RPi import GPIO

from .devices import GPIODeviceError, GPIODevice, GPIOThread


class OutputDeviceError(GPIODeviceError):
    pass


class OutputDevice(GPIODevice):
    """
    Generic GPIO Output Device (on/off).
    """
    def __init__(self, pin=None):
        super(OutputDevice, self).__init__(pin)
        GPIO.setup(pin, GPIO.OUT)

    def on(self):
        """
        Turns the device on.
        """
        GPIO.output(self.pin, True)

    def off(self):
        """
        Turns the device off.
        """
        GPIO.output(self.pin, False)


class DigitalOutputDevice(OutputDevice):
    """
    Generic Digital GPIO Output Device (on/off/blink/toggle/flash).
    """
    def __init__(self, pin=None):
        super(DigitalOutputDevice, self).__init__(pin)
        self._blink_thread = None
        self._lock = Lock()

    def on(self):
        """
        Turn the device on.
        """
        self._stop_blink()
        super(DigitalOutputDevice, self).on()

    def off(self):
        """
        Turn the device off.
        """
        self._stop_blink()
        super(DigitalOutputDevice, self).off()

    def blink(self, on_time=1, off_time=1):
        """
        Make the device turn on and off repeatedly in the background.

        on_time: 1
            Number of seconds on

        off_time: 1
            Number of seconds off
        """
        self._stop_blink()
        self._blink_thread = GPIOThread(
            target=self._blink_led, args=(on_time, off_time)
        )
        self._blink_thread.start()

    def toggle(self):
        """
        Reverse the state of the device.
        If it's on, turn it off; if it's off, turn it on.
        """
        with self._lock:
            if self.is_active:
                self.off()
            else:
                self.on()

    def flash(self, on_time=1, off_time=1, n=1):
        """
        Turn the device on and off a given number of times.

        on_time: 1
            Number of seconds on

        off_time: 1
            Number of seconds off

        n: 1
            Number of iterations
        """
        for i in range(n):
            self.on()
            sleep(on_time)
            self.off()
            if i+1 < n:  # don't sleep on final iteration
                sleep(off_time)

    def _stop_blink(self):
        if self._blink_thread:
            self._blink_thread.stop()
            self._blink_thread = None

    def _blink_led(self, on_time, off_time):
        while True:
            super(DigitalOutputDevice, self).on()
            if self._blink_thread.stopping.wait(on_time):
                break
            super(DigitalOutputDevice, self).off()
            if self._blink_thread.stopping.wait(off_time):
                break


class LED(DigitalOutputDevice):
    """
    An LED (Light Emmitting Diode) component.
    """
    def on(self):
        """
        Turn the LED on.
        """
        super(LED, self).on()

    def off(self):
        """
        Turn the LED off.
        """
        super(LED, self).off()

    def blink(self, on_time=1, off_time=1):
        """
        Make the LED turn on and off repeatedly in the background.

        on_time: 1
            Number of seconds on

        off_time: 1
            Number of seconds off
        """
        super(LED, self).blink(on_time, off_time)

    def toggle(self):
        """
        Reverse the state of the LED.
        If it's on, turn it off; if it's off, turn it on.
        """
        super(LED, self).toggle()
{'fraction_non_alphanumeric': 0.10181465576949716, 'fraction_numerical': 0.00415570023548968, 'mean_word_length': 1.6456577500916088, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2820342', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2092, 'n_words': 593}