Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of callables #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ proof was designed to be used with the agate data analysis library, but can be u
Important links:

* Documentation: http://proof.rtfd.org
* Repository: https://github.com/onyxfish/proof
* Issues: https://github.com/onyxfish/proof/issues
* Repository: https://github.com/wireservice/proof
* Issues: https://github.com/wireservice/proof/issues
16 changes: 13 additions & 3 deletions proof/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
propagated to all dependent analyses.
"""

from __future__ import print_function

import bz2
from copy import deepcopy
from glob import glob
Expand All @@ -19,11 +21,12 @@

try:
import cPickle as pickle
except ImportError: # pragma: no cover
except ImportError: # pragma: no cover
import pickle

import six


class Cache(object):
"""
Utility class for managing cached data.
Expand Down Expand Up @@ -59,6 +62,7 @@ def set(self, data):
f.write(pickle.dumps(self._data))
f.close()


def never_cache(func):
"""
Decorator to flag that a given analysis function should never be cached.
Expand All @@ -67,6 +71,7 @@ def never_cache(func):

return func


class Analysis(object):
"""
An Analysis is a function whose source code fingerprint and output can be
Expand All @@ -90,7 +95,9 @@ def __init__(self, func, cache_dir='.proof', _trace=[]):
self._trace = _trace + [self]
self._child_analyses = []

self._cache_path = os.path.join(self._cache_dir, '%s.cache' % self._fingerprint())
self._cache_path = os.path.join(
self._cache_dir, '%s.cache' % self._fingerprint()
)
self._cache = Cache(self._cache_path)

self._registered_cache_paths = []
Expand All @@ -110,7 +117,10 @@ def _fingerprint(self):

hasher.update(history)

source = inspect.getsource(self._func)
if not inspect.isfunction(self._func) and hasattr(self._func, '__class__'):
source = inspect.getsource(self._func.__class__)
else:
source = inspect.getsource(self._func)

# In Python 3 inspect.getsource returns unicode data
if six.PY3:
Expand Down