Components

IOComponent

To start off, we’ll show how to work with probably the most common type of Component: IOComponent. IOComponents can be used for building components that require input and output streams.

Let’s take a look with the following example where we build a simple summation component that adds up all the data thatit receives over time.

from dataclasses import dataclass, field
from typing import List

from frater.component import ComponentState, IOComponent, IOComponentConfig, ComponentBuilder
from frater.stream import InputStream, OutputStream, StreamConfig, StreamState


@dataclass
class SummationComponentState(ComponentState):
    total: int = 0


class SummationComponent(IOComponent):
    def __init__(self, config: IOComponentConfig, input_stream: InputStream, output_stream: OutputStream):
        super(SummationComponent, self).__init__(config, input_stream, output_stream)

    def init_state(self):
        return SummationComponentState()

    def process(self, data):
        self.state.total += data
        return self.state.total

Here we’ve defined the SummationComponent with its corresponding state SummationComponentState that willhold the state info of