Skip to content

synt.expr.condition ¤

Condition ¤

Bases: Expression

Conditional expression, aka if - else.

References

expr.ExprPrecedence.Conditional.

Source code in synt/expr/condition.py
class Condition(expr.Expression):
    r"""Conditional expression, aka `if - else`.

    References:
        [expr.ExprPrecedence.Conditional][synt.expr.expr.ExprPrecedence.Conditional].
    """

    condition: expr.Expression
    """Condition expression."""
    true_expr: expr.Expression
    """expr.Expression to evaluate and return if the condition is true."""
    false_expr: expr.Expression
    """expr.Expression to evaluate and return if the condition is false."""
    precedence = expr.ExprPrecedence.Conditional
    expr_type = expr.ExprType.Condition

    def __init__(
        self,
        condition: expr.IntoExpression,
        true_expr: expr.IntoExpression,
        false_expr: expr.IntoExpression,
    ):
        """Initialize a new conditional expression.

        Args:
            condition: Condition expression.
            true_expr: expr.Expression to evaluate and return if the condition is true.
            false_expr: expr.Expression to evaluate and return if the condition is false.
        """
        self.condition = condition.into_expression()
        if self.condition.precedence > self.precedence:
            self.condition = self.condition.wrapped()
        self.true_expr = true_expr.into_expression()
        if self.true_expr.precedence > self.precedence:
            self.true_expr = self.true_expr.wrapped()
        self.false_expr = false_expr.into_expression()
        if self.false_expr.precedence > self.precedence:
            self.false_expr = self.false_expr.wrapped()

    def into_code(self) -> str:
        return f"{self.true_expr.into_code()} if {self.condition.into_code()} else {self.false_expr.into_code()}"

precedence class-attribute instance-attribute ¤

precedence = Conditional

expr_type class-attribute instance-attribute ¤

expr_type = Condition

condition instance-attribute ¤

condition: Expression = into_expression()

Condition expression.

true_expr instance-attribute ¤

true_expr: Expression = into_expression()

expr.Expression to evaluate and return if the condition is true.

false_expr instance-attribute ¤

false_expr: Expression = into_expression()

expr.Expression to evaluate and return if the condition is false.

__init__ ¤

1
2
3
4
5
__init__(
    condition: IntoExpression,
    true_expr: IntoExpression,
    false_expr: IntoExpression,
)

Initialize a new conditional expression.

Parameters:

Name Type Description Default
condition IntoExpression

Condition expression.

required
true_expr IntoExpression

expr.Expression to evaluate and return if the condition is true.

required
false_expr IntoExpression

expr.Expression to evaluate and return if the condition is false.

required
Source code in synt/expr/condition.py
def __init__(
    self,
    condition: expr.IntoExpression,
    true_expr: expr.IntoExpression,
    false_expr: expr.IntoExpression,
):
    """Initialize a new conditional expression.

    Args:
        condition: Condition expression.
        true_expr: expr.Expression to evaluate and return if the condition is true.
        false_expr: expr.Expression to evaluate and return if the condition is false.
    """
    self.condition = condition.into_expression()
    if self.condition.precedence > self.precedence:
        self.condition = self.condition.wrapped()
    self.true_expr = true_expr.into_expression()
    if self.true_expr.precedence > self.precedence:
        self.true_expr = self.true_expr.wrapped()
    self.false_expr = false_expr.into_expression()
    if self.false_expr.precedence > self.precedence:
        self.false_expr = self.false_expr.wrapped()

into_code ¤

into_code() -> str
Source code in synt/expr/condition.py
def into_code(self) -> str:
    return f"{self.true_expr.into_code()} if {self.condition.into_code()} else {self.false_expr.into_code()}"

ConditionBuilder ¤

Builder for Condition.

Source code in synt/expr/condition.py
class ConditionBuilder:
    r"""Builder for [`Condition`][synt.expr.condition.Condition]."""

    __condition: expr.Expression
    __true_expr: expr.Expression
    __false_expr: expr.Expression | None

    def __init__(self, condition: expr.IntoExpression, true_expr: expr.IntoExpression):
        """Initialize an empty condition builder.

        Args:
            condition: Condition expression.
            true_expr: expr.Expression to evaluate if the condition is true.
        """
        self.__condition = condition.into_expression()
        self.__true_expr = true_expr.into_expression()
        self.__false_expr = None

    def false_expr(self, e: expr.IntoExpression) -> Self:
        """Set the expression to evaluate if the condition is false.

        Args:
            e: expr.Expression to evaluate.
        """
        self.__false_expr = e.into_expression()
        return self

    def build(self) -> Condition:
        """Build the condition.

        Raises:
            ValueError: If any of the required field is empty.
        """
        err_fields = []
        if self.__false_expr is None:
            err_fields.append("false_expr")

        if err_fields:
            raise ValueError(
                f"Missing required field(s): {', '.join(f'`{t}`' for t in err_fields)}"
            )
        return Condition(self.__condition, self.__true_expr, self.__false_expr)  # type:ignore[arg-type]

    def else_(self, other: expr.IntoExpression) -> Condition:
        """Set the `false_expr` and build the builder.

        Args:
            other: expr.Expression to evaluate if the condition is false.

        Raises:
            ValueError: If any of the required field is empty.
        """
        self.false_expr(other)
        return self.build()

__init__ ¤

1
2
3
__init__(
    condition: IntoExpression, true_expr: IntoExpression
)

Initialize an empty condition builder.

Parameters:

Name Type Description Default
condition IntoExpression

Condition expression.

required
true_expr IntoExpression

expr.Expression to evaluate if the condition is true.

required
Source code in synt/expr/condition.py
def __init__(self, condition: expr.IntoExpression, true_expr: expr.IntoExpression):
    """Initialize an empty condition builder.

    Args:
        condition: Condition expression.
        true_expr: expr.Expression to evaluate if the condition is true.
    """
    self.__condition = condition.into_expression()
    self.__true_expr = true_expr.into_expression()
    self.__false_expr = None

false_expr ¤

false_expr(e: IntoExpression) -> Self

Set the expression to evaluate if the condition is false.

Parameters:

Name Type Description Default
e IntoExpression

expr.Expression to evaluate.

required
Source code in synt/expr/condition.py
def false_expr(self, e: expr.IntoExpression) -> Self:
    """Set the expression to evaluate if the condition is false.

    Args:
        e: expr.Expression to evaluate.
    """
    self.__false_expr = e.into_expression()
    return self

build ¤

build() -> Condition

Build the condition.

Raises:

Type Description
ValueError

If any of the required field is empty.

Source code in synt/expr/condition.py
def build(self) -> Condition:
    """Build the condition.

    Raises:
        ValueError: If any of the required field is empty.
    """
    err_fields = []
    if self.__false_expr is None:
        err_fields.append("false_expr")

    if err_fields:
        raise ValueError(
            f"Missing required field(s): {', '.join(f'`{t}`' for t in err_fields)}"
        )
    return Condition(self.__condition, self.__true_expr, self.__false_expr)  # type:ignore[arg-type]

else_ ¤

else_(other: IntoExpression) -> Condition

Set the false_expr and build the builder.

Parameters:

Name Type Description Default
other IntoExpression

expr.Expression to evaluate if the condition is false.

required

Raises:

Type Description
ValueError

If any of the required field is empty.

Source code in synt/expr/condition.py
def else_(self, other: expr.IntoExpression) -> Condition:
    """Set the `false_expr` and build the builder.

    Args:
        other: expr.Expression to evaluate if the condition is false.

    Raises:
        ValueError: If any of the required field is empty.
    """
    self.false_expr(other)
    return self.build()