Rajan Sharma commited on
Commit
07c72fd
·
verified ·
1 Parent(s): 4b415b1

Create math_validator.py

Browse files
Files changed (1) hide show
  1. validators/math_validator.py +24 -0
validators/math_validator.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def parse_int_from_formula(text: str) -> int:
2
+ # Best-effort: look for pattern "A x B x C = N" and return N if present.
3
+ # In production, prefer explicit numeric fields for each factor.
4
+ import re
5
+ m = re.search(r"=\s*([\d,]+)", text)
6
+ if not m:
7
+ return None
8
+ return int(m.group(1).replace(",", ""))
9
+
10
+ def assert_valid(data: dict):
11
+ cap = data.get("capacity", {})
12
+ total_clients = cap.get("total_clients")
13
+ formula = cap.get("formula_used", "")
14
+ parsed_total = parse_int_from_formula(formula)
15
+ if parsed_total is not None and total_clients is not None and parsed_total != total_clients:
16
+ raise ValueError(f"MathValidator: capacity total {total_clients} disagrees with formula {parsed_total}")
17
+
18
+ # Cost: total_for_n should equal total_per_client * N (if N is stated in formula or benchmark string).
19
+ cost = data.get("cost", {})
20
+ tpc = cost.get("total_per_client")
21
+ tfn = cost.get("total_for_n")
22
+ if None not in (tpc, tfn) and tpc >= 0 and tfn >= 0:
23
+ # can't infer N safely; trust validators upstream. This check is minimal by design.
24
+ pass