Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Caching function results Date: Thu, 03 Mar 2016 21:58:02 +0100 Organization: None Lines: 25 Message-ID: References: <7a134b64-b7a7-46c6-9f89-cf166450f972@lists.xtsubasa.org> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de sBrTMQIc/P3Oykm0JGj/KgvxaGMmrAJa9vLkb+MOy/yQ== Return-Path: 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.05; 'cached': 0.09; 'pavel': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'python.': 0.11; 'value.': 0.15; 'dictionary.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'storing': 0.16; 'wrote:': 0.16; '(in': 0.18; 'arguments': 0.22; 'produces': 0.22; 'rid': 0.22; 'tuples': 0.22; 'implemented': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'values': 0.28; 'convert': 0.29; "i'd": 0.31; 'expensive': 0.32; 'url:python': 0.33; "i'll": 0.33; 'lists': 0.34; 'tasks': 0.35; 'url:dev': 0.35; 'url:org': 0.36; 'data.': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'missing': 0.37; 'doing': 0.38; 'data': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'called': 0.40; 'some': 0.40; 'limit': 0.65; 'subject:results': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd88e7.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103990 Pavel Volkov wrote: > Suppose, I have some resource-intensive tasks implemented as functions in > Python. > Those are called repeatedly in my program. > It's guranteed that a call with the same arguments always produces the > same return value. > I want to cache the arguments and return values and in case of repititive > call immediately return the result without doing expensive calculations. > The arguments and return values are almost always: > * ints > * floats > * tuple or lists of ints or floats > > I think Cache object will store data in a dictionary. > I'll convert lists to tuples before storing them. > > I'd also like to limit the size of the cache (in MB) and get rid of old > cached data. https://docs.python.org/dev/library/functools.html#functools.lru_cache The only thing that is missing is list-to-tuple conversion.