From 033fd10de83065a7cf469666fd1bd76e6adcf805 Mon Sep 17 00:00:00 2001 From: Martin Schrimpf Date: Fri, 21 Jun 2019 14:08:57 -0400 Subject: [PATCH] add `RESULTCACHING_DISABLE` option to turn off the framework (load/save) --- README.md | 6 ++++++ result_caching/__init__.py | 11 ++++++++--- tests/test___init__.py | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 457d81b..de5022f 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,9 @@ By default, results will be stored in `~/.result_caching`, this can be changed through the environment variable `RESULTCACHING_HOME`. `cache` will only hold results in memory and not write them to disk. + +## Environment variables +| Variable | description | +|-----------------------|----------------------------------------------------------------------------------| +| RESULTCACHING_HOME | directory to cache results (benchmark ceilings) in, `~/.result_caching` by default | +| RESULTCACHING_DISABLE | `'1'` to disable loading and saving of results, functions will be called directly | diff --git a/result_caching/__init__.py b/result_caching/__init__.py index 4ba54fa..30bc7c2 100644 --- a/result_caching/__init__.py +++ b/result_caching/__init__.py @@ -27,6 +27,10 @@ def get_function_identifier(function, call_args): return function_identifier +def is_enabled(): + return os.getenv('RESULTCACHING_DISABLE', '0') != '1' + + class _Storage(object): def __init__(self, identifier_ignore=()): """ @@ -41,13 +45,14 @@ def __call__(self, function): def wrapper(*args, **kwargs): call_args = self.getcallargs(function, *args, **kwargs) function_identifier = self.get_function_identifier(function, call_args) - if self.is_stored(function_identifier): + if is_enabled() and self.is_stored(function_identifier): self._logger.debug("Loading from storage: {}".format(function_identifier)) return self.load(function_identifier) self._logger.debug("Running function: {}".format(function_identifier)) result = function(*args, **kwargs) - self._logger.debug("Saving to storage: {}".format(function_identifier)) - self.save(result, function_identifier) + if is_enabled(): + self._logger.debug("Saving to storage: {}".format(function_identifier)) + self.save(result, function_identifier) return result return wrapper diff --git a/tests/test___init__.py b/tests/test___init__.py index e6d1153..2d83da8 100644 --- a/tests/test___init__.py +++ b/tests/test___init__.py @@ -196,6 +196,25 @@ def __repr__(self): # second call returns same thing and doesn't actually call function again assert c.f(1) == 1 + def test_disable_store(self): + with tempfile.TemporaryDirectory() as storage_dir: + os.environ['RESULTCACHING_HOME'] = storage_dir + os.environ['RESULTCACHING_DISABLE'] = '1' + + function_calls = 0 + + @store() + def func(x): + nonlocal function_calls + function_calls += 1 + return x + + assert func(1) == 1 + assert function_calls == 1 + assert not os.listdir(storage_dir) + assert func(1) == 1 + assert function_calls == 2 + class TestCache: def test(self):