def __delitem__(self, key): with self.lock: try: self._dict.__delitem__(key) self._onCollectionChanges(CollectionChange.Remove(self, key)) except KeyError as ke: self._collectionChanges.on_error(ke)
def symmetric_difference_update(self, *args) -> None: """ Update an ObservableSet with the symmetric difference of itself and another. Publishes change notification """ with self.lock: self.check_disposed() self._set.symmetric_difference_update(*args) self._onCollectionChanges(CollectionChange.Extend(self, self))
def difference_update(self, *args) -> None: """ Remove all elements of another ObservableSet from this ObservableSet. Publishes change notifications """ with self.lock: self.check_disposed() self._set.difference_update(*args) self._onCollectionChanges(CollectionChange.Extend( self, self)) # Need better name...
def update(self, other=None): """ update the dictionary with (key, value) pairs from other Observable dictionary / dictionary, overwriting existing keys and publish Extend event """ with self.lock: self.check_disposed() if other is not None: self._dict.update(other) self._onCollectionChanges(CollectionChange.Extend(self, other))
def sort(self, key=None, reverse=False, suppress=False) -> None: """ sort the list in ascending / descending order and publishes the change notification if required. """ with self.lock: self.check_disposed() self._list.sort(key=key, reverse=reverse) if not suppress: self._onCollectionChanges( CollectionChange.IndexChanged(self, self._list))
def discard(self, element) -> None: """ Remove an element from a set if it is a member. Publishes change notification if an item is removed If the element is not a member, do nothing. """ with self.lock: self.check_disposed() if element in self._set: self._set.discard(element) self._onCollectionChanges( CollectionChange.Remove(self, element))
def remove(self, element) -> None: """ Remove an element from an ObservableSet; it must be a member. Publishes change notifications If the element is not a member, publishes KeyError to on_error channel. """ with self.lock: self.check_disposed() try: self._set.remove(element) self._onCollectionChanges( CollectionChange.Remove(self, element)) except KeyError as ke: self._collectionChanges.on_error(ke)
def pop(self): """ Remove and return an arbitrary ObservableSet element. Publishes change notifications Publishes KeyError to on_error channel if the set is empty.""" with self.lock: self.check_disposed() try: out = self._set.pop() self._onCollectionChanges(CollectionChange.Remove(self, out)) return out except KeyError as ke: self._collectionChanges.on_error(ke)
def popitem(self): """ remove and return an arbitrary item(key, value). Publishes Remove event upon successful removal or publishes KeyError to on_error if the Observable dictionary is empty """ with self.lock: self.check_disposed() try: element = self._dict.popitem() self._onCollectionChanges( CollectionChange.Remove(self, element)) return element except KeyError as ke: self._collectionChanges.on_error(ke)
def remove(self, item) -> None: """ remove first occurrence of the item and publishes the change notification. Publishes ValueError to on_error if the item is not present. """ with self.lock: self.check_disposed() try: self._list.remove(item) self._onCollectionChanges(CollectionChange.Remove(self, item)) except ValueError as ve: self._collectionChanges.on_error(ve) except Exception as ex: self._collectionChanges.on_error(ex)
def setdefault(self, key, default_value=None): """ if the given key is in Observable dictionary, return its values. If not, insert the key with the value and return the value (default=None). Only on addition of (key, value) events are published """ with self.lock: self.check_disposed() if default_value is None: result = self._dict.setdefault(key) else: result = self._dict.setdefault(key, default_value) if default_value == result: self._onCollectionChanges(CollectionChange.Add(self, result)) return result
def pop(self, key, value=None): """ remove the item with key and return its values or default if the key is not found. Publishes Remove event upon successful removal or publishes KeyError to on_error if the key is not found """ with self.lock: self.check_disposed() try: if value is None: element = self._dict.pop(key) else: element = self._dict.pop(key, value) self._onCollectionChanges( CollectionChange.Remove(self, element)) return element except KeyError as ke: self._collectionChanges.on_error(ke)
def clear(self): """ Remove all elements from this ObservableSet. Publishes change notifications """ with self.lock: self.check_disposed() self._set.clear() self._onCollectionChanges(CollectionChange.Clear(self))
def clear(self): """ removes all items from the Observable dictionary and publishes Clear event""" with self.lock: self.check_disposed() self._dict.clear() self._onCollectionChanges(CollectionChange.Clear(self))
def append(self, item) -> None: """ append the object to the end of the list and publishes an event to its subscribers. """ with self.lock: self.check_disposed() self._list.append(item) self._onCollectionChanges(CollectionChange.Add(self, item))
def update(self, items: Iterable) -> None: """ Update an ObservableSet with the union of itself and others. Publishes change notification """ with self.lock: self.check_disposed() self._set.update(items) self._onCollectionChanges(CollectionChange.Extend(self, items))
def add(self, element): """ Add an element to an ObservableSet. Publishes change notification """ with self.lock: self.check_disposed() self._set.add(element) self._onCollectionChanges(CollectionChange.Add(self, element))
def extend(self, items: Iterable) -> None: """ extend the list by appending elements from the iterable and publishes the change notification """ with self.lock: self.check_disposed() self._list.extend(items) self._onCollectionChanges(CollectionChange.Extend(self, items))
def clear(self) -> None: """ remove all the items from the list and publishes the change notification """ with self.lock: self.check_disposed() self._list.clear() self._onCollectionChanges(CollectionChange.Clear(self))
def insert(self, item, index) -> None: """ inserts the object in the specified index and publishes the change notification """ with self.lock: self.check_disposed() self._list.insert(index, item) self._onCollectionChanges(CollectionChange.Add(self, item))
def intersection_update(self, *args) -> None: """ Update an ObservableSet with the intersection of itself and another. Publishes change notifications """ with self.lock: self.check_disposed() self._set.intersection_update(*args) self._onCollectionChanges(CollectionChange.Extend(self, self))
def pop(self) -> None: """ remove the last index item from the list and publishes the change notification """ with self.lock: self.check_disposed() self._list.pop() self._onCollectionChanges(CollectionChange.Remove(self))