Skip to content

Commit e8c93d1

Browse files
Add min and max formula steps
Signed-off-by: Matthias Wende <[email protected]>
1 parent 1fe5cb8 commit e8c93d1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,52 @@ def apply(self, eval_stack: List[float]) -> None:
132132
eval_stack.append(res)
133133

134134

135+
class Maximizer(FormulaStep):
136+
"""A formula step that represents the max function."""
137+
138+
def __repr__(self) -> str:
139+
"""Return a string representation of the step.
140+
141+
Returns:
142+
A string representation of the step.
143+
"""
144+
return "max"
145+
146+
def apply(self, eval_stack: List[float]) -> None:
147+
"""Extract two values from the stack and pushes back the maximum.
148+
149+
Args:
150+
eval_stack: An evaluation stack, to apply the formula step on.
151+
"""
152+
val2 = eval_stack.pop()
153+
val1 = eval_stack.pop()
154+
res = max(val1, val2)
155+
eval_stack.append(res)
156+
157+
158+
class Minimizer(FormulaStep):
159+
"""A formula step that represents the min function."""
160+
161+
def __repr__(self) -> str:
162+
"""Return a string representation of the step.
163+
164+
Returns:
165+
A string representation of the step.
166+
"""
167+
return "min"
168+
169+
def apply(self, eval_stack: List[float]) -> None:
170+
"""Extract two values from the stack and pushes back the minimum.
171+
172+
Args:
173+
eval_stack: An evaluation stack, to apply the formula step on.
174+
"""
175+
val2 = eval_stack.pop()
176+
val1 = eval_stack.pop()
177+
res = min(val1, val2)
178+
eval_stack.append(res)
179+
180+
135181
class OpenParen(FormulaStep):
136182
"""A no-op formula step used while building a prefix formula engine.
137183

0 commit comments

Comments
 (0)