Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Pavel Volkov Newsgroups: comp.lang.python Subject: Caching function results Date: Thu, 03 Mar 2016 23:28:32 +0300 Lines: 58 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de J89TL0Pyh+bEqbtnEU9+VAeyvnjndjMNywUOYZIAtO4A== Return-Path: 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; 'yet.': 0.03; 'cache': 0.05; 'none:': 0.05; 'args,': 0.09; 'cached': 0.09; 'result)': 0.09; 'tuple': 0.09; 'way:': 0.09; 'python.': 0.11; 'def': 0.13; 'value.': 0.15; '"""load': 0.16; 'cache:': 0.16; 'clear(self):': 0.16; 'dictionary.': 0.16; 'filename):': 0.16; 'handle,': 0.16; 'optionally': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'storing': 0.16; '(in': 0.18; 'arguments': 0.22; 'produces': 0.22; 'rid': 0.22; 'tuples': 0.22; "python's": 0.23; 'import': 0.24; 'implemented': 0.24; 'previously': 0.24; 'module': 0.25; 'header :User-Agent:1': 0.26; "skip:' 10": 0.28; 'values': 0.28; 'calculated': 0.29; 'loads': 0.29; 'convert': 0.29; "i'd": 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; 'tasks': 0.35; 'data.': 0.36; 'to:addr :python-list': 0.36; 'received:org': 0.37; 'doing': 0.38; 'data': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'skip:u 10': 0.61; 'limit': 0.65; 'received:185': 0.91; 'subject:results': 0.91 X-PV-Sent: sailor@lists.xtsubasa.org User-Agent: Trojita/0.6; Qt/5.5.1; xcb; Linux; Gentoo Base System release 2.2 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:103989 Suppose, I have some resource-intensive tasks implemented as functions in=20 Python. Those are called repeatedly in my program. It's guranteed that a call with the same arguments always produces the same=20= return value. I want to cache the arguments and return values and in case of repititive=20 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=3DNone): # Optionally loads previously cached data from a file def clear(self): <...> # module 'calculations.py' import cache _cache =3D cache.Cache() def big_task(arg1, arg2, arg3=3DNone): cached_value =3D _cache.load_result('big_task', (arg1, arg2, arg3)) if cached_value is not None: return cached_value result =3D <...> _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=20 cached data. Don't know how yet. Do you like this design or maybe there's a better way with Python's=20 included batteries?