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


Groups > comp.lang.python > #103990

Re: Caching function results

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Caching function results
Date 2016-03-03 21:58 +0100
Organization None
Message-ID <mailman.162.1457038699.20602.python-list@python.org> (permalink)
References <7a134b64-b7a7-46c6-9f89-cf166450f972@lists.xtsubasa.org>

Show all headers | View raw


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.

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


Thread

Re: Caching function results Peter Otten <__peter__@web.de> - 2016-03-03 21:58 +0100

csiph-web