Coverage for backend/ahuora-builder/src/ahuora_builder/methods/scaling_suffix.py: 66%
18 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-03-26 20:57 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-03-26 20:57 +0000
1from pyomo.core.base.suffix import Suffix
4def sanitize_scaling_suffix(
5 block,
6 *,
7 print_removed: bool = False,
8 max_print: int = 20,
9) -> int:
10 """
11 Remove invalid entries from `scaling_factor` suffix objects.
12 An invalid entry is usually None values that will give typing errors
13 in nl_writer.
15 Returns:
16 Number of removed entries.
17 """
18 removed_entries = []
20 for suffix in block.component_objects(Suffix, active=True, descend_into=True):
21 if suffix.local_name != "scaling_factor":
22 continue
24 invalid_keys = []
25 for key, value in list(suffix.items()):
26 if value is None or not isinstance(value, (int, float)): 26 ↛ 27line 26 didn't jump to line 27 because the condition on line 26 was never true
27 invalid_keys.append((key, value))
29 for key, value in invalid_keys: 29 ↛ 30line 29 didn't jump to line 30 because the loop on line 29 never started
30 suffix.clear_value(key)
31 removed_entries.append((suffix.name, str(key), value))
33 if print_removed and removed_entries: 33 ↛ 34line 33 didn't jump to line 34 because the condition on line 33 was never true
34 print(
35 f"Removed {len(removed_entries)} invalid scaling_factor suffix entries."
36 )
37 for suffix_name, key_name, value in removed_entries[:max_print]:
38 print(f"suffix={suffix_name}, key={key_name}, value={value}")
40 return len(removed_entries)