Re: LRU cache

2023-02-22 Thread Rob Cliffe via Python-list
On 18/02/2023 17:19, Albert-Jan Roskam wrote: On Feb 18, 2023 17:28, Rob Cliffe via Python-list wrote: On 18/02/2023 15:29, Thomas Passin wrote: > On 2/18/2023 5:38 AM, Albert-Jan Roskam wrote: >>     I sometimes use this trick, which I learnt from a book by Martelli.

Re: LRU cache

2023-02-18 Thread Albert-Jan Roskam
On Feb 18, 2023 17:28, Rob Cliffe via Python-list wrote: On 18/02/2023 15:29, Thomas Passin wrote: > On 2/18/2023 5:38 AM, Albert-Jan Roskam wrote: >>     I sometimes use this trick, which I learnt from a book by Martelli. >>     Instead of try/except, membership te

Re: LRU cache

2023-02-18 Thread Rob Cliffe via Python-list
On 18/02/2023 15:29, Thomas Passin wrote: On 2/18/2023 5:38 AM, Albert-Jan Roskam wrote:     I sometimes use this trick, which I learnt from a book by Martelli.     Instead of try/except, membership testing with "in" (__contains__) might     be faster. Probably "depends". Matter of measuring

Re: LRU cache

2023-02-18 Thread Thomas Passin
On 2/18/2023 5:38 AM, Albert-Jan Roskam wrote: I sometimes use this trick, which I learnt from a book by Martelli. Instead of try/except, membership testing with "in" (__contains__) might be faster. Probably "depends". Matter of measuring. def somefunc(arg, _cache={}):     if

Re: LRU cache

2023-02-18 Thread Albert-Jan Roskam
I sometimes use this trick, which I learnt from a book by Martelli. Instead of try/except, membership testing with "in" (__contains__) might be faster. Probably "depends". Matter of measuring. def somefunc(arg, _cache={}):     if len(_cache) > 10 ** 5:         _cache.pop()    

Re: LRU cache

2023-02-17 Thread Dino
if value in recent: if (r := cache.get(value)) != value * value: raise ValueError(f"Cache missing recent {value} {r}") else: if cache.get(value) != None: raise ValueError(f"Cache includes old {value}") From: Pyt

Re: LRU cache

2023-02-16 Thread Weatherby,Gerard
else: if cache.get(value) != None: raise ValueError(f"Cache includes old {value}") From: Python-list on behalf of Dino Date: Wednesday, February 15, 2023 at 3:07 PM To: python-list@python.org Subject: Re: LRU cache *** Attention: This is an external email.

Re: LRU cache

2023-02-15 Thread Dino
Thank you Mats, Avi and Chris btw, functools.lru_cache seems rather different from what I need, but maybe I am missing something. I'll look closer. On 2/14/2023 7:36 PM, Mats Wichmann wrote: On 2/14/23 15:07, Dino wrote: -- https://mail.python.org/mailman/listinfo/python-list

Re: LRU cache

2023-02-15 Thread Mats Wichmann
On 2/14/23 15:07, Dino wrote: Here's my problem today. I am using a dict() to implement a quick and dirty in-memory cache. I am stopping adding elements when I am reaching 1000 elements (totally arbitrary number), but I would like to have something slightly more sophisticated to free up spa

RE: LRU cache

2023-02-14 Thread avi.e.gross
elico Sent: Tuesday, February 14, 2023 5:46 PM To: python-list@python.org Subject: Re: LRU cache On Wed, 15 Feb 2023 at 09:37, Dino wrote: > > > Here's my problem today. I am using a dict() to implement a quick and > dirty in-memory cache. > > I am stopping adding elem

RE: LRU cache

2023-02-14 Thread avi.e.gross
Dino, If your question is understood, you want to treat a dictionary as a sort of queue with a maximum number of entries. And, you want to remove some kind of least useful item to make room for any new one. Most dictionaries now have entries in the order they were entered. There may already be so

Re: LRU cache

2023-02-14 Thread Chris Angelico
On Wed, 15 Feb 2023 at 09:37, Dino wrote: > > > Here's my problem today. I am using a dict() to implement a quick and > dirty in-memory cache. > > I am stopping adding elements when I am reaching 1000 elements (totally > arbitrary number), but I would like to have something slightly more > sophist

Re: LRU cache?

2007-08-20 Thread qyloxe
#simple, but effective (sometimes) class ICORCache: def __init__(self,agetfunc,amaxlen=100): self.GetValue=agetfunc self.MaxLen=amaxlen self.VDict={} self.KDict={} self.VPos=1 self.AccessRatio=0 self.HitRatio=0 def __getitem__(self,key): self.A

Re: LRU cache?

2007-08-12 Thread Paul Rubin
Nikita the Spider <[EMAIL PROTECTED]> writes: > This one works for me: > http://www.webfast.com/~skip/python/Cache.py Thanks, this one keeps track of the actual clock time rather than just the relative age of cache entries, and has kind of a weird ageing mechanism, building and sorting an O(n) siz

Re: LRU cache?

2007-08-12 Thread Paul Rubin
[EMAIL PROTECTED] (Alex Martelli) writes: > So what's wrong with Evan Prodromou's lrucache.py module that's in pypi? > Haven't used it, but can't see anything wrong at a glance. Thanks, I wasn't aware of that one. I notice two things about it: 1) it's under a GPL-incompatible license (therefore a

Re: LRU cache?

2007-08-12 Thread Nikita the Spider
In article <[EMAIL PROTECTED]>, Paul Rubin wrote: > Anyone got a favorite LRU cache implementation? I see a few in google > but none look all that good. I just want a dictionary indexed by > strings, that remembers the last few thousand entries I put in it. > > It ac

Re: LRU cache?

2007-08-11 Thread Alex Martelli
Paul Rubin wrote: > Anyone got a favorite LRU cache implementation? I see a few in google > but none look all that good. I just want a dictionary indexed by > strings, that remembers the last few thousand entries I put in it. So what's wrong with Evan Prodromou's lruc

Re: LRU cache?

2007-08-11 Thread Steve Holden
Thomas Wittek wrote: > Paul Rubin schrieb: >> Anyone got a favorite LRU cache implementation? I see a few in google >> but none look all that good. I just want a dictionary indexed by >> strings, that remembers the last few thousand entries I put in it. > > I don't know a module for that (althou

Re: LRU cache?

2007-08-11 Thread Thomas Wittek
Paul Rubin schrieb: > Anyone got a favorite LRU cache implementation? I see a few in google > but none look all that good. I just want a dictionary indexed by > strings, that remembers the last few thousand entries I put in it. I don't know a module for that (although it might exist), but I coul

Re: LRU cache (and other things missing from the standard library ...)

2007-03-30 Thread skip
Evan> http://www.python.org/pypi?:action=display&name=lrucache&version=0.2 skip> Couldn't get to your website, but I've been using this one I wrote skip> for several years: skip> http://www.webfast.com/~skip/python/Cache.py skip> I'm just coming to this thread, so I don'

Re: LRU cache (and other things missing from the standard library ...)

2007-03-30 Thread skip
Evan> I agree that LRU caches are a data structure too frequently Evan> re-implemented in Python. I'd also like to see a "standard" cache Evan> package, either as part of the standard library or elsewhere. To Evan> that end, I've just uploaded an LRU cache package to the Python