File size: 505 Bytes
7145fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from contextlib import contextmanager
import signal
import sympy as sp

def timeout_handler(signum, frame):
    raise TimeoutError("Block timed out")
@contextmanager
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)