Skip to content

Commit 1c7e98b

Browse files
committed
feat(preprocessing/transformers): implement fluid pipelining of transformers
1 parent 736ed0a commit 1c7e98b

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

boiling_learning/preprocessing/transformers.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__ import annotations
22

3-
from typing import Any, Callable, Generic, TypeVar
3+
from typing import Any, Callable, Generic, Tuple, TypeVar
44

55
from typing_extensions import Protocol
66

77
from boiling_learning.io import json
88
from boiling_learning.utils.descriptions import describe
99
from boiling_learning.utils.functional import Pack
10+
from boiling_learning.utils.lazy import Lazy
1011

1112
_X_contra = TypeVar('_X_contra', contravariant=True)
1213
_Y_co = TypeVar('_Y_co', covariant=True)
@@ -31,6 +32,29 @@ def __str__(self) -> str:
3132
arguments = f'{args}, {kwargs}' if args and kwargs else args or kwargs
3233
return f'<{self.__class__.__name__} ({arguments})>'
3334

35+
def __ror__(self, arg: _X) -> _Y:
36+
return _Transformed(arg, self)
37+
38+
39+
class _Transformed(Lazy[_Y]):
40+
def __init__(self, arg: _X, transformer: Transformer[_X, _Y]) -> None:
41+
if isinstance(arg, _Transformed):
42+
super().__init__(lambda: transformer(arg()))
43+
else:
44+
super().__init__(lambda: transformer(arg))
45+
46+
self._arg = arg
47+
self._transformer = transformer
48+
49+
def __describe__(self) -> json.JSONDataType:
50+
return describe(self._pipe())
51+
52+
def _pipe(self) -> Tuple[Any, ...]:
53+
if isinstance(self._arg, _Transformed):
54+
return (*self._arg._pipe(), self._transformer) # pylint: disable=protected-access
55+
else:
56+
return (self._arg, self._transformer)
57+
3458

3559
# the concept of mathematical operator as a function mapping a set to itself
3660
Operator = Transformer[_X, _X]

0 commit comments

Comments
 (0)