projects
/
python_utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Make presence detection work from cabin or house and deal with a
[python_utils.git]
/
decorator_utils.py
diff --git
a/decorator_utils.py
b/decorator_utils.py
index 2817239c88c2396b0e5dcc56e7c535b8afdd99d9..4f98a6d4062c4fc5bfcbcac0a2fddfc00c607cf5 100644
(file)
--- a/
decorator_utils.py
+++ b/
decorator_utils.py
@@
-47,15
+47,11
@@
def invocation_logged(func: Callable) -> Callable:
@functools.wraps(func)
def wrapper_invocation_logged(*args, **kwargs):
@functools.wraps(func)
def wrapper_invocation_logged(*args, **kwargs):
- now = datetime.datetime.now()
- ts = now.strftime("%Y/%d/%b:%H:%M:%S%Z")
- msg = f"[{ts}]: Entered {func.__name__}"
+ msg = f"Entered {func.__qualname__}"
print(msg)
logger.info(msg)
ret = func(*args, **kwargs)
print(msg)
logger.info(msg)
ret = func(*args, **kwargs)
- now = datetime.datetime.now()
- ts = now.strftime("%Y/%d/%b:%H:%M:%S%Z")
- msg = f"[{ts}]: Exited {func.__name__}"
+ msg = f"Exited {func.__qualname__}"
print(msg)
logger.info(msg)
return ret
print(msg)
logger.info(msg)
return ret
@@
-192,7
+188,7
@@
def retry_predicate(
tries: int,
*,
predicate: Callable[..., bool],
tries: int,
*,
predicate: Callable[..., bool],
- delay_sec: float = 3,
+ delay_sec: float = 3
.0
,
backoff: float = 2.0,
):
"""Retries a function or method up to a certain number of times
backoff: float = 2.0,
):
"""Retries a function or method up to a certain number of times
@@
-202,10
+198,10
@@
def retry_predicate(
delay_sec sets the initial delay period in seconds.
backoff is a multiplied (must be >1) used to modify the delay.
predicate is a function that will be passed the retval of the
delay_sec sets the initial delay period in seconds.
backoff is a multiplied (must be >1) used to modify the delay.
predicate is a function that will be passed the retval of the
-
decorated function and must return True to stop or False to
-
retry.
+ decorated function and must return True to stop or False to
+ retry.
"""
"""
- if backoff < 1:
+ if backoff < 1
.0
:
msg = f"backoff must be greater than or equal to 1, got {backoff}"
logger.critical(msg)
raise ValueError(msg)
msg = f"backoff must be greater than or equal to 1, got {backoff}"
logger.critical(msg)
raise ValueError(msg)
@@
-225,9
+221,11
@@
def retry_predicate(
@functools.wraps(f)
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay_sec # make mutable
@functools.wraps(f)
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay_sec # make mutable
+ logger.debug(f'deco_retry: will make up to {mtries} attempts...')
retval = f(*args, **kwargs)
while mtries > 0:
if predicate(retval) is True:
retval = f(*args, **kwargs)
while mtries > 0:
if predicate(retval) is True:
+ logger.debug('Predicate succeeded, deco_retry is done.')
return retval
logger.debug("Predicate failed, sleeping and retrying.")
mtries -= 1
return retval
logger.debug("Predicate failed, sleeping and retrying.")
mtries -= 1
@@
-295,8
+293,8
@@
def thunkify(func):
exc[0] = True
exc[1] = sys.exc_info() # (type, value, traceback)
msg = f"Thunkify has thrown an exception (will be raised on thunk()):\n{traceback.format_exc()}"
exc[0] = True
exc[1] = sys.exc_info() # (type, value, traceback)
msg = f"Thunkify has thrown an exception (will be raised on thunk()):\n{traceback.format_exc()}"
- logger.warning(msg)
print(msg)
print(msg)
+ logger.warning(msg)
finally:
wait_event.set()
finally:
wait_event.set()
@@
-338,7
+336,7
@@
def _target(queue, function, *args, **kwargs):
"""
try:
queue.put((True, function(*args, **kwargs)))
"""
try:
queue.put((True, function(*args, **kwargs)))
- except:
+ except
Exception
:
queue.put((False, sys.exc_info()[1]))
queue.put((False, sys.exc_info()[1]))
@@
-433,7
+431,6
@@
def timeout(
use_signals = thread_utils.is_current_thread_main_thread()
def decorate(function):
use_signals = thread_utils.is_current_thread_main_thread()
def decorate(function):
-
if use_signals:
def handler(signum, frame):
if use_signals:
def handler(signum, frame):