From fb0883cb7bfa357b48a8a27732b0c23f71d1b582 Mon Sep 17 00:00:00 2001 From: Jirka Date: Tue, 13 Feb 2024 23:27:10 +0100 Subject: [PATCH 1/3] refactor: cleaning some more try/except --- cachier/core.py | 19 ++++--------------- cachier/cores/pickle.py | 22 ++++++++-------------- tests/test_core_lookup.py | 16 ++++------------ 3 files changed, 16 insertions(+), 41 deletions(-) diff --git a/cachier/core.py b/cachier/core.py index dfc326e5..f7f2e09a 100644 --- a/cachier/core.py +++ b/cachier/core.py @@ -36,11 +36,7 @@ def _max_workers(): - try: - return int(os.environ[MAX_WORKERS_ENVAR_NAME]) - except KeyError: - os.environ[MAX_WORKERS_ENVAR_NAME] = str(DEFAULT_MAX_WORKERS) - return DEFAULT_MAX_WORKERS + return int(os.environ.get(MAX_WORKERS_ENVAR_NAME, DEFAULT_MAX_WORKERS)) def _set_max_workers(max_workers): @@ -49,13 +45,9 @@ def _set_max_workers(max_workers): def _get_executor(reset=False): - if reset: - _get_executor.executor = ThreadPoolExecutor(_max_workers()) - try: - return _get_executor.executor - except AttributeError: + if reset or not hasattr(_get_executor, "executor"): _get_executor.executor = ThreadPoolExecutor(_max_workers()) - return _get_executor.executor + return _get_executor.executor def _function_thread(core, key, func, args, kwds): @@ -350,10 +342,7 @@ def clear_being_calculated(): def cache_dpath(): """Returns the path to the cache dir, if exists; None if not.""" - try: - return core.cache_dir - except AttributeError: - return None + return getattr(core, "cache_dir", None) def precache_value(*args, value_to_cache, **kwds): """Add an initial value to the cache. diff --git a/cachier/cores/pickle.py b/cachier/cores/pickle.py index d83a1979..603688f4 100644 --- a/cachier/cores/pickle.py +++ b/cachier/cores/pickle.py @@ -8,6 +8,7 @@ # Copyright (c) 2016, Shay Palachy import os import pickle # for local caching +from contextlib import suppress from datetime import datetime import threading @@ -148,19 +149,12 @@ def _get_cache(self): def _get_cache_by_key(self, key=None, hash=None): fpath = self._cache_fpath() - if hash is None: - fpath += f"_{key}" - else: - fpath += f"_{hash}" + fpath += f"_{key}" if hash is None else f"_{hash}" try: with portalocker.Lock(fpath, mode="rb") as cache_file: - try: - res = pickle.load(cache_file) - except EOFError: # pragma: no cover - res = None - except FileNotFoundError: - res = None - return res + return pickle.load(cache_file) + except (FileNotFoundError, EOFError): + return None def _clear_all_cache_files(self): fpath = self._cache_fpath() @@ -253,11 +247,11 @@ def mark_entry_not_calculated(self, key): self.mark_entry_not_calculated_separate_files(key) with self.lock: cache = self._get_cache() - try: + with suppress( + KeyError + ): # that's ok, we don't need an entry in that case cache[key]["being_calculated"] = False self._save_cache(cache) - except KeyError: - pass # that's ok, we don't need an entry in that case def wait_on_entry_calc(self, key): if self.separate_files: diff --git a/tests/test_core_lookup.py b/tests/test_core_lookup.py index 97d8d24c..229201de 100644 --- a/tests/test_core_lookup.py +++ b/tests/test_core_lookup.py @@ -1,4 +1,5 @@ """Testing a few basic cachier interfaces.""" +import pytest from cachier import cachier, get_default_params from cachier.core import MissingMongetter @@ -21,31 +22,22 @@ def test_get_default_params(): ) -def test_bad_name(): +def test_bad_name(name="nope"): """Test that the appropriate exception is thrown when an invalid backend is given.""" - name = "nope" - try: + with pytest.raises(ValueError, match=f"*{name}*"): @cachier(backend=name) def dummy_func(): pass - except ValueError as e: - assert name in e.args[0] - else: - assert False def test_missing_mongetter(): """Test that the appropriate exception is thrown when forgetting to specify the mongetter.""" - try: + with pytest.raises(MissingMongetter): @cachier(backend="mongo", mongetter=None) def dummy_func(): pass - except MissingMongetter: - assert True - else: - assert False From aa240c4c80084bea760d1bd9a8e4394132874c65 Mon Sep 17 00:00:00 2001 From: Jirka Borovec <6035284+Borda@users.noreply.github.com> Date: Tue, 13 Feb 2024 23:29:02 +0100 Subject: [PATCH 2/3] Apply suggestions from code review --- cachier/cores/pickle.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cachier/cores/pickle.py b/cachier/cores/pickle.py index 603688f4..2baed0a7 100644 --- a/cachier/cores/pickle.py +++ b/cachier/cores/pickle.py @@ -247,9 +247,8 @@ def mark_entry_not_calculated(self, key): self.mark_entry_not_calculated_separate_files(key) with self.lock: cache = self._get_cache() - with suppress( - KeyError - ): # that's ok, we don't need an entry in that case + # that's ok, we don't need an entry in that case + with suppress(KeyError): cache[key]["being_calculated"] = False self._save_cache(cache) From 48e91bc907054b4420762c9ea84cc78b1934bafd Mon Sep 17 00:00:00 2001 From: Jirka Date: Tue, 13 Feb 2024 23:45:49 +0100 Subject: [PATCH 3/3] match --- tests/test_core_lookup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_core_lookup.py b/tests/test_core_lookup.py index 229201de..57b5f3a2 100644 --- a/tests/test_core_lookup.py +++ b/tests/test_core_lookup.py @@ -26,7 +26,7 @@ def test_bad_name(name="nope"): """Test that the appropriate exception is thrown when an invalid backend is given.""" - with pytest.raises(ValueError, match=f"*{name}*"): + with pytest.raises(ValueError, match=f"specified an invalid core: {name}"): @cachier(backend=name) def dummy_func():