Re: dictionary that discards old items

2005-07-24 Thread Raymond Hettinger
[Raymond Hettinger] > >class Cache(dict): > >def __init__(self, n, *args, **kwds): > >self.n = n > >self.queue = collections.deque() > >dict.__init__(self, *args, **kwds) [Bengt Richter] > Minor comment: There is a potential name collision problem for keyword > n=som

Re: dictionary that discards old items

2005-07-23 Thread Bengt Richter
On Sat, 23 Jul 2005 20:07:25 -0600, Steven Bethard <[EMAIL PROTECTED]> wrote: >[Raymond Hettinger] >>>class Cache(dict): >>> def __init__(self, n, *args, **kwds): >>> self.n = n >>> self.queue = collections.deque() >>> dict.__init__(self, *args, **kwds) > >[Bengt Richter] >> Mi

Re: dictionary that discards old items

2005-07-23 Thread Steven Bethard
[Raymond Hettinger] >>class Cache(dict): >> def __init__(self, n, *args, **kwds): >> self.n = n >> self.queue = collections.deque() >> dict.__init__(self, *args, **kwds) [Bengt Richter] > Minor comment: There is a potential name collision problem for keyword > n=something, >

Re: dictionary that discards old items

2005-07-23 Thread Bengt Richter
On 21 Jul 2005 19:29:32 -0700, "Raymond Hettinger" <[EMAIL PROTECTED]> wrote: >[Will McGugan] >> I need a collection class that behaves like a dictionary but when it >> reaches 'n' items it discards the oldest item so that the length never >> goes above 'n'. (Its for caching search results) > > >i

Re: dictionary that discards old items

2005-07-22 Thread Michael Hoffman
Will McGugan wrote: >> You want a Least Recently Used, or LRU, cache. Here's one: >> > Thanks. I found the one I saw originally in the Python Cookbook. Only > they call it a FIFO cache. A FIFO cache is different, as gene tani points out. You need to consider which one it is you want. -- Mich

Re: dictionary that discards old items

2005-07-22 Thread Tim Williams (gmail)
On 7/21/05, Michael Hoffman <[EMAIL PROTECTED]> wrote: > Will McGugan wrote: > > > I need a collection class that behaves like a dictionary but when it > > reaches 'n' items it discards the oldest item so that the length never > > goes above 'n'. (Its for caching search results) > > > > I have a v

Re: dictionary that discards old items

2005-07-22 Thread Will McGugan
Michael Hoffman wrote: > Will McGugan wrote: > >> I need a collection class that behaves like a dictionary but when it >> reaches 'n' items it discards the oldest item so that the length never >> goes above 'n'. (Its for caching search results) >> >> I have a vague memory of a module that does t

Re: dictionary that discards old items

2005-07-22 Thread Raymond Hettinger
[Will McGugan] > I need a collection class that behaves like a dictionary but when it > reaches 'n' items it discards the oldest item so that the length never > goes above 'n'. (Its for caching search results) import collections class Cache(dict): def __init__(self, n, *args, **kwds):

Re: dictionary that discards old items

2005-07-22 Thread gene tani
not clear if you want a FIFO, or a LRU on read or write, so for the non-dictionary component of this, you could also ( (search Cookbook) || google) for terms like "ring buffer", "fixed-size cache", um, "bounded queue", "circular buffer", "deque", there's probably a bunch of others. -- http://mai

Re: dictionary that discards old items

2005-07-22 Thread Michael Hoffman
Will McGugan wrote: > I need a collection class that behaves like a dictionary but when it > reaches 'n' items it discards the oldest item so that the length never > goes above 'n'. (Its for caching search results) > > I have a vague memory of a module that does this, but I cant remember > whe

dictionary that discards old items

2005-07-22 Thread Will McGugan
Hi folks, I need a collection class that behaves like a dictionary but when it reaches 'n' items it discards the oldest item so that the length never goes above 'n'. (Its for caching search results) I have a vague memory of a module that does this, but I cant remember where I read about it. Ca