- Building blocks
- Python steps
Building blocks
Python steps
A Python step is a function. Use it for everything that must be exact.
A Python step runs your code as one step of a workflow. No model is involved. Use it for rules, calculations, API calls and anything with a consequence, such as sending or saving.
import avalanche as ava
@ava.step
def invoice_total(items: list[LineItem], discount_percent: int = 0) -> int:
"""Amount due in cents."""
subtotal = sum(item.quantity * item.unit_price_cents for item in items)
return subtotal * (100 - discount_percent) // 100
The parameters are the step’s inputs. The return value is its output, passed to the next step.
The docstring is shown on the step’s card in the UI. async def works too.
Three decorators, one kind of step
All three run plain Python. The name tells the reader what the step is for.
| Decorator | Use it for |
|---|---|
@ava.source | Bringing data in |
@ava.step | Working on data |
@ava.dest | Sending or saving a result |
@ava.source
def load_orders() -> list[Order]: ...
@ava.step
def keep_unpaid(orders: list[Order]) -> list[Order]: ...
@ava.dest
def send_reminders(orders: list[Order]) -> int: ...
@ava.workflow
def reminders():
return load_orders() >> keep_unpaid() >> send_reminders()
Rendering diagram…
Options
@ava.step(retries=2, slug="total")
def invoice_total(...): ...
| Option | Default | What it does |
|---|---|---|
retries | 7 | Extra attempts after a failure. |
on_uncertain | "retry" | What to do when it is unknown whether the step’s action happened: "retry", "hold" or "fail". |
slug | function name | The step’s name in runs and reruns. |
num_returns | 1 | Set to 2 or more when the step returns several values used separately. |
integrations | none | Connectors the step calls. @ava.step only. |
What a step can ask for
A step receives more than the previous step’s output. Declare a parameter and Avalanche fills it.
@ava.step
def load(config: InvoiceInput, ctx: ava.RunContext, log=ava.Logger()) -> str:
log.info(f"run {ctx.run_id}")
return config.text
| Parameter | You get |
|---|---|
| Typed with the workflow’s input model | The run input |
ava.RunContext | The run ID, workflow name and step name |
ava.Logger() as a default | A logger whose lines appear in the run’s logs |
Stop without failing
@ava.step
def score(lead: Lead) -> Score:
if not lead.qualified:
ava.skip("Lead is not qualified")
...
ava.skip ends the step as skipped. The run still succeeds, and the reason is shown on the step.
