from contextlib import contextmanager | |
import signal | |
import sympy as sp | |
def timeout_handler(signum, frame): | |
raise TimeoutError("Block timed out") | |
def timeout(duration): | |
signal.signal(signal.SIGALRM, timeout_handler) | |
signal.alarm(duration) | |
try: | |
yield | |
finally: | |
signal.alarm(0) | |
class DecodeError(Exception): | |
pass | |
def sympy_expr_ok(expr): | |
atoms = expr.atoms() | |
return not (sp.I in atoms or sp.oo in atoms or sp.zoo in atoms or sp.nan in atoms) | |