<commit_before>import cloudviz as cv

class DataCollection(object):
    """ DataCollections manage sets of data for Clients.

    In addition to storing data, DataCollections have an "active"
    object (either a data or subset object) that clients can use as
    the "editable" components of a user interaction.  DataCollections
    will broadcast messages when the active object is reassigned.
    """

    def __init__(self, data=None):
        self._active = None
        self.hub = None

        self._data = []
        if isinstance(data, cv.data.Data):
            self._data = [data]
            self._active = data

        elif isinstance(data, list):
            self._data = data
            self._active = data[0]


        self._links = []

    def add_link(self, link):
        self._links.append(link)
        if self.hub:
            link.register_to_hub(self.hub)

    def remove_link(self, link):
        if link not in self._links: return
        self._links.remove(link)
        self.hub.remove(link)

    def append(self, data):
        self._data.append(data)
        if self.hub:
            msg = cv.message.DataCollectionAddMessage(self, data)
            self.hub.broadcast(msg)
        if len(self._data) == 1: self._active = data

    def remove(self, data):
        if data not in self._data: return
        self._data.remove(data)
        if self.hub:
            msg = cv.message.DataCollectionDeleteMessage(self, data)
            self.hub.broadcast(msg)
        if data == self.active:
            self._active = None

    def get(self, index):
        if index < len(self._data):
            return self._data[index]
        raise IndexError("index is greater than number of data sets")

    def all_data(self):
        return self._data

    @property
    def active_data(self):
        return self._active.data if self._active else None

    @property
    def active(self):
        return self._active

    @active.setter
    def active(self, new):
        if isinstance(new, cv.Subset):
            if new not in [s for d in self._data for s in d.subsets]:
                raise TypeError("Object not in data collection: %s" % new)
        if isinstance(new, cv.Data):
            if new not in self._data:
                raise TypeError("Object not in data collection: %s" % new)

        changed = self.active != new

        old_data = self.active.data if self._active else None
        data = new.data
        self._active = new

        if changed and self.hub is not None:
            msg = cv.message.DataCollectionActiveChange(self)
            self.hub.broadcast(msg)

        if data != old_data and self.hub is not None:
            msg = cv.message.DataCollectionActiveDataChange(self)
            self.hub.broadcast(msg)


    def register_to_hub(self, hub):
        if not isinstance(hub, cv.Hub):
            raise TypeError("Input is not a Hub object: %s" % type(hub))
        self.hub = hub<commit_msg>Data collection re-parents hub reverences when adding data, registering, etc<commit_after>import cloudviz as cv

class DataCollection(object):
    """ DataCollections manage sets of data for Clients.

    In addition to storing data, DataCollections have an "active"
    object (either a data or subset object) that clients can use as
    the "editable" components of a user interaction.  DataCollections
    will broadcast messages when the active object is reassigned.
    """

    def __init__(self, data=None):
        self._active = None
        self.hub = None

        self._data = []
        if isinstance(data, cv.data.Data):
            self._data = [data]
            self._active = data

        elif isinstance(data, list):
            self._data = data
            self._active = data[0]


        self._links = []

    def add_link(self, link):
        self._links.append(link)
        if self.hub:
            link.register_to_hub(self.hub)

    def remove_link(self, link):
        if link not in self._links: return
        self._links.remove(link)
        self.hub.remove(link)

    def append(self, data):
        self._data.append(data)
        if self.hub:
            data.hub = self.hub
            for s in data.subsets:
                s.register()
            msg = cv.message.DataCollectionAddMessage(self, data)
            self.hub.broadcast(msg)
        if len(self._data) == 1: self._active = data

    def remove(self, data):
        if data not in self._data: return
        self._data.remove(data)
        if self.hub:
            msg = cv.message.DataCollectionDeleteMessage(self, data)
            self.hub.broadcast(msg)
        if data == self.active:
            self._active = None

    def get(self, index):
        if index < len(self._data):
            return self._data[index]
        raise IndexError("index is greater than number of data sets")

    def all_data(self):
        return self._data

    @property
    def active_data(self):
        return self._active.data if self._active else None

    @property
    def active(self):
        return self._active

    @active.setter
    def active(self, new):
        if isinstance(new, cv.Subset):
            if new not in [s for d in self._data for s in d.subsets]:
                raise TypeError("Object not in data collection: %s" % new)
        if isinstance(new, cv.Data):
            if new not in self._data:
                raise TypeError("Object not in data collection: %s" % new)

        changed = self.active != new

        old_data = self.active.data if self._active else None
        data = new.data
        self._active = new

        if changed and self.hub is not None:
            msg = cv.message.DataCollectionActiveChange(self)
            self.hub.broadcast(msg)

        if data != old_data and self.hub is not None:
            msg = cv.message.DataCollectionActiveDataChange(self)
            self.hub.broadcast(msg)

    def register_to_hub(self, hub):
        if not isinstance(hub, cv.Hub):
            raise TypeError("Input is not a Hub object: %s" % type(hub))
        self.hub = hub

        #re-assign all data, subset hub instances to this hub
        for d in self._data:
            d.hub = hub
            for s in d.subsets:
                s.register()
{'fraction_non_alphanumeric': 0.0900655895056791, 'fraction_numerical': 0.0006398976163813789, 'mean_word_length': 1.9785612196283944, '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': '7219193', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1712, 'n_words': 553}