Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51054

odd behavoiur seen

From Chris Hinsley <chris.hinsley@gmail.com>
Newsgroups comp.lang.python
Date 2013-07-22 19:36 +0100
Message-ID <2013072219364160391-chrishinsley@gmailcom> (permalink)
Subject odd behavoiur seen

Show all headers | View raw


Folks, I have this decorator:

def memoize(maxsize):
    def _memoize(func):
        lru_cache = {}
        lru_list = []

        def memoizer(*args, **kwargs):
            key = str(args) + str(kwargs)
            if key in lru_cache:
                lru_list.remove(key)
                lru_list.append(key)
                return lru_cache[key]
            print len(lru_list),
            if len(lru_list) >= maxsize:
                del(lru_cache[lru_list[0]])
                del(lru_list[0])
            ret = func(*args, **kwargs)
            lru_cache[key] = ret
            lru_list.append(key)
            return ret
        return memoizer
    return _memoize

I didn't used to do the 'len(lru_list) >= maxsize' just '==' and 
noticed it sailing past the max number of entries, so put in the print 
statement, and now I see it ocationally printing a value 1 larger than 
maxsize !!!

So if I use it as '@memoize(64)' I see some 65's in the output ! I'm at 
a loss to explain it, does anyone knows why ? Is it a bug or some 
threading issue ? I'm not useing threads BTW, and I've noticed this in 
both running it with Python or Pypy.

Best Regards

Chris

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

odd behavoiur seen Chris Hinsley <chris.hinsley@gmail.com> - 2013-07-22 19:36 +0100
  Re: odd behavoiur seen Chris Hinsley <chris.hinsley@gmail.com> - 2013-07-22 19:42 +0100
    Re: odd behavoiur seen Peter Otten <__peter__@web.de> - 2013-07-22 21:47 +0200
      Re: odd behavoiur seen Chris Hinsley <chris.hinsley@gmail.com> - 2013-07-22 21:12 +0100

csiph-web