Tutorial — Level 2: Adding a Transformation¶
Now we insert a step that converts each character to lowercase. This introduces EACH mode — the step is called once per item.
from typing import NamedTuple
from synaflow import pipeline, step, run
class Params(NamedTuple):
message: str
def hello(message: str) -> list[str]:
return list(message)
def lowercase(hello: str) -> str:
return hello.lower()
def printer(lowercase: list[str]) -> None:
print(lowercase)
p = pipeline(
name="tutorial",
params=Params,
steps=[
step("hello", fn=hello),
step("lowercase", fn=lowercase),
step("printer", fn=printer),
],
)
run(p, Params(message="SynaFlow"))
# Output: ['s', 'y', 'n', 'a', 'f', 'l', 'o', 'w']
from typing import NamedTuple
from synaflow import pipeline, step, async_run
class Params(NamedTuple):
message: str
async def hello(message: str) -> list[str]:
return list(message)
async def lowercase(hello: str) -> str:
return hello.lower()
async def printer(lowercase: list[str]) -> None:
print(lowercase)
p = pipeline(
name="tutorial",
params=Params,
steps=[
step("hello", fn=hello),
step("lowercase", fn=lowercase),
step("printer", fn=printer),
],
)
async_run(p, Params(message="SynaFlow"))
# Output: ['s', 'y', 'n', 'a', 'f', 'l', 'o', 'w']
What happened?
hellooutputslist[str]— a collection of 8 characters.lowercaseasks forstr— a scalar. Becausehellooutputs an iterable andlowercaseexpects a scalar, SynaFlow selects EACH mode and callslowercaseonce per character.- EACH-mode outputs are automatically collected into a list.
printerreceiveslist[str]in ALL mode and prints it.
flowchart TD
hello["hello<br/><i>list[str]</i>"]
lowercase["lowercase<br/><i>ListType(str)</i>"]
printer["printer<br/><i>None</i>"]
message --> hello
hello --> lowercase
lowercase --> printer
EACH mode wraps the output type in
ListType.lowercasereturnsstrper item, so its output type isListType(str)— meaning "a list of strings".
You can force the mode explicitly:
Next¶
Count the characters in Level 3.