<commit_before>"""Base class for implementing the event-driven programming paradigm.

.. codeauthor:: <NAME> <<EMAIL>>
.. codeauthor:: <NAME> <<EMAIL>>

"""
import abc
import copy
import Queue
import inspect
import threading
from abc import abstractmethod

QUEUE_TIMEOUT = 0.5


class CallbackHandler(object):
    """Abstract class to represent callback objects."""

    # Ensure abstract methods are redefined in sub-classes.
    __metaclass__ = abc.ABCMeta

    @abstractmethod
    def enqueue(self, data):
        """Virtual: Enqueue data to be processed by callback.

        Args:
            data (any): data to send to callback.

        Raises:
            NotImplementedError: Virtual function must be implemented by
                                 subclasses.

        """
        pass

    @abstractmethod
    def start(self):
        """Virtual: Start the callbacks' activity.

        Raises:
            NotImplementedError: Virtual function must be implemented by
                                 subclasses.

        """
        pass

    @abstractmethod
    def request_stop(self):
        """Virtual: Non-blocking signal to stop callback activity.

        Raises:
            NotImplementedError: Virtual function must be implemented by
                                 subclasses.

        """
        pass

    def stop(self):
        """Virtual: Blocking signal to stop thread on next iteration.

        Raises:
            NotImplementedError: Virtual function must be implemented by
                                 subclasses.

        """
        pass


class CallbackSynchronous(CallbackHandler):
    """Object for executing callbacks **synchronously**.

    Attributes:
        is_alive (boolean): Return whether the callback thread is alive
        is_stop_requested (boolean): Return whether the callback thread has
                                     been requested to stop

    """

    def __init__(self, callback):
        """Document the __init__ method at the class level."""

        super(CallbackSynchronous, self).__init__()
        self.__callback = callback
        self.__stop_requested = False
        self.__is_alive = False

    @property
    def is_alive(self):
        return self.__is_alive

    @property
    def is_stop_requested(self):
        return False

    def enqueue(self, data):
        """Enqueue data to be processed by callback.

        Args:
            data (any): data to send to callback.

        """

        if self.is_alive:
            self.__callback(data)

    def start(self):
        """Start the callbacks' activity.

        Returns:
            boolean: Returns ``True`` if the callback was started. If the
                     callback is already alive, the request is ignored and the
                     method returns ``False``.

        """

        if self.is_alive:
            return False
        else:
            self.__is_alive = True
            return True

    def request_stop(self):
        """Non-blocking signal to stop callback activity.

        Returns:

            boolean: Returns ``True`` if the request to stop was successful. If
                     the callback is already in a stopped state, the request is
                     ignored and the method returns ``False``.

        """

        if self.__is_alive:
            self.__is_alive = False
            return True
        else:
            return False

    def stop(self):
        """Blocking signal to stop callback activity.

        Returns:
            boolean: Returns ``True`` if the callback was stopped. If the
                     callback is already in a stopped state, the request is
                     ignored and the method returns ``False``.

        """

        return self.request_stop()


class CallbackAsynchronous(CallbackHandler):
    """Object for executing callbacks **asynchronously** on a thread.

    Attributes:
        is_alive (boolean): Return whether the callback thread is alive
        is_stop_requested (boolean): Return whether the callback thread has
                                     been requested to stop<commit_msg>event: self.__is_alive() changed to self.is_alive()<commit_after>"""Base class for implementing the event-driven programming paradigm.

.. codeauthor:: <NAME> <<EMAIL>>
.. codeauthor:: <NAME> <<EMAIL>>

"""
import abc
import copy
import Queue
import inspect
import threading
from abc import abstractmethod

QUEUE_TIMEOUT = 0.5


class CallbackHandler(object):
    """Abstract class to represent callback objects."""

    # Ensure abstract methods are redefined in sub-classes.
    __metaclass__ = abc.ABCMeta

    @abstractmethod
    def enqueue(self, data):
        """Virtual: Enqueue data to be processed by callback.

        Args:
            data (any): data to send to callback.

        Raises:
            NotImplementedError: Virtual function must be implemented by
                                 subclasses.

        """
        pass

    @abstractmethod
    def start(self):
        """Virtual: Start the callbacks' activity.

        Raises:
            NotImplementedError: Virtual function must be implemented by
                                 subclasses.

        """
        pass

    @abstractmethod
    def request_stop(self):
        """Virtual: Non-blocking signal to stop callback activity.

        Raises:
            NotImplementedError: Virtual function must be implemented by
                                 subclasses.

        """
        pass

    def stop(self):
        """Virtual: Blocking signal to stop thread on next iteration.

        Raises:
            NotImplementedError: Virtual function must be implemented by
                                 subclasses.

        """
        pass


class CallbackSynchronous(CallbackHandler):
    """Object for executing callbacks **synchronously**.

    Attributes:
        is_alive (boolean): Return whether the callback thread is alive
        is_stop_requested (boolean): Return whether the callback thread has
                                     been requested to stop

    """

    def __init__(self, callback):
        """Document the __init__ method at the class level."""

        super(CallbackSynchronous, self).__init__()
        self.__callback = callback
        self.__stop_requested = False
        self.__is_alive = False

    @property
    def is_alive(self):
        return self.__is_alive

    @property
    def is_stop_requested(self):
        return False

    def enqueue(self, data):
        """Enqueue data to be processed by callback.

        Args:
            data (any): data to send to callback.

        """

        if self.is_alive:
            self.__callback(data)

    def start(self):
        """Start the callbacks' activity.

        Returns:
            boolean: Returns ``True`` if the callback was started. If the
                     callback is already alive, the request is ignored and the
                     method returns ``False``.

        """

        if self.is_alive:
            return False
        else:
            self.__is_alive = True
            return True

    def request_stop(self):
        """Non-blocking signal to stop callback activity.

        Returns:

            boolean: Returns ``True`` if the request to stop was successful. If
                     the callback is already in a stopped state, the request is
                     ignored and the method returns ``False``.

        """

        if self.is_alive:
            self.__is_alive = False
            return True
        else:
            return False

    def stop(self):
        """Blocking signal to stop callback activity.

        Returns:
            boolean: Returns ``True`` if the callback was stopped. If the
                     callback is already in a stopped state, the request is
                     ignored and the method returns ``False``.

        """

        return self.request_stop()


class CallbackAsynchronous(CallbackHandler):
    """Object for executing callbacks **asynchronously** on a thread.

    Attributes:
        is_alive (boolean): Return whether the callback thread is alive
        is_stop_requested (boolean): Return whether the callback thread has
                                     been requested to stop
{'fraction_non_alphanumeric': 0.07096385542168675, 'fraction_numerical': 0.00048192771084337347, 'mean_word_length': 1.8148524923702951, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4467960', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1927, 'n_words': 741}