laboneq.workflow.blocks
¶
Workflow block definitions.
Block(parameters=None)
¶
Bases: ABC
A base class for workflow blocks.
A block can be an individual task or a collection of other blocks.
Classes inheriting from Block
must define the following methods:
- `execute()`: A method that executes the block and it's children defined
in `Block.body`.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameters
|
dict | None
|
Expected input parameters of the block. |
None
|
body: list[Block]
property
¶
Body of the block.
A list of other blocks that are defined within this block.
hidden: bool
property
¶
Whether or not the block is a hidden block.
Hidden blocks are generally used only for execution and they are not deemed relevant in results.
name: str
property
¶
Name of the block.
parameters: dict
property
¶
Input parameters of the block.
collect()
¶
Collect blocks defined within the context.
Every block defined within the context are added to the body of the block.
execute(executor)
abstractmethod
¶
Execute the block.
extend(blocks)
¶
Extend the body of the block.
iter_child_blocks()
¶
Iterate over the children of this block.
BlockVisitor()
¶
A block tree visitor.
This class is meant to be subclassed.
Subclasses can implement custom visitor method for each block type.
The format for naming the methods are visit_<block type lowercase>
For example, to write a visitor method for `WorkflowBlock`:
`def visit_workflowblock(self, block):`
The visitor method should visit or call .generic_visit()
to visit the
children.
BreakLoopBlock(parameters=None)
¶
ForExpression(values, loop_indexer=None)
¶
Bases: Block
For expression.
A block that iterates workflow blocks over the given values.
The expression will always fully execute regardless if the workflow is partially executed or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Iterable | Reference
|
An iterable. Iterable can contain workflow objects. |
required |
loop_indexer
|
Callable[[object], object] | None
|
A callable to produce dynamic loop indexes for each item.
The callable must accept a single argument, the item currently being
iterated.
The index should be a hashable and unique across the items in the loop.
Usually they are simple Python types such as
When |
None
|
IFExpression(condition)
¶
Namespace(**kwargs)
¶
A class to provide attribute access to an immutable namespace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
object
|
Attributes to set to the namespace. |
{}
|
ReturnStatement(callback, /, **kwargs)
¶
Bases: Block
Return statement for a workflow.
Sets the active workflow block output and interrupts the current workflow execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback
|
Callable
|
Callback producing the value to be set for the workflow output. |
required |
**kwargs
|
object
|
Keyword arguments for the callback. |
{}
|
callback = callback
instance-attribute
¶
execute(executor)
¶
Execute the block.
from_value(value)
classmethod
¶
Construct a return statement that returns the just given value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
object
|
The value to be returned as the workflow output. |
required |
Returns:
Name | Type | Description |
---|---|---|
block |
ReturnStatement
|
A return block. |
TaskBlock(task, parameters=None)
¶
WorkflowBlock(name, options_type=WorkflowOptions, parameters=None)
¶
Bases: Block
Workflow block.
name: str
property
¶
Name of the block.
options_type: type[WorkflowOptions]
property
¶
Type of workflow options.
ref: Reference
property
¶
Reference to the object.
create_options()
¶
Create options for the block.
The method goes over the sub-blocks and finds which blocks has options available.
Same task option instance is shared across the same named task executions per workflow, therefore the same task calls within a single workflow block cannot have multiple option definitions.
Returns:
Type | Description |
---|---|
WorkflowOptions
|
Workflow options where |
execute(executor)
¶
Execute the block.
from_callable(name, func, **kwargs)
classmethod
¶
Create the block from a callable.
By default the signature of the function is used to define the default parameters of the block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the block. |
required |
func
|
Callable
|
A function defining the workflow |
required |
**kwargs
|
object
|
Default parameter values that overwrite function signature |
{}
|
break_()
¶
Break statement to break out of the currently running innermost loop.
The equivalent of Python's break
statement.
Raises:
Type | Description |
---|---|
WorkflowError
|
Defined outside of loop scope. |
elif_(condition)
¶
Workflow else if statement.
The equivalent of Python's elif-statement.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
condition
|
Any
|
A condition that has to be true for code block to be executed. |
required |
Example
from laboneq import workflow
@workflow.workflow
def a_workflow(x):
with workflow.if_(x == 1):
...
with workflow.elif_(x == 2):
...
Raises:
Type | Description |
---|---|
WorkflowError
|
Expression is defined without |
else_()
¶
Workflow else statement.
The equivalent of Python's else-statement.
Example
from laboneq import workflow
@workflow.workflow
def a_workflow(x):
with workflow.if_(x == 1):
...
with workflow.elif_(x == 2):
...
with workflow.else_():
...
Raises:
Type | Description |
---|---|
WorkflowError
|
Expression is defined without |
for_(values, loop_indexer=None)
¶
For expression to iterate over the values within a code block.
The equivalent of Python's for loop.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Iterable[T]
|
An iterable. |
required |
loop_indexer
|
Callable[[object], str] | None
|
A callable to produce dynamic loop indexes for each item.
The callable must accept a single argument, the item currently being
iterated.
The index should be a hashable and unique across the items in the loop.
Usually they are simple Python types such as
When |
None
|
Example
with workflow.for_([1, 2, 3]) as x:
...
if_(condition)
¶
Workflow if statement.
The equivalent of Python's if-statement.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
condition
|
Any
|
A condition that has to be true for code block to be executed. |
required |
Example
from laboneq import workflow
@workflow.workflow
def a_workflow(x):
with workflow.if_(x == 1):
...
return_(output=None, /, **kwargs)
¶
Return statement of an workflow.
Interrupts the current workflow execution and sets the active workflow output
value either to the given output
value or to a Namespace
object by using
the given keyword arguments.
The equivalent of Python's return
statement.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
Any | None
|
Value to be set for workflow output. |
None
|
**kwargs
|
object
|
Keyword arguments that are passed to the |
{}
|
Raises:
Type | Description |
---|---|
TypeError
|
Function is called with positional and keyword arguments. |