@@ -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"
@@ -290,59 +274,37 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
290274 nonlocal allow_none , last_cleanup
291275 _allow_none = _update_with_defaults (allow_none , "allow_none" , kwds )
292276 # 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- )
277+ ignore_cache = _pop_kwds_with_deprecation (kwds , "ignore_cache" , False )
278+ overwrite_cache = _pop_kwds_with_deprecation (kwds , "overwrite_cache" , False )
299279 verbose = _pop_kwds_with_deprecation (kwds , "verbose_cache" , False )
300280 ignore_cache = kwds .pop ("cachier__skip_cache" , ignore_cache )
301- overwrite_cache = kwds .pop (
302- "cachier__overwrite_cache" , overwrite_cache
303- )
281+ overwrite_cache = kwds .pop ("cachier__overwrite_cache" , overwrite_cache )
304282 verbose = kwds .pop ("cachier__verbose" , verbose )
305- _stale_after = _update_with_defaults (
306- stale_after , "stale_after" , kwds
307- )
283+ _stale_after = _update_with_defaults (stale_after , "stale_after" , kwds )
308284 _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- )
285+ _cleanup_flag = _update_with_defaults (cleanup_stale , "cleanup_stale" , kwds )
286+ _cleanup_interval_val = _update_with_defaults (cleanup_interval , "cleanup_interval" , kwds )
315287 # 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- )
288+ kwargs = _convert_args_kwargs (func , _is_method = core .func_is_method , args = args , kwds = kwds )
319289
320290 if _cleanup_flag :
321291 now = datetime .now ()
322292 with cleanup_lock :
323293 if now - last_cleanup >= _cleanup_interval_val :
324294 last_cleanup = now
325- _get_executor ().submit (
326- core .delete_stale_entries , _stale_after
327- )
295+ _get_executor ().submit (core .delete_stale_entries , _stale_after )
328296
329297 _print = print if verbose else lambda x : None
330298
331299 # Check current global caching state dynamically
332300 from .config import _global_params
333301
334302 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- )
303+ return func (args [0 ], ** kwargs ) if core .func_is_method else func (** kwargs )
340304 key , entry = core .get_entry ((), kwargs )
341305 if overwrite_cache :
342306 return _calc_entry (core , key , func , args , kwds , _print )
343- if entry is None or (
344- not entry ._completed and not entry ._processing
345- ):
307+ if entry is None or (not entry ._completed and not entry ._processing ):
346308 _print ("No entry found. No current calc. Calling like a boss." )
347309 return _calc_entry (core , key , func , args , kwds , _print )
348310 _print ("Entry found." )
@@ -353,10 +315,7 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
353315 nonneg_max_age = True
354316 if max_age is not None :
355317 if max_age < ZERO_TIMEDELTA :
356- _print (
357- "max_age is negative. "
358- "Cached result considered stale."
359- )
318+ _print ("max_age is negative. Cached result considered stale." )
360319 nonneg_max_age = False
361320 else :
362321 assert max_age is not None # noqa: S101
@@ -379,9 +338,7 @@ def _call(*args, max_age: Optional[timedelta] = None, **kwds):
379338 _print ("Async calc and return stale" )
380339 core .mark_entry_being_calculated (key )
381340 try :
382- _get_executor ().submit (
383- _function_thread , core , key , func , args , kwds
384- )
341+ _get_executor ().submit (_function_thread , core , key , func , args , kwds )
385342 finally :
386343 core .mark_entry_not_calculated (key )
387344 return entry .value
@@ -426,9 +383,7 @@ def _precache_value(*args, value_to_cache, **kwds): # noqa: D417
426383
427384 """
428385 # 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- )
386+ kwargs = _convert_args_kwargs (func , _is_method = core .func_is_method , args = args , kwds = kwds )
432387 return core .precache_value ((), kwargs , value_to_cache )
433388
434389 func_wrapper .clear_cache = _clear_cache
0 commit comments