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


Groups > comp.lang.python > #103993

Re: Caching function results

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Caching function results
Date Thu, 3 Mar 2016 13:59:44 -0700
Lines 68
Message-ID <mailman.165.1457038833.20602.python-list@python.org> (permalink)
References <7a134b64-b7a7-46c6-9f89-cf166450f972@lists.xtsubasa.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 78nnnBabLMtTQAxsKANg9ww88eiZaYV4HGePW6M/f0IQ==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'yet.': 0.03; 'cache': 0.05; 'none:': 0.05; 'args,': 0.09; 'cached': 0.09; 'pavel': 0.09; 'result)': 0.09; 'tuple': 0.09; 'way:': 0.09; 'python.': 0.11; 'def': 0.13; 'thu,': 0.15; 'value.': 0.15; '"""load': 0.16; '2016': 0.16; 'cache:': 0.16; 'clear(self):': 0.16; 'dictionary.': 0.16; 'entries,': 0.16; 'filename):': 0.16; 'handle,': 0.16; 'optionally': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'storing': 0.16; 'wrote:': 0.16; '(in': 0.18; 'arguments': 0.22; 'produces': 0.22; 'rid': 0.22; 'tuples': 0.22; "python's": 0.23; 'specified': 0.23; 'import': 0.24; 'implemented': 0.24; 'previously': 0.24; 'header :In-Reply-To:1': 0.24; 'module': 0.25; 'message- id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; 'values': 0.28; 'calculated': 0.29; 'loads': 0.29; 'convert': 0.29; "i'd": 0.31; 'option': 0.31; 'included': 0.32; 'skip:_ 10': 0.32; 'expensive': 0.32; 'maybe': 0.33; 'class': 0.33; 'skip:_ 30': 0.33; "i'll": 0.33; 'file': 0.34; 'lists': 0.34; 'received:google.com': 0.35; 'tasks': 0.35; 'received:209.85': 0.36; 'data.': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'doing': 0.38; 'received:209': 0.38; 'data': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'save': 0.60; 'skip:u 10': 0.61; 'limit': 0.65; 'mar': 0.65; 'idiomatic': 0.84; 'to:name:python': 0.84; 'subject:results': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=ZqSX1NSFrON27pz/2UrNWq5tCSLn91LPLBawOPEEpa4=; b=yavhvVF0yo45NlVAa9zOARCkfg0DSvbNV/jsX4Yd1Ikz2wgOhIbXbhlCpgapHBNFB7 wBCzQNraC7LodkfXVPLRUfrs0iK+dl4dn90EBdxrIKMofyNBh/iWNLR+dy9lLf9nMi63 ZNdjCjfhM8CjSb52UGwCJxzCpO15FWRq7O3QDDxs+Iyrb4V7eBgaT5PICQK5OdO/8uha wMkDtTeuntgc3ApFI1JOWqucvhFMizdJNdUVtlS86/s/V/Ey79IsGSNxkD3a4bGv7c7K Pp++sQXO+RtP+juxjYZnYYz0WmhW9r4r+kgYohJo4vuz0+At6qU//lBVzHjW1nqnJtMC PTxw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=ZqSX1NSFrON27pz/2UrNWq5tCSLn91LPLBawOPEEpa4=; b=kzJaVIvVMiTnXAmyGzXgZNCUdzc7K2kGHdziNLZ+exHrDDJj6APN1ax1kE3XO8vwih M+3wQQj8BOHXt6NemIWhSIbK6AgmAKHhzHTRChaqLQ80nY4+GhozH/uC7UvAIYETfoJI H8PEJtJBWm5McrvjTdftrQIOIcA/Yrl3SPmZP0iS/dLthn1/FqDmK79JMxEJD9Gf47+k Viz2L1CSvM7KiXukTe4tWVaDTFWnSXegxFwp9zdv883dJuxy8ms5THIb9t/BqymTZJXM 32cLBT1pdrWRjvsQWMM/3NpAyC1K4Unyc/IXrLIbdowMSimUm8Oj/owi1icYpnpM5fJF xrGg==
X-Gm-Message-State AD7BkJLGrbP8DiKGKUg5mQXW6oxXkyfzVzSA0rucKxHhEToN++5S2FSyJ1QdCnuowhp9BhzaxX6O4MROtH+o7w==
X-Received by 10.107.185.214 with SMTP id j205mr5065229iof.111.1457038824063; Thu, 03 Mar 2016 13:00:24 -0800 (PST)
In-Reply-To <7a134b64-b7a7-46c6-9f89-cf166450f972@lists.xtsubasa.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103993

Show key headers only | View raw


On Thu, Mar 3, 2016 at 1:28 PM, Pavel Volkov <sailor@lists.xtsubasa.org> 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.
>
> I intend to do it this way:
>
> # module 'cache.py'
>
> class Cache:
>    def save_result(self, handle, args):
>        """Save calculated result to cache."""
>        <...>
>    def load_result(self, handle, args, result):
>        """Load previously calculated result from cache.
>        Return None is it's unavailable."""
>        <...>
>    def save_to_file(self, filename):
>        """Save all cached data to a file."""
>    def __init__(self, filename=None):
>        # Optionally loads previously cached data from a file
>    def clear(self):
>        <...>
>
>
> # module 'calculations.py'
>
> import cache
> _cache = cache.Cache()
>
> def big_task(arg1, arg2, arg3=None):
>    cached_value = _cache.load_result('big_task', (arg1, arg2, arg3))
>    if cached_value is not None:
>        return cached_value
>    <do the normal calculations, no return operators here>
>    result = <...>
>    _cache.save_result('big_task', (arg1, arg2, arg3), result)
>    return result
>
> 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.
> Don't know how yet.
>
> Do you like this design or maybe there's a better way with Python's included
> batteries?

The idiomatic way is to use functools.lru_cache like so:

import functools

@functools.lru_cache(maxsize=128)
def big_task(arg1, arg2, arg3=None):
    ...

This does not give you the option to save the cache to a file however,
and the maxsize is specified in entries, not MB.

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


Thread

Re: Caching function results Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-03 13:59 -0700

csiph-web