Module Expr Str ImportFrom alias Import alias Import alias Import alias Import alias Import alias Import alias ImportFrom alias Assign NameStore Str If UnaryOpNot Call AttributeLoad AttributeLoad NameLoad attr attr NameLoad body Expr Call AttributeLoad NameLoad attr NameLoad Assign NameStore Call AttributeLoad NameLoad attr NameLoad FunctionDef arguments args defaults vararg kwarg body Return BinOpAdd Call NameLoad NameLoad Call NameLoad Call NameLoad Call AttributeLoad NameLoad attr decorator_list ClassDef bases NameLoad body Pass decorator_list FunctionDef arguments args NameParam NameParam defaults NameLoad NameLoad body FunctionDef arguments args NameParam defaults body FunctionDef arguments args defaults vararg kwarg body TryExcept body Assign NameStore Call AttributeLoad Call NameLoad Call NameLoad NameLoad NameLoad attr Str handlers ExceptHandler type NameLoad body Return Call NameLoad NameLoad NameLoad Assign NameStore SubscriptLoad Call AttributeLoad Call AttributeLoad NameLoad attr NameLoad attr Slice Num Assign NameStore Call AttributeLoad AttributeLoad NameLoad attr attr BoolOpOr NameLoad NameLoad Call AttributeLoad Str attr AttributeLoad NameLoad attr AttributeLoad NameLoad attr NameLoad If Call AttributeLoad AttributeLoad NameLoad attr attr NameLoad body Assign NameStore Call AttributeLoad AttributeLoad NameLoad attr attr NameLoad Assign NameStore BinOpMod Str BinOpDiv NameLoad BinOpMult Num Num Expr Call AttributeLoad NameLoad attr Call AttributeLoad Str attr NameLoad NameLoad Return Call AttributeLoad NameLoad attr NameLoad orelse Expr Call AttributeLoad NameLoad attr Call AttributeLoad Str attr NameLoad Assign NameStore Call NameLoad NameLoad NameLoad Assign NameStore Call AttributeLoad Str attr NameLoad Call AttributeLoad Str attr Call AttributeLoad NameLoad attr AttributeLoad NameLoad attr Num TryExcept body Expr Call AttributeLoad NameLoad attr NameLoad NameLoad keyword Num Expr Call AttributeLoad NameLoad attr NameLoad NameLoad handlers ExceptHandler type NameLoad body Expr Call AttributeLoad NameLoad attr Call AttributeLoad Str attr NameLoad Return NameLoad decorator_list Call NameLoad NameLoad Assign AttributeStore NameLoad attr NameLoad Return NameLoad decorator_list Return NameLoad decorator_list
N/A N/A This module contains a decorator :func:`cached` that can be used to
cache the results of any Python functions to disk.

This is useful when you have functions that take a long time to
compute their value, and you want to cache the results of those
functions between runs.

Python's :mod:`pickle` is used to serialize data.  All cache files go
into the `cache/` directory inside your working directory.

`@cached` uses a cache key function to find out if it has the value
for some given function arguments cached on disk.  The way it
calculates that cache key by default is to simply use the string
representation of all arguments passed into the function.  Thus, the
default cache key function looks like this:

.. code-block:: python

    def default_cache_key(*args, **kwargs):
        return str(args) + str(sorted(kwargs.items()))

Here is an example use of the :func:`cached` decorator:

.. code-block:: python

    import math
    @cached()
    def fac(x):
        print 'called!'
        return math.factorial(x)

    fac(20)
    called!
    2432902008176640000
    fac(20)
    2432902008176640000

Often you will want to use a more intelligent cache key, one that
takes more things into account.  Here's an example cache key function
for a cache decorator used with a `transform` method of a scikit-learn
:class:`~sklearn.base.BaseEstimator`:

.. doctest::

    >>> def transform_cache_key(self, X):
    ...     return ','.join([
    ...         str(X[:20]),
    ...         str(X[-20:]),
    ...         str(X.shape),
    ...         str(sorted(self.get_params().items())),
    ...         ])

This function puts the first and the last twenty rows of the matrix
`X` into the cache key.  On top of that, it adds the shape of the
matrix `X.shape` along with the items in `self.get_params`, which with
a scikit-learn :class:`~sklearn.base.BaseEstimator` class is the
dictionary of model parameters.  This makes sure that even though the
input matrix is the same, it will still calculate the value again if
the value of `self.get_params()` is different.

Your estimator class can then use the decorator like so:

.. code-block:: python

    class MyEstimator(BaseEstimator):
        @cached(transform_cache_key)
        def transform(self, X):
            # ...
 functools wraps N/A hashlib N/A logging N/A random N/A os N/A string N/A traceback joblib numpy_pickle N/A CACHE_PATH cache/ N/A N/A N/A N/A N/A os path exists CACHE_PATH N/A N/A N/A N/A os mkdir CACHE_PATH N/A logger N/A N/A logging getLogger __name__ default_cache_key N/A N/A N/A args kwargs N/A N/A N/A N/A str args N/A str N/A sorted N/A N/A kwargs items N/A DontCache N/A Exception N/A N/A N/A cached N/A N/A cache_key cache_path N/A default_cache_key None N/A cached N/A N/A func N/A N/A wrapper N/A N/A N/A args kwargs N/A N/A N/A N/A key N/A N/A N/A str N/A cache_key args kwargs encode ascii N/A N/A N/A DontCache N/A N/A N/A func args kwargs N/A hashed_key N/A N/A N/A N/A N/A hashlib sha1 key hexdigest N/A 8 N/A filename N/A N/A N/A os path join N/A cache_path CACHE_PATH N/A N/A {}.{}-cache-{} format N/A func __module__ N/A func __name__ hashed_key N/A N/A N/A N/A os path exists filename N/A N/A filesize N/A N/A N/A os path getsize filename N/A size N/A %0.1f MB N/A filesize N/A 1024 1024.0 N/A N/A N/A logger debug N/A N/A  * cache hit: {} ({}) format filename size N/A N/A N/A numpy_pickle load filename N/A N/A N/A N/A logger debug N/A N/A  * cache miss: {} format filename N/A value N/A func args kwargs N/A tmp_filename N/A N/A {}-{}.tmp format filename N/A N/A join N/A N/A random sample N/A string ascii_letters 4 N/A N/A N/A N/A N/A numpy_pickle dump value tmp_filename compress 9 N/A N/A N/A os rename tmp_filename filename N/A N/A N/A Exception N/A N/A N/A N/A logger exception N/A N/A Saving pickle {} resulted in Exception format filename N/A value N/A N/A wraps func N/A N/A wrapper uncached func N/A wrapper N/A N/A cached N/A