Upload operator.py with huggingface_hub
Browse files2 unmodified lines34567822 unmodified lines313233343536373839404117 unmodified lines596061626364656667686970711 unmodified line7374757677787980818283848586872 unmodified lines909192939495969798991001011021031041056 unmodified lines11211311411511611711811911 unmodified lines13113213313413513620 unmodified lines15715815916016116216316414 unmodified lines17918018118218318413 unmodified lines19819920020120220312 unmodified lines21621721821922022113 unmodified lines2352362372382392402 unmodified lines2432442452462472482492502512522532548 unmodified lines26326426526626726826927027127230 unmodified lines3033043053063073088 unmodified lines31731831932032132210 unmodified lines3333343353363373383393402 unmodified linesfrom typing import Any, Dict, Generator, List, Optional, Unionfrom .artifact import Artifactfrom .stream import MultiStream, Stream22 unmodified linesclass StreamingOperator(Artifact):"""Base class for all stream operators in the streaming model.Stream operators are a key component of the streaming model and are responsible for processing continuous data streams.They perform operations such as transformations, aggregations, joins, windowing and more on these streams.There are several types of stream operators, including source operators, processing operators, etc.As a `StreamingOperator`, this class is responsible for performing operations on a stream, and must be implemented by all other specific types of stream operators in the system.When called, a `StreamingOperator` must return a MultiStream.17 unmodified linesclass StreamSource(StreamingOperator):"""A class representing a stream source operator in the streaming system.A stream source operator is a special type of `StreamingOperator` that generates a data stream without taking any input streams. It serves as the starting point in a stream processing pipeline, providing the initial data that other operators in the pipeline can process.When called, a `StreamSource` should generate a `MultiStream`. This behavior must be implemented by any classes that inherit from `StreamSource`."""@abstractmethoddef __call__(self) -> MultiStream:pass1 unmodified lineclass SourceOperator(StreamSource):"""A class representing a source operator in the streaming system.A source operator is responsible for generating the data stream from some source, such as a database or a file. This is the starting point of a stream processing pipeline. The `SourceOperator` class is a type of `StreamSource`, which is a special type of `StreamingOperator` that generates an output stream but does not take any input streams.When called, a `SourceOperator` invokes its `process` method, which should be implemented by all subclasses to generate the required `MultiStream`."""def __call__(self) -> MultiStream:return self.process()@abstractmethoddef process(self) -> MultiStream:2 unmodified linesclass StreamInitializerOperator(StreamSource):"""A class representing a stream initializer operator in the streaming system.A stream initializer operator is a special type of `StreamSource` that is capable of taking parameters during the stream generation process. This can be useful in situations where the stream generation process needs to be customized or configured based on certain parameters.When called, a `StreamInitializerOperator` invokes its `process` method, passing any supplied arguments and keyword arguments. The `process` method should be implemented by all subclasses to generate the required `MultiStream` based on the given arguments and keyword arguments."""def __call__(self, *args, **kwargs) -> MultiStream:return self.process(*args, **kwargs)@abstractmethoddef process(self, *args, **kwargs) -> MultiStream:6 unmodified linesA multi-stream operator is a type of `StreamingOperator` that operates on an entire MultiStream object at once. It takes a `MultiStream` as input and produces a `MultiStream` as output. The `process` method should be implemented by subclasses to define the specific operations to be performed on the input `MultiStream`."""def __call__(self, multi_stream: Optional[MultiStream] = None) -> MultiStream:return self._process_multi_stream(multi_stream)def _process_multi_stream(self, multi_stream: Optional[MultiStream] = None) -> MultiStream:result = self.process(multi_stream)11 unmodified linesA single-stream operator is a type of `MultiStreamOperator` that operates on individual `Stream` objects within a `MultiStream`. It iterates through each `Stream` in the `MultiStream` and applies the `process` method. The `process` method should be implemented by subclasses to define the specific operations to be performed on each `Stream`."""def _process_multi_stream(self, multi_stream: MultiStream) -> MultiStream:result = {}for stream_name, stream in multi_stream.items():20 unmodified linesA paged-stream operator is a type of `SingleStreamOperator` that operates on a page of instances in a `Stream` at a time, where a page is a subset of instances. The `process` method should be implemented by subclasses to define the specific operations to be performed on each page."""page_size: int = 1000def _process_stream(self, stream: Stream, stream_name: str = None) -> Generator:page = []for instance in stream:14 unmodified linesA single-stream reducer is a type of `StreamingOperator` that operates on individual `Stream` objects within a `MultiStream` and reduces each `Stream` to a single output value. The `process` method should be implemented by subclasses to define the specific reduction operation to be performed on each `Stream`."""def __call__(self, multi_stream: Optional[MultiStream] = None) -> Dict[str, Any]:result = {}for stream_name, stream in multi_stream.items():13 unmodified linesA stream instance operator is a type of `SingleStreamOperator` that operates on individual instances within a `Stream`. It iterates through each instance in the `Stream` and applies the `process` method. The `process` method should be implemented by subclasses to define the specific operations to be performed on each instance."""def _process_stream(self, stream: Stream, stream_name: str = None) -> Generator:for instance in stream:yield self._process_instance(instance, stream_name)12 unmodified linesA stream instance operator validator is a type of `StreamInstanceOperator` that includes a validation step. It operates on individual instances within a `Stream` and validates the result of processing each instance."""@abstractmethoddef validate(self, instance):pass13 unmodified linesAn instance operator is a type of `Artifact` that operates on a single instance (represented as a dict) at a time. It takes an instance as input and produces a transformed instance as output."""def __call__(self, data: dict) -> dict:return self.process(data)2 unmodified linespassclass FieldOperator(Artifact):"""A class representing a field operator in the streaming system.A field operator is a type of `Artifact` that operates on a single field within an instance. It takes an instance and a field name as input, processes the field, and updates the field in the instance with the processed value."""def __call__(self, data: Dict[str, Any], field: str) -> dict:value = self.process(data[field])data[field] = value8 unmodified lines"""A class representing an instance operator with global access in the streaming system.An instance operator with global access is a type of `StreamingOperator` that operates on individual instances within a `Stream` and can also access other streams.It uses the `accessible_streams` attribute to determine which other streams it has access to.In order to make this efficient and to avoid qudratic complexity, it caches the accessible streams by default."""accessible_streams: Union[MultiStream, List[str]] = Nonecache_accessible_streams: bool = True30 unmodified linesA sequential operator is a type of `MultiStreamOperator` that applies a sequence of other operators to a `MultiStream`. It maintains a list of `StreamingOperator`s and applies them in order to the `MultiStream`."""steps: List[StreamingOperator] = field(default_factory=list)def process(self, multi_stream: Optional[MultiStream] = None) -> MultiStream:8 unmodified linesA source sequential operator is a type of `SequntialOperator` that starts with a source operator. The first operator in its list of steps is a `StreamSource`, which generates the initial `MultiStream` that the other operators then process."""def __call__(self) -> MultiStream:return super().__call__()10 unmodified linesA sequential operator initializer is a type of `SequntialOperator` that starts with a stream initializer operator. The first operator in its list of steps is a `StreamInitializerOperator`, which generates the initial `MultiStream` based on the provided arguments and keyword arguments."""def __call__(self, *args, **kwargs) -> MultiStream:return self.process(*args, **kwargs)def process(self, *args, **kwargs) -> MultiStream:assert isinstance(2 unmodified lines345678922 unmodified lines323334353637383940414217 unmodified lines60616263646566676869707172731 unmodified line75767778798081828384858687888990912 unmodified lines9495969798991001011021031041051061071081091106 unmodified lines11711811912012112212312412512611 unmodified lines13813914014114214314420 unmodified lines16516616716816917017117217314 unmodified lines18818919019119219319413 unmodified lines20820921021121221321412 unmodified lines22722822923023123223313 unmodified lines2472482492502512522532 unmodified lines2562572582592602612622632642652662672688 unmodified lines27727827928028128228328428528628730 unmodified lines3183193203213223233248 unmodified lines33333433533633733833910 unmodified lines3503513523533543553563573583592 unmodified linesfrom typing import Any, Dict, Generator, List, Optional, Unionfrom .artifact import Artifactfrom .random_utils import nested_seedfrom .stream import MultiStream, Stream22 unmodified linesclass StreamingOperator(Artifact):"""Base class for all stream operators in the streaming model.Stream operators are a key component of the streaming model and are responsible for processing continuous data streams.They perform operations such as transformations, aggregations, joins, windowing and more on these streams.There are several types of stream operators, including source operators, processing operators, etc.As a `StreamingOperator`, this class is responsible for performing operations on a stream, and must be implemented by all other specific types of stream operators in the system.When called, a `StreamingOperator` must return a MultiStream.17 unmodified linesclass StreamSource(StreamingOperator):"""A class representing a stream source operator in the streaming system.A stream source operator is a special type of `StreamingOperator` that generates a data stream without taking any input streams. It serves as the starting point in a stream processing pipeline, providing the initial data that other operators in the pipeline can process.When called, a `StreamSource` should generate a `MultiStream`. This behavior must be implemented by any classes that inherit from `StreamSource`."""@abstractmethoddef __call__(self) -> MultiStream:pass1 unmodified lineclass SourceOperator(StreamSource):"""A class representing a source operator in the streaming system.A source operator is responsible for generating the data stream from some source, such as a database or a file. This is the starting point of a stream processing pipeline. The `SourceOperator` class is a type of `StreamSource`, which is a special type of `StreamingOperator` that generates an output stream but does not take any input streams.When called, a `SourceOperator` invokes its `process` method, which should be implemented by all subclasses to generate the required `MultiStream`."""def __call__(self) -> MultiStream:with nested_seed():return self.process()@abstractmethoddef process(self) -> MultiStream:2 unmodified linesclass StreamInitializerOperator(StreamSource):"""A class representing a stream initializer operator in the streaming system.A stream initializer operator is a special type of `StreamSource` that is capable of taking parameters during the stream generation process. This can be useful in situations where the stream generation process needs to be customized or configured based on certain parameters.When called, a `StreamInitializerOperator` invokes its `process` method, passing any supplied arguments and keyword arguments. The `process` method should be implemented by all subclasses to generate the required `MultiStream` based on the given arguments and keyword arguments."""def __call__(self, *args, **kwargs) -> MultiStream:with nested_seed():return self.process(*args, **kwargs)@abstractmethoddef process(self, *args, **kwargs) -> MultiStream:6 unmodified linesA multi-stream operator is a type of `StreamingOperator` that operates on an entire MultiStream object at once. It takes a `MultiStream` as input and produces a `MultiStream` as output. The `process` method should be implemented by subclasses to define the specific operations to be performed on the input `MultiStream`."""def __call__(self, multi_stream: Optional[MultiStream] = None) -> MultiStream:with nested_seed():return self._process_multi_stream(multi_stream)def _process_multi_stream(self, multi_stream: Optional[MultiStream] = None) -> MultiStream:result = self.process(multi_stream)11 unmodified linesA single-stream operator is a type of `MultiStreamOperator` that operates on individual `Stream` objects within a `MultiStream`. It iterates through each `Stream` in the `MultiStream` and applies the `process` method. The `process` method should be implemented by subclasses to define the specific operations to be performed on each `Stream`."""def _process_multi_stream(self, multi_stream: MultiStream) -> MultiStream:result = {}for stream_name, stream in multi_stream.items():20 unmodified linesA paged-stream operator is a type of `SingleStreamOperator` that operates on a page of instances in a `Stream` at a time, where a page is a subset of instances. The `process` method should be implemented by subclasses to define the specific operations to be performed on each page."""page_size: int = 1000def _process_stream(self, stream: Stream, stream_name: str = None) -> Generator:page = []for instance in stream:14 unmodified linesA single-stream reducer is a type of `StreamingOperator` that operates on individual `Stream` objects within a `MultiStream` and reduces each `Stream` to a single output value. The `process` method should be implemented by subclasses to define the specific reduction operation to be performed on each `Stream`."""def __call__(self, multi_stream: Optional[MultiStream] = None) -> Dict[str, Any]:result = {}for stream_name, stream in multi_stream.items():13 unmodified linesA stream instance operator is a type of `SingleStreamOperator` that operates on individual instances within a `Stream`. It iterates through each instance in the `Stream` and applies the `process` method. The `process` method should be implemented by subclasses to define the specific operations to be performed on each instance."""def _process_stream(self, stream: Stream, stream_name: str = None) -> Generator:for instance in stream:yield self._process_instance(instance, stream_name)12 unmodified linesA stream instance operator validator is a type of `StreamInstanceOperator` that includes a validation step. It operates on individual instances within a `Stream` and validates the result of processing each instance."""@abstractmethoddef validate(self, instance):pass13 unmodified linesAn instance operator is a type of `Artifact` that operates on a single instance (represented as a dict) at a time. It takes an instance as input and produces a transformed instance as output."""def __call__(self, data: dict) -> dict:return self.process(data)2 unmodified linespassclass BaseFieldOperator(Artifact):"""A class representing a field operator in the streaming system.A field operator is a type of `Artifact` that operates on a single field within an instance. It takes an instance and a field name as input, processes the field, and updates the field in the instance with the processed value."""def __call__(self, data: Dict[str, Any], field: str) -> dict:value = self.process(data[field])data[field] = value8 unmodified lines"""A class representing an instance operator with global access in the streaming system.An instance operator with global access is a type of `StreamingOperator` that operates on individual instances within a `Stream` and can also access other streams.It uses the `accessible_streams` attribute to determine which other streams it has access to.In order to make this efficient and to avoid qudratic complexity, it caches the accessible streams by default."""accessible_streams: Union[MultiStream, List[str]] = Nonecache_accessible_streams: bool = True30 unmodified linesA sequential operator is a type of `MultiStreamOperator` that applies a sequence of other operators to a `MultiStream`. It maintains a list of `StreamingOperator`s and applies them in order to the `MultiStream`."""steps: List[StreamingOperator] = field(default_factory=list)def process(self, multi_stream: Optional[MultiStream] = None) -> MultiStream:8 unmodified linesA source sequential operator is a type of `SequntialOperator` that starts with a source operator. The first operator in its list of steps is a `StreamSource`, which generates the initial `MultiStream` that the other operators then process."""def __call__(self) -> MultiStream:return super().__call__()10 unmodified linesA sequential operator initializer is a type of `SequntialOperator` that starts with a stream initializer operator. The first operator in its list of steps is a `StreamInitializerOperator`, which generates the initial `MultiStream` based on the provided arguments and keyword arguments."""def __call__(self, *args, **kwargs) -> MultiStream:with nested_seed():return self.process(*args, **kwargs)def process(self, *args, **kwargs) -> MultiStream:assert isinstance(
operator.py+34-15