@@ -56,9 +56,7 @@ def _function_thread(core, key, func, args, kwds):
5656 print (f"Function call failed with the following exception:\n { exc } " )
5757
5858
59- def _calc_entry (
60- core , key , func , args , kwds , printer = lambda * _ : None
61- ) -> Optional [Any ]:
59+ def _calc_entry (core , key , func , args , kwds , printer = lambda * _ : None ) -> Optional [Any ]:
6260 core .mark_entry_being_calculated (key )
6361 try :
6462 func_res = func (* args , ** kwds )
@@ -70,26 +68,18 @@ def _calc_entry(
7068 core .mark_entry_not_calculated (key )
7169
7270
73- def _convert_args_kwargs (
74- func , _is_method : bool , args : tuple , kwds : dict
75- ) -> dict :
71+ def _convert_args_kwargs (func , _is_method : bool , args : tuple , kwds : dict ) -> dict :
7672 """Convert mix of positional and keyword arguments to aggregated kwargs."""
7773 # unwrap if the function is functools.partial
7874 if hasattr (func , "func" ):
7975 args = func .args + args
8076 kwds .update ({k : v for k , v in func .keywords .items () if k not in kwds })
8177 func = func .func
8278 func_params = list (inspect .signature (func ).parameters )
83- args_as_kw = dict (
84- zip (func_params [1 :], args [1 :])
85- if _is_method
86- else zip (func_params , args )
87- )
79+ args_as_kw = dict (zip (func_params [1 :], args [1 :]) if _is_method else zip (func_params , args ))
8880 # init with default values
8981 kwargs = {
90- k : v .default
91- for k , v in inspect .signature (func ).parameters .items ()
92- if v .default is not inspect .Parameter .empty
82+ k : v .default for k , v in inspect .signature (func ).parameters .items () if v .default is not inspect .Parameter .empty
9383 }
9484 # merge args expanded as kwargs and the original kwds
9585 kwargs .update (dict (** args_as_kw , ** kwds ))
@@ -99,8 +89,7 @@ def _convert_args_kwargs(
9989def _pop_kwds_with_deprecation (kwds , name : str , default_value : bool ):
10090 if name in kwds :
10191 warnings .warn (
102- f"`{ name } ` is deprecated and will be removed in a future release,"
103- " use `cachier__` alternative instead." ,
92+ f"`{ name } ` is deprecated and will be removed in a future release, use `cachier__` alternative instead." ,
10493 DeprecationWarning ,
10594 stacklevel = 2 ,
10695 )
@@ -201,18 +190,13 @@ def cachier(
201190 """
202191 # Check for deprecated parameters
203192 if hash_params is not None :
204- message = (
205- "hash_params will be removed in a future release, "
206- "please use hash_func instead"
207- )
193+ message = "hash_params will be removed in a future release, please use hash_func instead"
208194 warn (message , DeprecationWarning , stacklevel = 2 )
209195 hash_func = hash_params
210196 # Update parameters with defaults if input is None
211197 backend = _update_with_defaults (backend , "backend" )
212198 mongetter = _update_with_defaults (mongetter , "mongetter" )
213- size_limit_bytes = parse_bytes (
214- _update_with_defaults (entry_size_limit , "entry_size_limit" )
215- )
199+ size_limit_bytes = parse_bytes (_update_with_defaults (entry_size_limit , "entry_size_limit" ))
216200 # Override the backend parameter if a mongetter is provided.
217201 if callable (mongetter ):
218202 backend = "mongo"
@@ -235,9 +219,7 @@ def cachier(
235219 )
236220 elif backend == "memory" :
237221 core = _MemoryCore (
238- hash_func = hash_func ,
239- wait_for_calc_timeout = wait_for_calc_timeout ,
240- entry_size_limit = size_limit_bytes ,
222+ hash_func = hash_func , wait_for_calc_timeout = wait_for_calc_timeout , entry_size_limit = size_limit_bytes
241223 )
242224 elif backend == "sql" :
243225 core = _SQLCore (
@@ -290,59 +272,37 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
290272 nonlocal allow_none , last_cleanup
291273 _allow_none = _update_with_defaults (allow_none , "allow_none" , kwds )
292274 # print('Inside general wrapper for {}.'.format(func.__name__))
293- ignore_cache = _pop_kwds_with_deprecation (
294- kwds , "ignore_cache" , False
295- )
296- overwrite_cache = _pop_kwds_with_deprecation (
297- kwds , "overwrite_cache" , False
298- )
275+ ignore_cache = _pop_kwds_with_deprecation (kwds , "ignore_cache" , False )
276+ overwrite_cache = _pop_kwds_with_deprecation (kwds , "overwrite_cache" , False )
299277 verbose = _pop_kwds_with_deprecation (kwds , "verbose_cache" , False )
300278 ignore_cache = kwds .pop ("cachier__skip_cache" , ignore_cache )
301- overwrite_cache = kwds .pop (
302- "cachier__overwrite_cache" , overwrite_cache
303- )
279+ overwrite_cache = kwds .pop ("cachier__overwrite_cache" , overwrite_cache )
304280 verbose = kwds .pop ("cachier__verbose" , verbose )
305- _stale_after = _update_with_defaults (
306- stale_after , "stale_after" , kwds
307- )
281+ _stale_after = _update_with_defaults (stale_after , "stale_after" , kwds )
308282 _next_time = _update_with_defaults (next_time , "next_time" , kwds )
309- _cleanup_flag = _update_with_defaults (
310- cleanup_stale , "cleanup_stale" , kwds
311- )
312- _cleanup_interval_val = _update_with_defaults (
313- cleanup_interval , "cleanup_interval" , kwds
314- )
283+ _cleanup_flag = _update_with_defaults (cleanup_stale , "cleanup_stale" , kwds )
284+ _cleanup_interval_val = _update_with_defaults (cleanup_interval , "cleanup_interval" , kwds )
315285 # merge args expanded as kwargs and the original kwds
316- kwargs = _convert_args_kwargs (
317- func , _is_method = core .func_is_method , args = args , kwds = kwds
318- )
286+ kwargs = _convert_args_kwargs (func , _is_method = core .func_is_method , args = args , kwds = kwds )
319287
320288 if _cleanup_flag :
321289 now = datetime .now ()
322290 with cleanup_lock :
323291 if now - last_cleanup >= _cleanup_interval_val :
324292 last_cleanup = now
325- _get_executor ().submit (
326- core .delete_stale_entries , _stale_after
327- )
293+ _get_executor ().submit (core .delete_stale_entries , _stale_after )
328294
329295 _print = print if verbose else lambda x : None
330296
331297 # Check current global caching state dynamically
332298 from .config import _global_params
333299
334300 if ignore_cache or not _global_params .caching_enabled :
335- return (
336- func (args [0 ], ** kwargs )
337- if core .func_is_method
338- else func (** kwargs )
339- )
301+ return func (args [0 ], ** kwargs ) if core .func_is_method else func (** kwargs )
340302 key , entry = core .get_entry ((), kwargs )
341303 if overwrite_cache :
342304 return _calc_entry (core , key , func , args , kwds , _print )
343- if entry is None or (
344- not entry ._completed and not entry ._processing
345- ):
305+ if entry is None or (not entry ._completed and not entry ._processing ):
346306 _print ("No entry found. No current calc. Calling like a boss." )
347307 return _calc_entry (core , key , func , args , kwds , _print )
348308 _print ("Entry found." )
@@ -353,10 +313,7 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
353313 nonneg_max_age = True
354314 if max_age is not None :
355315 if max_age < ZERO_TIMEDELTA :
356- _print (
357- "max_age is negative. "
358- "Cached result considered stale."
359- )
316+ _print ("max_age is negative. Cached result considered stale." )
360317 nonneg_max_age = False
361318 else :
362319 assert max_age is not None # noqa: S101
@@ -379,9 +336,7 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
379336 _print ("Async calc and return stale" )
380337 core .mark_entry_being_calculated (key )
381338 try :
382- _get_executor ().submit (
383- _function_thread , core , key , func , args , kwds
384- )
339+ _get_executor ().submit (_function_thread , core , key , func , args , kwds )
385340 finally :
386341 core .mark_entry_not_calculated (key )
387342 return entry .value
@@ -426,9 +381,7 @@ def _precache_value(*args, value_to_cache, **kwds): # noqa: D417
426381
427382 """
428383 # merge args expanded as kwargs and the original kwds
429- kwargs = _convert_args_kwargs (
430- func , _is_method = core .func_is_method , args = args , kwds = kwds
431- )
384+ kwargs = _convert_args_kwargs (func , _is_method = core .func_is_method , args = args , kwds = kwds )
432385 return core .precache_value ((), kwargs , value_to_cache )
433386
434387 func_wrapper .clear_cache = _clear_cache
0 commit comments