def __setitem__(self, key, value): # check boundaries to minimiza duplicate references while len(self.queue) and self.queue[0][0] == key: # item at left end of queue pop it since it'll be appended # to right self.queue.popleft() while len(self.queue) and self.queue[-1][0] == key: # item at right end of queue pop it since it'll be # appended again self.queue.pop() if len(self) >= self.maxsize: self.cull() self.queue.append((key, value)) WeakValueDictionary.__setitem__(self, key, value)
def __setitem__(self, key, value): # check boundaries to minimiza duplicate references while len(self.queue) and self.queue[0][0] == key: # item at left end of queue pop it since it'll be appended # to right self.queue.popleft() while len(self.queue) and self.queue[-1][0] == key: # item at right end of queue pop it since it'll be # appended again self.queue.pop() if len(self) >= self.maxsize: self.cull() self.queue.append((key, value)) WeakValueDictionary.__setitem__(self, key, value)
def __setitem__(self, key, value): # check boundaries to minimiza duplicate references while len(self.queue) and self.queue[0][0] == key: # item at left end of queue pop it since it'll be appended # to right self.queue.popleft() while len(self.queue) and self.queue[-1][0] == key: # item at right end of queue pop it since it'll be # appended again self.queue.pop() if len(self) >= self.maxsize: # maximum cache size exceeded, cull old items # # note queue is the real cache but its size is boundless # since it might have duplicate references. # # don't bother culling if queue is smaller than weakref, # this means there are too many references outside the # cache, culling won't free much memory (if any). while len(self) >= self.maxsize <= len(self.queue): cullsize = max(int(len(self.queue) / self.cullsize), 2) try: for i in range(cullsize): self.queue.popleft() except IndexError: # queue is empty, bail out. #FIXME: should we force garbage collection here too? break # call garbage collecter manually since objects # with circular references take some time to get # collected for i in range(5): if gc.collect() == 0: break self.queue.append((key, value)) WeakValueDictionary.__setitem__(self, key, value)
def __setitem__(self, key, value): # check boundaries to minimiza duplicate references while len(self.queue) and self.queue[0][0] == key: # item at left end of queue pop it since it'll be appended # to right self.queue.popleft() while len(self.queue) and self.queue[-1][0] == key: # item at right end of queue pop it since it'll be # appended again self.queue.pop() if len(self) >= self.maxsize: # maximum cache size exceeded, cull old items # # note queue is the real cache but its size is boundless # since it might have duplicate references. # # don't bother culling if queue is smaller than weakref, # this means there are too many references outside the # cache, culling won't free much memory (if any). while len(self) >= self.maxsize <= len(self.queue): cullsize = max(int(len(self.queue) / self.cullsize), 2) try: for i in range(cullsize): self.queue.popleft() except IndexError: # queue is empty, bail out. #FIXME: should we force garbage collection here too? break # call garbage collecter manually since objects # with circular references take some time to get # collected for i in range(5): if gc.collect() == 0: break self.queue.append((key, value)) WeakValueDictionary.__setitem__(self, key, value)
def __setitem__(self, k, v): WeakValueDictionary.__setitem__(self, k, v) self.keep_this(v)
def __setitem__(self, k, v): WeakValueDictionary.__setitem__(self, k, v) self.keep_this(v)
class thread_container(blist): ''' Base class for thread containers. This container requires "hints" in order to work. Okay, it requires more than hints. It needs every conversation container to list its conversaton id. To be clear, thread_containers are used to merge related conversations into the same object, and are used to contain all emails/conversations in a folder or those found in a query. The thread_container holds the conversations inside itself (it is a list) and holds metadata that is used to instantly find related conversations in self._map when adding new messages to the container. While Jamie Zawinski makes some excellent arguments against storing which message belongs to which conversation, doing threading his way requires either loading every message into ram in order to find every message that goes in a conversation, or doing dozens of searches until everything we find everything. This eats up lots of ram unfortunately. :( ''' __slots__ = ('_map') #__metaclass__ = MetaSuper def __init__(self): #self._map = lazy_refmap(self, 'nique_terms') #self._map = {} self._map = WeakValueDictionary() def datesort(self): ''' Sort conversations so newest are at the top. ''' self.sort(key=attrgetter('last_update'), reverse=True) def __getitem__(self, idx): ''' If the key we're given is an integer, what's being asked for is a message at index idx. Otherwise we've been given a string and are being asked to look up something in the lookup table instead. ''' try: idx.__int__ except AttributeError: return self._map[idx] else: return super(thread_container, self).__getitem__(idx) #else: return self.__super.__getitem__(idx) def __setitem__(self, idx, value): try: idx.__int__ except AttributeError: return self._map.__setitem__(idx, value) else: return super(thread_container, self).__setitem__(idx, value) #else: return self.__super.__setitem__(idx, value) def __delitem__(self, idx): try: idx.__int__ except AttributeError: return self._map.__delitem__(idx) else: return super(thread_container, self).__delitem__(idx) #else: return self.__super.__delitem__(idx) #def append(self, item): # if type(item) is not conv_container: # raise TypeError('Wrong type of container. Use a conv_container instead of %s' % type(item)) # return list.append(self, item) def join(self, item): ''' To keep things lite and simple (translation: use the least amount of of ram possible while still keeping things fast), look conversations up only based upon their threadid. ''' if type(item) is conv_container: try: return self[item.thread].merge(item) except KeyError: self.append(item) self[item.thread] = item elif type(item) is msg_container: raise TypeError('Unable to thread that.') #return self.join(conv_container(item)) _thread = join def thread(self, msgs): #map(self._thread, threadmap.map(conv_factory, msgs) ) map(self._thread, (conv_factory(x) for x in msgs) ) self.datesort() return
def __setitem__(self, key, value): WeakValueDictionary.__setitem__(self, freeze(key), value)
def __setitem__(self, key, value): WeakValueDictionary.__setitem__(self, freeze(key), value)