summaryrefslogtreecommitdiff
path: root/decorator_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'decorator_utils.py')
-rw-r--r--decorator_utils.py24
1 files changed, 6 insertions, 18 deletions
diff --git a/decorator_utils.py b/decorator_utils.py
index daae64e..1ecbce3 100644
--- a/decorator_utils.py
+++ b/decorator_utils.py
@@ -80,9 +80,7 @@ def invocation_logged(func: Callable) -> Callable:
return wrapper_invocation_logged
-def rate_limited(
- n_calls: int, *, per_period_in_seconds: float = 1.0
-) -> Callable:
+def rate_limited(n_calls: int, *, per_period_in_seconds: float = 1.0) -> Callable:
"""Limit invocation of a wrapped function to n calls per period.
Thread safe. In testing this was relatively fair with multiple
threads using it though that hasn't been measured.
@@ -220,9 +218,7 @@ def debug_count_calls(func: Callable) -> Callable:
@functools.wraps(func)
def wrapper_debug_count_calls(*args, **kwargs):
wrapper_debug_count_calls.num_calls += 1
- msg = (
- f"Call #{wrapper_debug_count_calls.num_calls} of {func.__name__!r}"
- )
+ msg = f"Call #{wrapper_debug_count_calls.num_calls} of {func.__name__!r}"
print(msg)
logger.info(msg)
return func(*args, **kwargs)
@@ -266,15 +262,11 @@ def delay(
@functools.wraps(func)
def wrapper_delay(*args, **kwargs):
if when & DelayWhen.BEFORE_CALL:
- logger.debug(
- f"@delay for {seconds}s BEFORE_CALL to {func.__name__}"
- )
+ logger.debug(f"@delay for {seconds}s BEFORE_CALL to {func.__name__}")
time.sleep(seconds)
retval = func(*args, **kwargs)
if when & DelayWhen.AFTER_CALL:
- logger.debug(
- f"@delay for {seconds}s AFTER_CALL to {func.__name__}"
- )
+ logger.debug(f"@delay for {seconds}s AFTER_CALL to {func.__name__}")
time.sleep(seconds)
return retval
@@ -368,9 +360,7 @@ def memoized(func: Callable) -> Callable:
cache_key = args + tuple(kwargs.items())
if cache_key not in wrapper_memoized.cache:
value = func(*args, **kwargs)
- logger.debug(
- f"Memoizing {cache_key} => {value} for {func.__name__}"
- )
+ logger.debug(f"Memoizing {cache_key} => {value} for {func.__name__}")
wrapper_memoized.cache[cache_key] = value
else:
logger.debug(f"Returning memoized value for {func.__name__}")
@@ -760,9 +750,7 @@ def call_with_sample_rate(sample_rate: float) -> Callable:
if random.uniform(0, 1) < sample_rate:
return f(*args, **kwargs)
else:
- logger.debug(
- f"@call_with_sample_rate skipping a call to {f.__name__}"
- )
+ logger.debug(f"@call_with_sample_rate skipping a call to {f.__name__}")
return _call_with_sample_rate