def insert(self, index, item): self._items.insert(index, item) self.obs_list.notify.insert(index, item) def pop(self, index=-1): value = self._items.pop(index) self.obs_list.notify.pop(index, value) def remove(self, item): self.pop(self.index(item)) def append(self, item): self.insert(len(self), item) def __setitem__(self, index, new_value): old_value = self._items[index] self._items[index] = new_value self.obs_list.notify.replace(index, old_value, new_value) def __delitem__(self, index): self.pop(index) from lib.proxyclass import proxy_class List = proxy_class(List, '_items', methods=[ '__getitem__', '__len__', '__iter__', 'index', ])
if key not in self._dict: adding = True else: adding = False self._dict[key] = value if adding: self.obs_dict.notify.add_item(key, value) else: self.obs_dict.notify.set_item(key, value) def __delitem__(self, key): val = self._dict[key] del self._dict[key] self.obs_dict.notify.remove_item(key, val) Dict = proxy_class(Dict, '_dict', methods=[ '__contains__', '__getitem__', '__iter__', '__len__', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'values' ])
old_index = self._remove_item(key, old_value) new_index = self._add_item(key, new_value) if old_index == new_value: self.obs_list.notify.replace(old_index, (key, old_value), (key, new_value)) else: self.obs_list.notify.pop(old_index, (key, old_value)) self.obs_list.notify.insert(new_index, (key, new_value)) def _add_item(self, key, value): index = bisect.bisect_left(self._items, (key, value)) self._items.insert(index, (key, value)) return index def _remove_item(self, key, value): index = bisect.bisect_left(self._items, (key, value)) # TODO: Remove this if index >= len(self._items) or self._items[index] != (key, value): import pdb pdb.set_trace() assert self._items[index] == (key, value) self._items.pop(index) return index from lib.proxyclass import proxy_class SortedItems = proxy_class(SortedItems, '_items', methods=[ '__getitem__', '__len__', '__iter__', 'index', ])
def __init__(self, func, l): self.obs_list = Observable() self.func = func self._list = l self._cache = map(self.func, self._list) l.obs_list.add_observer(self, '_list_') def _list_insert(self, index, item): new_item = self.func(item) self._cache.insert(index, new_item) self.obs_list.notify.insert(index, new_item) def _list_pop(self, index, value): result = self._cache.pop(index) self.obs_list.notify.pop(index, result) def _list_replace(self, index, old_value, new_value): new_item = self.func(new_value) old_item = self._cache[index] self._cache[index] = new_item self.obs_list.notify.replace(index, old_item, new_item) from lib.proxyclass import proxy_class CacheMap = proxy_class(CacheMap, '_cache', methods=[ '__getitem__', '__len__', 'index', ])