Skip to content

Commit 631e5d2

Browse files
committed
move FunCall class from _ast to _functions module
This enables us to import _ast from _functions, without it being a circular dependency. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 298df08 commit 631e5d2

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

src/frequenz/sdk/timeseries/formulas/_ast.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from ..._internal._math import is_close_to_zero
1818
from .._base_types import QuantityT, Sample
1919
from ._base_ast_node import AstNode, NodeSynchronizer
20-
from ._functions import Function
2120

2221
_logger = logging.getLogger(__name__)
2322

@@ -104,33 +103,6 @@ def _is_quantity_sample(
104103
return isinstance(sample.value, Quantity)
105104

106105

107-
@dataclass(kw_only=True)
108-
class FunCall(AstNode[QuantityT]):
109-
"""A function call in the formula."""
110-
111-
function: Function[QuantityT]
112-
113-
@override
114-
async def evaluate(self) -> Sample[QuantityT] | QuantityT | None:
115-
"""Evaluate the function call with its arguments."""
116-
return await self.function()
117-
118-
@override
119-
def format(self, wrap: bool = False) -> str:
120-
"""Return a string representation of the function call node."""
121-
return self.function.format()
122-
123-
@override
124-
async def subscribe(self) -> None:
125-
"""Subscribe to any data streams needed by the function."""
126-
await self.function.subscribe()
127-
128-
@override
129-
async def unsubscribe(self) -> None:
130-
"""Unsubscribe from any data streams needed by the function."""
131-
await self.function.unsubscribe()
132-
133-
134106
@dataclass(kw_only=True)
135107
class Constant(AstNode[QuantityT]):
136108
"""A constant numerical value in the formula."""

src/frequenz/sdk/timeseries/formulas/_formula.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from . import _ast
2323
from ._base_ast_node import AstNode
2424
from ._formula_evaluator import FormulaEvaluatingActor
25-
from ._functions import Coalesce, Max, Min
25+
from ._functions import Coalesce, FunCall, Max, Min
2626

2727
_logger = logging.getLogger(__name__)
2828

@@ -299,7 +299,7 @@ def coalesce(
299299
else:
300300
right_nodes.append(_ast.Constant(value=item))
301301

302-
new_root = _ast.FunCall(
302+
new_root = FunCall(
303303
function=Coalesce([self.root] + right_nodes),
304304
)
305305

@@ -332,7 +332,7 @@ def min(
332332
else:
333333
right_nodes.append(_ast.Constant(value=item))
334334

335-
new_root = _ast.FunCall(
335+
new_root = FunCall(
336336
function=Min([self.root] + right_nodes),
337337
)
338338

@@ -365,7 +365,7 @@ def max(
365365
else:
366366
right_nodes.append(_ast.Constant(value=item))
367367

368-
new_root = _ast.FunCall(
368+
new_root = FunCall(
369369
function=Max([self.root] + right_nodes),
370370
)
371371

src/frequenz/sdk/timeseries/formulas/_functions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@
1818
from ._base_ast_node import AstNode, NodeSynchronizer
1919

2020

21+
@dataclass(kw_only=True)
22+
class FunCall(AstNode[QuantityT]):
23+
"""A function call in the formula."""
24+
25+
function: Function[QuantityT]
26+
27+
@override
28+
async def evaluate(self) -> Sample[QuantityT] | QuantityT | None:
29+
"""Evaluate the function call with its arguments."""
30+
return await self.function()
31+
32+
@override
33+
def format(self, wrap: bool = False) -> str:
34+
"""Return a string representation of the function call node."""
35+
return self.function.format()
36+
37+
@override
38+
async def subscribe(self) -> None:
39+
"""Subscribe to any data streams needed by the function."""
40+
await self.function.subscribe()
41+
42+
@override
43+
async def unsubscribe(self) -> None:
44+
"""Unsubscribe from any data streams needed by the function."""
45+
await self.function.unsubscribe()
46+
47+
2148
@dataclass
2249
class Function(abc.ABC, Generic[QuantityT]):
2350
"""A function that can be called in a formula expression."""

src/frequenz/sdk/timeseries/formulas/_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from . import _ast, _token
2020
from ._base_ast_node import AstNode
2121
from ._formula import Formula
22-
from ._functions import Function
22+
from ._functions import FunCall, Function
2323
from ._lexer import Lexer
2424
from ._peekable import Peekable
2525
from ._resampled_stream_fetcher import ResampledStreamFetcher
@@ -177,7 +177,7 @@ def _parse_function_call(self) -> AstNode[QuantityT] | None:
177177
f"Expected ',' or ')' in function call at position {fn_name.span}"
178178
)
179179

180-
return _ast.FunCall(
180+
return FunCall(
181181
span=fn_name.span,
182182
function=Function.from_string(fn_name.value, params),
183183
)

0 commit comments

Comments
 (0)