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


Groups > comp.lang.python > #51057

Re: odd behavoiur seen

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'cache': 0.07; 'problem:': 0.07; 'function:': 0.09; 'item,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '**kwargs)': 0.16; 'alpha,': 0.16; 'cached': 0.16; 'folks,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sees': 0.16; 'alpha': 0.16; 'wrote:': 0.18; 'header:User- Agent:1': 0.23; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'ok.': 0.31; 'reduced': 0.31; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'problem': 0.35; 'should': 0.36; 'skip:- 20': 0.37; 'to:addr:python-list': 0.38; 'delete': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'remove': 0.60; 'length': 0.61; 'skip:n 10': 0.64; 'said:': 0.68; 'score': 0.74; 'colour': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: odd behavoiur seen
Date Mon, 22 Jul 2013 21:47:33 +0200
Organization None
References <2013072219364160391-chrishinsley@gmailcom> <2013072219424735368-chrishinsley@gmailcom>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084814e.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4987.1374522447.3114.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1374522447 news.xs4all.nl 15979 [2001:888:2000:d::a6]:48619
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51057

Show key headers only | View raw


Chris Hinsley wrote:

> On 2013-07-22 18:36:41 +0000, Chris Hinsley said:
> 
>> Folks, I have this decorator:
>> 
>> def memoize(maxsize):
>>     def _memoize(func):
>>         lru_cache = {}
>>         lru_list = []
> 
> Other clues, I use it on a recursive function:
> 
> @memoize(64)
> def next_move(board, colour, alpha, beta, ply):
>     if ply <= 0:
>         return evaluate(board) * colour
>     for new_board in all_moves(board[:], colour):
>         score = -next_move(new_board, -colour, -beta, -alpha, ply - 1)
>         if score >= beta:
>             return score
>         if score > alpha:
>             alpha = score
>     return alpha
> 
> And I notice I don't get the strange problem on a non-recursive
> function ! Or at least I don't seam to.

That's indeed the problem: 

>             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)

You delete a cached item, then call the original function which causes calls 
of the decorated function. This causes a length check which sees the already 
reduced length and decides that the cache is not yet full.

If you remove the oldest item after calling the original function you should 
be OK.

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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