Skip to content

Commit 5d828fc

Browse files
committed
Remove custom formula channels and receivers
We will use regular broadcast channels, and provide composability on FormulaEngine instances instead. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent d743d77 commit 5d828fc

File tree

1 file changed

+0
-210
lines changed

1 file changed

+0
-210
lines changed

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

Lines changed: 0 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -416,192 +416,6 @@ def build(self) -> FormulaEngine:
416416
)
417417

418418

419-
class _BaseFormulaChannel(
420-
Generic[_GenericSample, _GenericEngine],
421-
Broadcast[_GenericSample],
422-
ABC,
423-
):
424-
"""A broadcast channel implementation for use with formulas."""
425-
426-
ReceiverType: Type[FormulaReceiver | FormulaReceiver3Phase]
427-
428-
def __init__(
429-
self, name: str, engine: _GenericEngine, resend_latest: bool = False
430-
) -> None:
431-
"""Create a `FormulaChannel` instance.
432-
433-
Args:
434-
name: A name for the channel.
435-
engine: A FormulaEngine instance that produces values for this channel.
436-
resend_latest: Whether to resend latest channel values to newly created
437-
receivers, like in `Broadcast` channels.
438-
"""
439-
self._engine: _GenericEngine = engine
440-
super().__init__(name, resend_latest)
441-
442-
@property
443-
def engine(self) -> _GenericEngine:
444-
"""Return the formula engine attached to the channel.
445-
446-
Returns:
447-
A FormulaEngine instance.
448-
"""
449-
return self._engine
450-
451-
def new_receiver(
452-
self, name: Optional[str] = None, maxsize: int = 50
453-
) -> _GenericFormulaReceiver:
454-
"""Create a new FormulaReceiver for the channel.
455-
456-
This implementation is similar to `Broadcast.new_receiver()`, except that it
457-
creates and returns a `FormulaReceiver`. The way the default name for the
458-
receiver is constructed, is also slightly tweaked.
459-
460-
Args:
461-
name: An optional name for the receiver.
462-
maxsize: size of the receiver's buffer.
463-
464-
Returns:
465-
A `FormulaReceiver` instance attached to the `FormulaChannel`.
466-
"""
467-
uuid = uuid4()
468-
if name is None:
469-
name = self.name
470-
recv = self.ReceiverType(uuid, name, maxsize, self)
471-
self.receivers[uuid] = weakref.ReferenceType(recv)
472-
if self._resend_latest and self._latest is not None:
473-
recv.enqueue(self._latest)
474-
return recv
475-
476-
477-
class _BaseFormulaReceiver(
478-
Generic[_GenericSample, _GenericEngine],
479-
BroadcastReceiver[_GenericSample],
480-
ABC,
481-
):
482-
"""A receiver to receive calculated `Sample`s from a Formula channel.
483-
484-
They function as regular channel receivers, but can be composed to form higher order
485-
formulas.
486-
"""
487-
488-
BuilderType: Type[HigherOrderFormulaBuilder | HigherOrderFormulaBuilder3Phase]
489-
490-
def __init__(
491-
self,
492-
uuid: UUID,
493-
name: str,
494-
maxsize: int,
495-
chan: _GenericFormulaChannel,
496-
) -> None:
497-
"""Create a `FormulaReceiver` instance.
498-
499-
Args:
500-
uuid: uuid to uniquely identify the receiver. Forwarded to
501-
BroadcastReceiver's `__init__` function.
502-
name: Name for the receiver.
503-
maxsize: Buffer size for the receiver.
504-
chan: The `FormulaChannel` instance that this receiver is attached to.
505-
"""
506-
self._engine = chan.engine
507-
super().__init__(uuid, name, maxsize, chan)
508-
509-
@property
510-
def name(self) -> str:
511-
"""Name of the receiver.
512-
513-
Returns:
514-
Name of the receiver.
515-
"""
516-
return self._name
517-
518-
@property
519-
def engine(self) -> _GenericEngine:
520-
"""Return the formula engine attached to the receiver.
521-
522-
Returns:
523-
Formula Engine attached to the receiver.
524-
"""
525-
return self._engine
526-
527-
# The use of `Self` is necessary for mypy to deduce the type of `clone` method in
528-
# the derived classes. With `from __future__ import annotations`, both CPython and
529-
# mypy accept this in python <= 3.10.
530-
#
531-
# Unfortunately pylint doesn't accept `Self` before python 3.11, even with `from
532-
# __future__ import annotations`. So the pylint `undefined-variable` check is
533-
# disabled to get `Self` to pass the checks.
534-
def clone(self) -> Self: # pylint: disable=undefined-variable
535-
"""Create a new receiver from the formula engine.
536-
537-
Returns:
538-
New `FormulaReceiver` streaming a copy of the formula engine output.
539-
"""
540-
return self._engine.new_receiver()
541-
542-
def __add__(
543-
self,
544-
other: _BaseFormulaReceiver | _GenericHOFormulaBuilder,
545-
) -> _GenericHOFormulaBuilder:
546-
"""Return a formula builder that adds (data in) `other` to `self`.
547-
548-
Args:
549-
other: A formula receiver, or a formula builder instance corresponding to a
550-
sub-expression.
551-
552-
Returns:
553-
A formula builder that can take further expressions, or can be built
554-
into a formula engine.
555-
"""
556-
return self.BuilderType(self) + other
557-
558-
def __sub__(
559-
self,
560-
other: _BaseFormulaReceiver | _GenericHOFormulaBuilder,
561-
) -> _GenericHOFormulaBuilder:
562-
"""Return a formula builder that subtracts (data in) `other` from `self`.
563-
564-
Args:
565-
other: A formula receiver, or a formula builder instance corresponding to a
566-
sub-expression.
567-
568-
Returns:
569-
A formula builder that can take further expressions, or can be built
570-
into a formula engine.
571-
"""
572-
return self.BuilderType(self) - other
573-
574-
def __mul__(
575-
self, other: _BaseFormulaReceiver | _GenericHOFormulaBuilder
576-
) -> _GenericHOFormulaBuilder:
577-
"""Return a formula builder that multiplies (data in) `self` with `other`.
578-
579-
Args:
580-
other: A formula receiver, or a formula builder instance corresponding to a
581-
sub-expression.
582-
583-
Returns:
584-
A formula builder that can take further expressions, or can be built
585-
into a formula engine.
586-
"""
587-
return self.BuilderType(self) * other
588-
589-
def __truediv__(
590-
self, other: _BaseFormulaReceiver | _GenericHOFormulaBuilder
591-
) -> _GenericHOFormulaBuilder:
592-
"""Return a formula builder that divides (data in) `self` by `other`.
593-
594-
Args:
595-
other: A formula receiver, or a formula builder instance corresponding to a
596-
sub-expression.
597-
598-
Returns:
599-
A formula builder that can take further expressions, or can be built
600-
into a formula engine.
601-
"""
602-
return self.BuilderType(self) / other
603-
604-
605419
class _BaseHOFormulaBuilder(
606420
ABC, Generic[_GenericFormulaReceiver, _GenericSample, _GenericEngine]
607421
):
@@ -795,27 +609,3 @@ def build(self, name: str, nones_are_zeros: bool = False) -> FormulaEngine3Phase
795609
),
796610
)
797611
return self._engine
798-
799-
800-
class FormulaReceiver(_BaseFormulaReceiver[Sample, FormulaEngine]):
801-
"""A specialization of the _BaseFormulaChannel for `Sample` objects."""
802-
803-
BuilderType = HigherOrderFormulaBuilder
804-
805-
806-
class FormulaReceiver3Phase(_BaseFormulaReceiver[Sample3Phase, FormulaEngine3Phase]):
807-
"""A specialization of the _BaseFormulaChannel for `Sample3Phase` objects."""
808-
809-
BuilderType = HigherOrderFormulaBuilder3Phase
810-
811-
812-
class FormulaChannel(_BaseFormulaChannel[Sample, FormulaEngine]):
813-
"""A specialization of the _BaseFormulaChannel for `Sample` objects."""
814-
815-
ReceiverType = FormulaReceiver
816-
817-
818-
class FormulaChannel3Phase(_BaseFormulaChannel[Sample3Phase, FormulaEngine3Phase]):
819-
"""A specialization of the _BaseFormulaChannel for `Sample3Phase` objects."""
820-
821-
ReceiverType = FormulaReceiver3Phase

0 commit comments

Comments
 (0)