Source code for pyop3.debug

import collections
import contextlib
import time
import warnings

import numpy as np
from mpi4py import MPI
from petsc4py import PETSc

warnings.warn(
    "Importing pyop3.debug, this should not happen in released code",
    RuntimeWarning,
    stacklevel=2,
)


_stopping = collections.defaultdict(lambda: False)
"""Flag to switch conditional breakpoints on and off."""


[docs] def enable_conditional_breakpoints(marker=None): _stopping[marker] = True
[docs] def disable_conditional_breakpoints(marker=None): _stopping[marker] = False
[docs] def maybe_breakpoint(marker=None): if breakpoint_enabled(marker): breakpoint()
[docs] def breakpoint_enabled(marker=None): return _stopping[marker]
[docs] class TodoWarning(UserWarning): pass
[docs] def warn_todo(message: str) -> None: warnings.warn(message, TodoWarning)
[docs] def sane_print(array: np.ndarray) -> None: """Print an array to a fixed precision. This allows one to compare it by eye without going crazy over irrelevant floating point precision differences. """ with np.printoptions(precision=5, floatmode="fixed", suppress=True): print(array)
[docs] @contextlib.contextmanager def timer(func_name: str | None = None): start = time.time() yield duration = time.time() - start if func_name is None: print(f"Timer took {duration} seconds") else: print(f"Timer for '{func_name}' took {duration} seconds")