Coverage for backend/core/auxiliary/methods/replace_expression_ids.py: 80%
22 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-11-06 23:27 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-11-06 23:27 +0000
1from typing import List
2import re
5def get_formula_keys(s="") -> List[str]:
6 if not s: 6 ↛ 7line 6 didn't jump to line 7 because the condition on line 6 was never true
7 return []
8 props = re.findall(r'\(prop[^)]*\)', s)
9 props = [p[1:-1] for p in props]
10 return props
13def extract_id_from_formula_key(prop: str) -> int | None:
14 match = re.search(r'\d+', prop)
15 if match: 15 ↛ 17line 15 didn't jump to line 17 because the condition on line 15 was always true
16 return int(match.group())
17 return None
19def replace_props(text, new_props):
20 # Find all old (prop...) patterns
21 matches = list(re.finditer(r'\(prop[^)]*\)', text))
23 if len(matches) != len(new_props): 23 ↛ 24line 23 didn't jump to line 24 because the condition on line 23 was never true
24 raise ValueError("Number of props does not match number of replacements")
26 # Replace each match with the corresponding new prop
27 new_text = text
28 for match, new_prop in zip(reversed(matches), reversed(new_props)):
29 start, end = match.span()
30 # Insert the new prop (wrapped in parentheses)
31 new_text = new_text[:start] + f'({new_prop})' + new_text[end:]
33 return new_text