Skip to content

Commit 931848e

Browse files
Update FormulaBuilder supporting new formula steps
Adds the ability to push the consumption and production operators the operation stack using the `push_oper` function. Signed-off-by: Matthias Wende <[email protected]>
1 parent e01d5c9 commit 931848e

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/frequenz/sdk/timeseries/formula_engine/_formula_engine.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
Adder,
2626
Clipper,
2727
ConstantValue,
28+
Consumption,
2829
Divider,
2930
FormulaStep,
3031
Maximizer,
3132
MetricFetcher,
3233
Minimizer,
3334
Multiplier,
3435
OpenParen,
36+
Production,
3537
Subtractor,
3638
)
3739
from ._tokenizer import TokenType
@@ -41,12 +43,14 @@
4143
_operator_precedence = {
4244
"max": 0,
4345
"min": 1,
44-
"(": 2,
45-
"/": 3,
46-
"*": 4,
47-
"-": 5,
48-
"+": 6,
49-
")": 7,
46+
"consumption": 2,
47+
"production": 3,
48+
"(": 4,
49+
"/": 5,
50+
"*": 6,
51+
"-": 7,
52+
"+": 8,
53+
")": 9,
5054
}
5155
"""The dictionary of operator precedence for the shunting yard algorithm."""
5256

@@ -574,7 +578,7 @@ def __init__(self, name: str, create_method: Callable[[float], QuantityT]) -> No
574578
self._steps: list[FormulaStep] = []
575579
self._metric_fetchers: dict[str, MetricFetcher[QuantityT]] = {}
576580

577-
def push_oper(self, oper: str) -> None:
581+
def push_oper(self, oper: str) -> None: # pylint: disable=too-many-branches
578582
"""Push an operator into the engine.
579583
580584
Args:
@@ -608,6 +612,10 @@ def push_oper(self, oper: str) -> None:
608612
self._build_stack.append(Maximizer())
609613
elif oper == "min":
610614
self._build_stack.append(Minimizer())
615+
elif oper == "consumption":
616+
self._build_stack.append(Consumption())
617+
elif oper == "production":
618+
self._build_stack.append(Production())
611619

612620
def push_metric(
613621
self,

0 commit comments

Comments
 (0)