https://github.com/pymc-devs/pymc3
Raw File
Tip revision: 90a3fbf266ef0aac92326ecf95fe60e992b9ad46 authored by Chris Fonnesbeck on 06 September 2016, 19:12:49 UTC
Incremented version to 3.0.rc1 (#1334)
Tip revision: 90a3fbf
memoize.py
import functools


def memoize(obj):
    """
    An expensive memoizer that works with unhashables
    """
    cache = obj.cache = {}

    @functools.wraps(obj)
    def memoizer(*args, **kwargs):
        key = (hashable(args), hashable(kwargs))

        if key not in cache:
            cache[key] = obj(*args, **kwargs)

        return cache[key]
    return memoizer


def hashable(a):
    """
    Turn some unhashable objects into hashable ones.
    """
    if isinstance(a, dict):
        return hashable(a.items())
    try:
        return tuple(map(hashable, a))
    except:
        return a
back to top