diff options
| author | J08nY | 2023-10-16 20:11:52 +0200 |
|---|---|---|
| committer | J08nY | 2023-10-16 20:11:52 +0200 |
| commit | a8d60bff6d4c70698cb4145cf1cc53cf215d4ad4 (patch) | |
| tree | 38f3a278eed73168d59e85438eca6d65cc04f7b0 /pyecsca/misc/utils.py | |
| parent | 6e33ca6b07dd09f42949c5f9a3f24e2d87c852e8 (diff) | |
| download | pyecsca-a8d60bff6d4c70698cb4145cf1cc53cf215d4ad4.tar.gz pyecsca-a8d60bff6d4c70698cb4145cf1cc53cf215d4ad4.tar.zst pyecsca-a8d60bff6d4c70698cb4145cf1cc53cf215d4ad4.zip | |
Diffstat (limited to 'pyecsca/misc/utils.py')
| -rw-r--r-- | pyecsca/misc/utils.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/pyecsca/misc/utils.py b/pyecsca/misc/utils.py index 1e9e9a0..010bbd2 100644 --- a/pyecsca/misc/utils.py +++ b/pyecsca/misc/utils.py @@ -1,6 +1,9 @@ """Just some utilities I promise.""" +import sys from ast import parse +from pyecsca.misc.cfg import getconfig + def pexec(s): return parse(s, mode="exec") @@ -8,3 +11,28 @@ def pexec(s): def peval(s): return parse(s, mode="eval") + + +def in_notebook() -> bool: + """Test whether we are executing in Jupyter notebook.""" + try: + from IPython import get_ipython + if 'IPKernelApp' not in get_ipython().config: # pragma: no cover + return False + except ImportError: + return False + except AttributeError: + return False + return True + + +def log(*args, **kwargs): + """Log a message.""" + if in_notebook() and getconfig().log.enabled: + print(*args, **kwargs) + + +def warn(*args, **kwargs): + """Log a message.""" + if in_notebook() and getconfig().log.enabled: + print(*args, **kwargs, file=sys.stderr) |
