import json, re, os RAW = os.path.join(os.path.dirname(__file__), '..', 'raw') def load(n): return json.load(open(os.path.join(RAW, n + '.json'), encoding='utf-8')) MRE = re.compile(r'(?:IL|IP|I)?\s*-?\s*26\s*-?\s*(001|638|645)\b|\b(001|638|645)\b') OLD = re.compile(r'\b(2066|2081|2109|2111|2113|2117|2124)\b') def measures(desc): s = desc or '' ms = set(re.search(r'(001|638|645)', m.group(0)).group(1) for m in MRE.finditer(s)) return ms, bool(OLD.search(s)) def i638_share(x): """Equal-split I-638 share of one expenditure (L3 rule). Returns (i638, allocated_flag).""" amt = float(x['amount']); ms, old = measures(x.get('description')) if not ms: return 0.0, old # old-measure items count as allocated (other measures); else not allocated return (amt / len(ms) if '638' in ms else 0.0), True