import ast,json
from pathlib import Path
import numberdb.sage as numberdb
from generate import estrada
from sage.rings.real_arb import RealBallField
root=Path(__file__).resolve().parent
d=json.loads((root/'stored-document.json').read_text())
code=d['Programs']['program-sage']['code']
tree=ast.parse(code)
for node in tree.body:
    if isinstance(node,ast.Expr) and isinstance(node.value,ast.Call) and isinstance(node.value.func,ast.Name) and node.value.func.id=='sum':
        node_index=tree.body.index(node)
        tree.body[node_index]=ast.Assign(targets=[ast.Name(id='program_result',ctx=ast.Store())],value=node.value)
ns={}
exec(compile(ast.fix_missing_locations(tree),'stored-program','exec'),ns)
v=ns['program_result']; b=RealBallField(480)
assert v.is_finite() and b(v.rad())<abs(b(v))*b(10)**(-120)
assert v.overlaps(estrada('CL'))
print('Stored Programs snippet: PASS, P4, finite narrow enclosure agrees')
