Coverage for backend/idaes_factory/adapters/convert_expression.py: 88%

12 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-11-06 23:27 +0000

1import re 

2 

3def convert_expression(expression: str | None) -> str: 

4 if expression is None: 4 ↛ 5line 4 didn't jump to line 5 because the condition on line 4 was never true

5 raise ValueError("Unexpected null expression") 

6 

7 def replacer(match): 

8 id = match.group(1) # Get the id from the match 

9 if id.startswith("prop"): 

10 # Convert "prop5000" to "id_5000" 

11 return "id_" + id[4:] 

12 

13 # Ignore ids that don't start with "prop" 

14 return "" 

15 

16 # Regex to match @[Display Text](id) 

17 pattern = r"@\[[^\]]+\]\(([^)]+)\)" 

18 transformed_expression = re.sub(pattern, replacer, expression) 

19 

20 # Clean up any extra spaces or operators left behind 

21 return transformed_expression