<commit_before>import glue
import glue.message as msg


class Client(glue.HubListener):
    """
    Base class for interaction / visualization modules

    Attributes
    ----------
    data: DataCollection instance
        The data associated with this client.

    """

    def __init__(self, data=None):
        """
        Create a new client object.

        Parameters
        ----------
        data: Data, DataCollection, or list of data
            The primary data associated with this client.

        Raises
        ------
        TypeError: If the data input is the wrong type
        """
        super(Client, self).__init__()
        self._data = data
        if isinstance(data, glue.Data):
            self._data = glue.DataCollection(data)
        elif isinstance(data, list):
            self._data = glue.DataCollection(data)
        elif isinstance(data, glue.DataCollection):
            self._data = data
        else:
            raise TypeError("Input data must be a Data object, "
                            "list of Data, or DatCollection: %s"
                            % type(data))

    @property
    def data(self):
        """ Returns the data collection """
        return self._data

    def register_to_hub(self, hub):
        """The main method to establish a link with a hub,
        and set up event handlers. For common message types

        Client subclasses at a minimum should override these methods
        to provide functionality:
        _add_subset
        _update_subset
        _remove_subset
        _remove_data

        Clients can also override register_to_hub to add additional
        event handlers.

        Attributes
        ----------
        hub: The hub to subscribe to

        """
        dfilter = lambda x:x.sender.data in self._data
        dcfilter = lambda x:x.sender is self._data

        hub.subscribe(self,
                      msg.SubsetCreateMessage,
                      handler=self._add_subset,
                      filter=dfilter)
        hub.subscribe(self,
                      msg.SubsetUpdateMessage,
                      handler=self._update_subset,
                      filter=dfilter)
        hub.subscribe(self,
                      msg.SubsetDeleteMessage,
                      handler=self._remove_subset,
                      filter=dfilter)
        hub.subscribe(self,
                      msg.DataUpdateMessage,
                      handler=self._update_data,
                      filter=dfilter)
        hub.subscribe(self,
                      msg.DataCollectionDeleteMessage,
                      handler=self._remove_data,
                      filter=dcfilter)

    def _remove_data(self, message):
        raise NotImplementedError("_remove_data not implemented")

    def _remove_subset(self, message):
        raise NotImplementedError("_remove_data not implemented")

    def _update_data(self, message):
        """ Default handler for DataMessage """
        raise NotImplementedError("_update_data not implemented")

    def _update_subset(self, message):
        """ Default handler for SubsetUpdateMessage """
        raise NotImplementedError("_update_subset not implemented")

    def select(self):
        """
        General purpose function for selecting a subset
        """

        # Initialize a new empty subset
        subset = self.data.new_subset()

        # Here would be some code for (e.g. GUI) selection, which would
        # define some parameters for the selection, e.g a polygon. The client
        # then calls the following each time the GUI selection changes, and
        # until the user validates the selection:
        subset.modify()

        # Once the section is done, just leave the function

    def __setattr__(self, name, value):

        # Check if data has already been set
        if name == "data" and hasattr(self, "data"):
            raise AttributeError("Cannot modify client's data"
                                 " assignment after creation")

        object.__setattr__(self, name, value)
<commit_msg>Added _add_subset abstract method<commit_after>import glue
import glue.message as msg


class Client(glue.HubListener):
    """
    Base class for interaction / visualization modules

    Attributes
    ----------
    data: DataCollection instance
        The data associated with this client.

    """

    def __init__(self, data=None):
        """
        Create a new client object.

        Parameters
        ----------
        data: Data, DataCollection, or list of data
            The primary data associated with this client.

        Raises
        ------
        TypeError: If the data input is the wrong type
        """
        super(Client, self).__init__()
        self._data = data
        if isinstance(data, glue.Data):
            self._data = glue.DataCollection(data)
        elif isinstance(data, list):
            self._data = glue.DataCollection(data)
        elif isinstance(data, glue.DataCollection):
            self._data = data
        else:
            raise TypeError("Input data must be a Data object, "
                            "list of Data, or DatCollection: %s"
                            % type(data))

    @property
    def data(self):
        """ Returns the data collection """
        return self._data

    def register_to_hub(self, hub):
        """The main method to establish a link with a hub,
        and set up event handlers. For common message types

        Client subclasses at a minimum should override these methods
        to provide functionality:
        _add_subset
        _update_subset
        _remove_subset
        _remove_data

        Clients can also override register_to_hub to add additional
        event handlers.

        Attributes
        ----------
        hub: The hub to subscribe to

        """
        dfilter = lambda x:x.sender.data in self._data
        dcfilter = lambda x:x.sender is self._data

        hub.subscribe(self,
                      msg.SubsetCreateMessage,
                      handler=self._add_subset,
                      filter=dfilter)
        hub.subscribe(self,
                      msg.SubsetUpdateMessage,
                      handler=self._update_subset,
                      filter=dfilter)
        hub.subscribe(self,
                      msg.SubsetDeleteMessage,
                      handler=self._remove_subset,
                      filter=dfilter)
        hub.subscribe(self,
                      msg.DataUpdateMessage,
                      handler=self._update_data,
                      filter=dfilter)
        hub.subscribe(self,
                      msg.DataCollectionDeleteMessage,
                      handler=self._remove_data,
                      filter=dcfilter)

    def _add_subset(self, message):
        raise NotImplementedError("_add_subset not implemented")

    def _remove_data(self, message):
        raise NotImplementedError("_remove_data not implemented")

    def _remove_subset(self, message):
        raise NotImplementedError("_remove_data not implemented")

    def _update_data(self, message):
        """ Default handler for DataMessage """
        raise NotImplementedError("_update_data not implemented")

    def _update_subset(self, message):
        """ Default handler for SubsetUpdateMessage """
        raise NotImplementedError("_update_subset not implemented")

    def select(self):
        """
        General purpose function for selecting a subset
        """

        # Initialize a new empty subset
        subset = self.data.new_subset()

        # Here would be some code for (e.g. GUI) selection, which would
        # define some parameters for the selection, e.g a polygon. The client
        # then calls the following each time the GUI selection changes, and
        # until the user validates the selection:
        subset.modify()

        # Once the section is done, just leave the function

    def __setattr__(self, name, value):

        # Check if data has already been set
        if name == "data" and hasattr(self, "data"):
            raise AttributeError("Cannot modify client's data"
                                 " assignment after creation")

        object.__setattr__(self, name, value)

{'fraction_non_alphanumeric': 0.0757319138640213, 'fraction_numerical': 0.0, 'mean_word_length': 1.761189044756179, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1146811', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1923, 'n_words': 640}