memoize to disk - python - persistent memoization -


is there way memoize output of function disk?

i have function

def gethtmlofurl(url):     ... # expensive computation 

and like:

def gethtmlmemoized(url) = memoizetofile(gethtmlofurl, "file.dat") 

and call gethtmlmemoized(url), expensive computation once each url.

python offers elegant way - decorators. basically, decorator function wraps function provide additional functionality without changing function source code. decorator can written this:

import json  def persist_to_file(file_name):      def decorator(original_func):          try:             cache = json.load(open(file_name, 'r'))         except (ioerror, valueerror):             cache = {}          def new_func(param):             if param not in cache:                 cache[param] = original_func(param)                 json.dump(cache, open(file_name, 'w'))             return cache[param]          return new_func      return decorator 

once you've got that, 'decorate' function using @-syntax , you're ready.

@persist_to_file('cache.dat') def html_of_url(url):     function code... 

note decorator intentionally simplified , may not work every situation, example, when source function accepts or returns data cannot json-serialized.

more on decorators: how make chain of function decorators?

and here's how make decorator save cache once, @ exit time:

import json, atexit  def persist_to_file(file_name):      try:         cache = json.load(open(file_name, 'r'))     except (ioerror, valueerror):         cache = {}      atexit.register(lambda: json.dump(cache, open(file_name, 'w')))      def decorator(func):         def new_func(param):             if param not in cache:                 cache[param] = func(param)             return cache[param]         return new_func      return decorator 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -