zutshi / py-diskmemo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A simple disk-caching/memoizing decorator based on the disk-persistent dictionary in the standard library, shelve. I've found this thing to be very useful when trying experiments with lots of different parameter values: using diskmemo I don't have to re-write caching code for every function and I don't have to worry about restarting the interpreter.

Here's a simple usage example based on the standard memoized Fibonacci sequence computation.

import diskmemo

@diskmemo.memoize
def fib(n):
    "Return the nth Fibonacci number."
    if n in (0,1):
        return n
    return fib(n-1) + fib(n-2)

From a Python interpreter we can do this:

from fibdemo import fib
print fib(100) # <- way faster than naive recursion!

Then we can exit and re-enter the Python interpreter, re-import the function or re-define the same one, and

print fib(100) # <- instant!

The Fibonacci case isn't very inspiring, but if you have any deterministic code that you'd like to persistently memoize, this decorator can help!

It syncs to disk at every call.

About

License:The Unlicense


Languages

Language:Python 100.0%