# flytekit.core.condition

## Directory

### Classes

| Class | Description |
|-|-|
| [`BranchNode`](https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.condition/page.md#flytekitcoreconditionbranchnode) |  |
| [`Case`](https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.condition/page.md#flytekitcoreconditioncase) |  |
| [`Condition`](https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.condition/page.md#flytekitcoreconditioncondition) |  |
| [`ConditionalSection`](https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.condition/page.md#flytekitcoreconditionconditionalsection) | ConditionalSection is used to denote a condition within a Workflow. |
| [`LocalExecutedConditionalSection`](https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.condition/page.md#flytekitcoreconditionlocalexecutedconditionalsection) |  |
| [`SkippedConditionalSection`](https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.condition/page.md#flytekitcoreconditionskippedconditionalsection) | This ConditionalSection is used for nested conditionals, when the branch has been evaluated to false. |

### Methods

| Method | Description |
|-|-|
| [`conditional()`](#conditional) | Use a conditional section to control the flow of a workflow. |
| [`create_branch_node_promise_var()`](#create_branch_node_promise_var) | Generates a globally (wf-level) unique id for a variable. |
| [`merge_promises()`](#merge_promises) |  |
| [`to_branch_node()`](#to_branch_node) |  |
| [`to_case_block()`](#to_case_block) |  |
| [`to_ifelse_block()`](#to_ifelse_block) |  |
| [`transform_to_boolexpr()`](#transform_to_boolexpr) |  |
| [`transform_to_comp_expr()`](#transform_to_comp_expr) |  |
| [`transform_to_conj_expr()`](#transform_to_conj_expr) |  |
| [`transform_to_operand()`](#transform_to_operand) |  |

## Methods

#### conditional()

```python
def conditional(
    name: str,
) -> ConditionalSection
```
Use a conditional section to control the flow of a workflow. Conditional sections can only be used inside a workflow
context. Outside of a workflow they will raise an Assertion.

The ``conditional`` method returns a new conditional section, that allows to create a - ternary operator like
if-else clauses. The reason why it is called ternary-like is because, it returns the output of the branch result.
So in-effect it is a functional style condition.

Example of a condition usage. Note the nesting and the assignment to a LHS variable

```python
v = (
conditional("fractions")
.if_((my_input > 0.1) & (my_input < 1.0))
.then(
    conditional("inner_fractions")
    .if_(my_input < 0.5)
    .then(double(n=my_input))
    .elif_((my_input > 0.5) & (my_input < 0.7))
    .then(square(n=my_input))
    .else_()
    .fail("Only <0.7 allowed")
)
.elif_((my_input > 1.0) & (my_input < 10.0))
.then(square(n=my_input))
.else_()
.then(double(n=my_input))
)
```

| Parameter | Type | Description |
|-|-|-|
| `name` | `str` | |

#### create_branch_node_promise_var()

```python
def create_branch_node_promise_var(
    node_id: str,
    var: str,
) -> str
```
Generates a globally (wf-level) unique id for a variable.

When building bindings for the branch node, the inputs to the conditions (e.g. (x==5)) need to have variable names
(e.g. x). Because it's currently infeasible to get the name (e.g. x), we resolve to using the referenced node's
output name (e.g. o0, my_out,... etc.). In order to avoid naming collisions (in cases when, for example, the
conditions reference two outputs of two different nodes named the same), we build a variable name composed of the
referenced node name + '.' + the referenced output name. Ideally we use something like
(https://github.com/pwwang/python-varname) to retrieve the assigned variable name (e.g. x). However, because of
https://github.com/pwwang/python-varname/issues/28, this is not currently supported for all AST nodes types.

| Parameter | Type | Description |
|-|-|-|
| `node_id` | `str` | |
| `var` | `str` | |

**Returns:** The generated unique id of the variable.

#### merge_promises()

```python
def merge_promises(
    args: *args,
) -> typing.List[Promise]
```
| Parameter | Type | Description |
|-|-|-|
| `args` | `*args` | |

#### to_branch_node()

```python
def to_branch_node(
    name: str,
    cs: ConditionalSection,
) -> Tuple[BranchNode, typing.List[Promise]]
```
| Parameter | Type | Description |
|-|-|-|
| `name` | `str` | |
| `cs` | `ConditionalSection` | |

#### to_case_block()

```python
def to_case_block(
    c: Case,
) -> Tuple[Union[_core_wf.IfBlock], typing.List[Promise]]
```
| Parameter | Type | Description |
|-|-|-|
| `c` | `Case` | |

#### to_ifelse_block()

```python
def to_ifelse_block(
    node_id: str,
    cs: ConditionalSection,
) -> Tuple[_core_wf.IfElseBlock, typing.List[Binding]]
```
| Parameter | Type | Description |
|-|-|-|
| `node_id` | `str` | |
| `cs` | `ConditionalSection` | |

#### transform_to_boolexpr()

```python
def transform_to_boolexpr(
    expr: Union[ComparisonExpression, ConjunctionExpression],
) -> Tuple[_core_cond.BooleanExpression, typing.List[Promise]]
```
| Parameter | Type | Description |
|-|-|-|
| `expr` | `Union[ComparisonExpression, ConjunctionExpression]` | |

#### transform_to_comp_expr()

```python
def transform_to_comp_expr(
    expr: ComparisonExpression,
) -> Tuple[_core_cond.ComparisonExpression, typing.List[Promise]]
```
| Parameter | Type | Description |
|-|-|-|
| `expr` | `ComparisonExpression` | |

#### transform_to_conj_expr()

```python
def transform_to_conj_expr(
    expr: ConjunctionExpression,
) -> Tuple[_core_cond.ConjunctionExpression, typing.List[Promise]]
```
| Parameter | Type | Description |
|-|-|-|
| `expr` | `ConjunctionExpression` | |

#### transform_to_operand()

```python
def transform_to_operand(
    v: Union[Promise, Literal],
) -> Tuple[_core_cond.Operand, Optional[Promise]]
```
| Parameter | Type | Description |
|-|-|-|
| `v` | `Union[Promise, Literal]` | |

## flytekit.core.condition.BranchNode

### Parameters

```python
class BranchNode(
    name: str,
    ifelse_block: _core_wf.IfElseBlock,
)
```
| Parameter | Type | Description |
|-|-|-|
| `name` | `str` | |
| `ifelse_block` | `_core_wf.IfElseBlock` | |

### Properties

| Property | Type | Description |
|-|-|-|
| `name` | `None` |  |

## flytekit.core.condition.Case

### Parameters

```python
class Case(
    cs: ConditionalSection,
    expr: Optional[Union[ComparisonExpression, ConjunctionExpression]],
    stmt: str,
)
```
| Parameter | Type | Description |
|-|-|-|
| `cs` | `ConditionalSection` | |
| `expr` | `Optional[Union[ComparisonExpression, ConjunctionExpression]]` | |
| `stmt` | `str` | |

### Properties

| Property | Type | Description |
|-|-|-|
| `err` | `None` |  |
| `expr` | `None` |  |
| `output_node` | `None` |  |
| `output_promise` | `None` |  |

### Methods

| Method | Description |
|-|-|
| [`fail()`](#fail) |  |
| [`then()`](#then) |  |

#### fail()

```python
def fail(
    err: str,
) -> Promise
```
| Parameter | Type | Description |
|-|-|-|
| `err` | `str` | |

#### then()

```python
def then(
    p: Union[Promise, Tuple[Promise]],
) -> Optional[Union[Condition, Promise, Tuple[Promise], VoidPromise]]
```
| Parameter | Type | Description |
|-|-|-|
| `p` | `Union[Promise, Tuple[Promise]]` | |

## flytekit.core.condition.Condition

### Parameters

```python
class Condition(
    cs: ConditionalSection,
)
```
| Parameter | Type | Description |
|-|-|-|
| `cs` | `ConditionalSection` | |

### Methods

| Method | Description |
|-|-|
| [`elif_()`](#elif_) |  |
| [`else_()`](#else_) |  |

#### elif_()

```python
def elif_(
    expr: Union[ComparisonExpression, ConjunctionExpression],
) -> Case
```
| Parameter | Type | Description |
|-|-|-|
| `expr` | `Union[ComparisonExpression, ConjunctionExpression]` | |

#### else_()

```python
def else_()
```
## flytekit.core.condition.ConditionalSection

ConditionalSection is used to denote a condition within a Workflow. This default conditional section only works
for Compilation mode. It is advised to derive the class and re-implement the `start_branch` and `end_branch` methods
to override the compilation behavior

&gt; [!NOTE]
&gt; Conditions can only be used within a workflow context.

Usage:

```python
v =  conditional("fractions").if_((my_input > 0.1) & (my_input < 1.0)).then(...)...
```

### Parameters

```python
class ConditionalSection(
    name: str,
)
```
| Parameter | Type | Description |
|-|-|-|
| `name` | `str` | |

### Properties

| Property | Type | Description |
|-|-|-|
| `cases` | `None` |  |
| `name` | `None` |  |

### Methods

| Method | Description |
|-|-|
| [`compute_output_vars()`](#compute_output_vars) | Computes and returns the minimum set of outputs for this conditional block, based on all the cases that have. |
| [`end_branch()`](#end_branch) | This should be invoked after every branch has been visited. |
| [`if_()`](#if_) |  |
| [`start_branch()`](#start_branch) | At the start of an execution of every branch this method should be called. |

#### compute_output_vars()

```python
def compute_output_vars()
```
Computes and returns the minimum set of outputs for this conditional block, based on all the cases that have
been registered

#### end_branch()

```python
def end_branch()
```
This should be invoked after every branch has been visited.
In case this is not local workflow execution then, we should check if this is the last case.
If so then return the promise, else return the condition

#### if_()

```python
def if_(
    expr: Union[ComparisonExpression, ConjunctionExpression],
) -> Case
```
| Parameter | Type | Description |
|-|-|-|
| `expr` | `Union[ComparisonExpression, ConjunctionExpression]` | |

#### start_branch()

```python
def start_branch(
    c: Case,
    last_case: bool,
) -> Case
```
At the start of an execution of every branch this method should be called.

| Parameter | Type | Description |
|-|-|-|
| `c` | `Case` | -&gt; the case that represents this branch |
| `last_case` | `bool` | -&gt; a boolean that indicates if this is the last branch in the ifelseblock |

## flytekit.core.condition.LocalExecutedConditionalSection

### Parameters

```python
class LocalExecutedConditionalSection(
    name: str,
)
```
| Parameter | Type | Description |
|-|-|-|
| `name` | `str` | |

### Properties

| Property | Type | Description |
|-|-|-|
| `cases` | `None` |  |
| `name` | `None` |  |

### Methods

| Method | Description |
|-|-|
| [`compute_output_vars()`](#compute_output_vars) | Computes and returns the minimum set of outputs for this conditional block, based on all the cases that have. |
| [`end_branch()`](#end_branch) | This should be invoked after every branch has been visited. |
| [`if_()`](#if_) |  |
| [`start_branch()`](#start_branch) | At the start of an execution of every branch this method should be called. |

#### compute_output_vars()

```python
def compute_output_vars()
```
Computes and returns the minimum set of outputs for this conditional block, based on all the cases that have
been registered

#### end_branch()

```python
def end_branch()
```
This should be invoked after every branch has been visited
In case of Local workflow execution, we should first mark the branch as complete, then
Then we first check for if this is the last case,
In case this is the last case, we return the output from the selected case - A case should always
be selected (see start_branch)
If this is not the last case, we should return the condition so that further chaining can be done

#### if_()

```python
def if_(
    expr: Union[ComparisonExpression, ConjunctionExpression],
) -> Case
```
| Parameter | Type | Description |
|-|-|-|
| `expr` | `Union[ComparisonExpression, ConjunctionExpression]` | |

#### start_branch()

```python
def start_branch(
    c: Case,
    last_case: bool,
) -> Case
```
At the start of an execution of every branch this method should be called.

| Parameter | Type | Description |
|-|-|-|
| `c` | `Case` | -&gt; the case that represents this branch |
| `last_case` | `bool` | -&gt; a boolean that indicates if this is the last branch in the ifelseblock |

## flytekit.core.condition.SkippedConditionalSection

This ConditionalSection is used for nested conditionals, when the branch has been evaluated to false.
This ensures that the branch is not evaluated and thus the local tasks are not executed.

### Parameters

```python
class SkippedConditionalSection(
    name: str,
)
```
| Parameter | Type | Description |
|-|-|-|
| `name` | `str` | |

### Properties

| Property | Type | Description |
|-|-|-|
| `cases` | `None` |  |
| `name` | `None` |  |

### Methods

| Method | Description |
|-|-|
| [`compute_output_vars()`](#compute_output_vars) | Computes and returns the minimum set of outputs for this conditional block, based on all the cases that have. |
| [`end_branch()`](#end_branch) | This should be invoked after every branch has been visited. |
| [`if_()`](#if_) |  |
| [`start_branch()`](#start_branch) | At the start of an execution of every branch this method should be called. |

#### compute_output_vars()

```python
def compute_output_vars()
```
Computes and returns the minimum set of outputs for this conditional block, based on all the cases that have
been registered

#### end_branch()

```python
def end_branch()
```
This should be invoked after every branch has been visited

#### if_()

```python
def if_(
    expr: Union[ComparisonExpression, ConjunctionExpression],
) -> Case
```
| Parameter | Type | Description |
|-|-|-|
| `expr` | `Union[ComparisonExpression, ConjunctionExpression]` | |

#### start_branch()

```python
def start_branch(
    c: Case,
    last_case: bool,
) -> Case
```
At the start of an execution of every branch this method should be called.

| Parameter | Type | Description |
|-|-|-|
| `c` | `Case` | -&gt; the case that represents this branch |
| `last_case` | `bool` | -&gt; a boolean that indicates if this is the last branch in the ifelseblock |

---
**Source**: https://github.com/unionai/unionai-docs/blob/main/content/api-reference/flytekit-sdk/packages/flytekit.core.condition.md
**HTML**: https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.condition/
