From 54249a97416fdad524a88e6cd2f152b1503b6479 Mon Sep 17 00:00:00 2001 From: Shay Palachy Date: Thu, 20 Apr 2017 14:22:53 +0300 Subject: [PATCH] small optimization and a fix for python2.7 on travis --- .travis.yml | 2 +- cachier/pickle_core.py | 34 ++++++++++++++++++---------------- tests/speed_eval.py | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/test_cachier.py | 35 ----------------------------------- 4 files changed, 61 insertions(+), 52 deletions(-) create mode 100644 tests/speed_eval.py diff --git a/.travis.yml b/.travis.yml index 83f9890..b6b855e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ before_install: # coverage submission packages - pip install codecov install: - - if [ "$TRAVIS_PYTHON_VERSION" == "2.7" ]; then pip install pytest coverage pytest-cov; else pip install ".[test]"; fi + - if [ "$TRAVIS_PYTHON_VERSION" == "2.7" ]; then pip install pytest coverage pytest-cov .; else pip install ".[test]"; fi script: python -m pytest --cov=cachier # submit coverage after_success: diff --git a/cachier/pickle_core.py b/cachier/pickle_core.py index e299369..55b7d47 100644 --- a/cachier/pickle_core.py +++ b/cachier/pickle_core.py @@ -66,23 +66,25 @@ class _PickleCore(_BaseCore): self.cache = None self.reload = reload - def _get_cache_file_name(self): - return '.{}.{}'.format( - self.func.__module__, self.func.__name__) # pylint: disable=W0212 + def _cache_fname(self): + if not hasattr(self, 'cache_fname'): + self.cache_fname = '.{}.{}'.format( + self.func.__module__, self.func.__name__) + return self.cache_fname - def _get_cache_path(self): - # print(EXPANDED_CACHIER_DIR) - if not os.path.exists(EXPANDED_CACHIER_DIR): - os.makedirs(EXPANDED_CACHIER_DIR) - fpath = os.path.abspath(os.path.join( - os.path.realpath(EXPANDED_CACHIER_DIR), - self._get_cache_file_name() - )) - # print(fpath) - return fpath + def _cache_fpath(self): + if not hasattr(self, 'cache_fpath'): + # print(EXPANDED_CACHIER_DIR) + if not os.path.exists(EXPANDED_CACHIER_DIR): + os.makedirs(EXPANDED_CACHIER_DIR) + self.cache_fpath = os.path.abspath(os.path.join( + os.path.realpath(EXPANDED_CACHIER_DIR), + self._cache_fname() + )) + return self.cache_fpath def _reload_cache(self): - fpath = self._get_cache_path() + fpath = self._cache_fpath() try: with open(fpath, 'rb') as cache_file: fcntl.flock(cache_file, fcntl.LOCK_SH) @@ -101,7 +103,7 @@ class _PickleCore(_BaseCore): def _save_cache(self, cache): self.cache = cache - fpath = self._get_cache_path() + fpath = self._cache_fpath() with open(fpath, 'wb') as cache_file: fcntl.flock(cache_file, fcntl.LOCK_EX) pickle.dump(cache, cache_file) @@ -155,7 +157,7 @@ class _PickleCore(_BaseCore): if not entry['being_calculated']: return entry['value'] event_handler = _PickleCore.CacheChangeHandler( - filename=self._get_cache_file_name(), + filename=self._cache_fname(), core=self, key=key ) diff --git a/tests/speed_eval.py b/tests/speed_eval.py new file mode 100644 index 0000000..dced773 --- /dev/null +++ b/tests/speed_eval.py @@ -0,0 +1,42 @@ + +from time import time + +from cachier import cachier + + +@cachier(next_time=True) +def _test_int_pickling(int_1, int_2): + """Add the two given ints.""" + return int_1 + int_2 + + +def _test_int_pickling_compare(int_1, int_2): + """Add the two given ints.""" + return int_1 + int_2 + + +def test_pickle_speed(): + """Test speeds""" + print("Comparing speeds of decorated vs non-decorated functions...") + num_of_vals = 100 + times = [] + for i in range(1, num_of_vals): + tic = time() + _test_int_pickling_compare(i, i + 1) + toc = time() + times.append(toc - tic) + print(' - Non-decorated average = {:.8f}'.format( + sum(times) / num_of_vals)) + + _test_int_pickling.clear_cache() + times = [] + for i in range(1, num_of_vals): + tic = time() + _test_int_pickling(i, i + 1) + toc = time() + times.append(toc - tic) + print(' - Decorated average = {:.8f}'.format( + sum(times) / num_of_vals)) + +if __name__ == '__main__': + test_pickle_speed() diff --git a/tests/test_cachier.py b/tests/test_cachier.py index f94714b..fc81d04 100644 --- a/tests/test_cachier.py +++ b/tests/test_cachier.py @@ -23,41 +23,6 @@ from cachier import cachier # Pickle core tests -@cachier(next_time=True) -def _test_int_pickling(int_1, int_2): - """Add the two given ints.""" - return int_1 + int_2 - - -def _test_int_pickling_compare(int_1, int_2): - """Add the two given ints.""" - return int_1 + int_2 - - -def test_pickle_speed(): - """Test speeds""" - print(" * Comparing speeds of decorated vs non-decorated functions...") - num_of_vals = 100 - times = [] - for i in range(1, num_of_vals): - tic = time() - _test_int_pickling_compare(i, i + 1) - toc = time() - times.append(toc - tic) - print(' - Non-decorated average = {:.8f}'.format( - sum(times) / num_of_vals)) - - _test_int_pickling.clear_cache() - times = [] - for i in range(1, num_of_vals): - tic = time() - _test_int_pickling(i, i + 1) - toc = time() - times.append(toc - tic) - print(' - Decorated average = {:.8f}'.format( - sum(times) / num_of_vals)) - - @cachier(next_time=False) def _takes_5_seconds(arg_1, arg_2): """Some function."""