Skip to content

Commit fac5343

Browse files
committed
chore: example test of creating a custom transformer
1 parent a6b4fc3 commit fac5343

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/test_custom_transformer.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections.abc import Iterable
2+
from collections.abc import Iterator
3+
4+
from laygo.context.types import IContextManager
5+
from laygo.pipeline import Pipeline
6+
from laygo.transformers.types import BaseTransformer
7+
8+
# In should be an int
9+
10+
11+
class MultiplierTransformer(BaseTransformer[int, int]):
12+
def __call__(self, data: Iterable[int], context: IContextManager | None = None) -> Iterator[int]:
13+
"""
14+
Takes an iterable of data and yields each item multiplied.
15+
"""
16+
17+
multiplier = context["multiplier"] if context and "multiplier" in context else 1
18+
19+
for item in data:
20+
yield item * multiplier
21+
22+
23+
class TestCustomTransformer:
24+
def test_multiplier_transformer(self):
25+
data = [1, 2, 3, 4, 5]
26+
expected_output = [2, 4, 6, 8, 10]
27+
28+
result, _ = Pipeline(data).context({"multiplier": 2}).apply(MultiplierTransformer()).to_list()
29+
30+
assert result == expected_output, f"Expected {expected_output}, but got {result}"

0 commit comments

Comments
 (0)