from fractions import Fraction
from pathlib import Path
import json
cases=[('120-four',30,0,120,4),('100-four',30,0,100,4),('90-four',30,0,90,4),('120-seven',30,0,120,7),('reserve-two',30,2,120,4)]
rows=[]
for name,target,reserve,bpm,beats in cases:
    available=Fraction(target-reserve)
    bar_seconds=Fraction(60*beats,bpm)
    bars=available/bar_seconds
    full=bars.numerator//bars.denominator
    remainder=available-full*bar_seconds
    rows.append({'case':name,'target_seconds':target,'reserve_seconds':reserve,'bpm_counted_pulses':bpm,'counted_pulses_per_bar':beats,'available_seconds':str(available),'bar_seconds_exact':str(bar_seconds),'bars_exact':str(bars),'whole_bars':full,'remainder_seconds_exact':str(remainder),'assumption':'Constant tempo, same counted pulse for BPM and pulses/bar; theoretical budget, no generated output'})
assert [r['bars_exact'] for r in rows]==['15','25/2','45/4','60/7','14']
assert [r['remainder_seconds_exact'] for r in rows]==['0','6/5','2/3','2','0']
Path(__file__).with_name('cue-budget-results.json').write_text(json.dumps(rows,indent=2))
print(json.dumps(rows))
