Choice Rules

This module defines the choice rules for a Choice state.

Use the add_choice() method to add a branch to a Choice step.

my_choice_state.add_choice(
    rule=ChoiceRule.BooleanEquals(variable=previous_state.output()["Success"], value=True),
    next_step=happy_path
)
my_choice_state.add_choice(
    ChoiceRule.BooleanEquals(variable=previous_state.output()["Success"], value=False),
    next_step=sad_state
)

In this example, choice rules are added to the Choice state my_choice_state using add_choice(). Logic in a Choice state is implemented with the help of Choice Rules. A Choice Rule encapsulates a comparison, which contains the following:

  • An input variable to compare
  • The type of comparison
  • The value to compare the variable to

The type of comparison is abstracted by the classes provided in this module. Multiple choice rules can be compounded together using the And() or Or() classes. A choice rule can be negated using the Not() class.

class stepfunctions.steps.choice_rule.BaseRule

Bases: object

Abstract class for rules.

class stepfunctions.steps.choice_rule.Rule(variable, operator, value)

Bases: stepfunctions.steps.choice_rule.BaseRule

Class for creating a rule.

Parameters:
  • variable (str) – Path to the variable to compare.
  • operator (str) – Comparison operator to be applied.
  • value (type depends on operator) – Constant value to compare variable against.
Raises:
  • ValueError – If variable doesn’t start with ‘$’
  • ValueError – If value is not the appropriate datatype for the operator specified.
class stepfunctions.steps.choice_rule.CompoundRule(operator, rules)

Bases: stepfunctions.steps.choice_rule.BaseRule

Class for creating a compound rule.

Parameters:
  • operator (str) – Compounding operator to be applied.
  • rules (list(BaseRule)) – List of rules to compound together.
Raises:

ValueError – If any item in the rules list is not a BaseRule object.

class stepfunctions.steps.choice_rule.NotRule(rule)

Bases: stepfunctions.steps.choice_rule.BaseRule

Class for creating a negation rule.

Parameters:rules (BaseRule) – Rule to negate.
Raises:ValueError – If rule is not a BaseRule object.
class stepfunctions.steps.choice_rule.ChoiceRule

Bases: object

Factory class for creating a choice rule.

classmethod StringEquals(variable, value)

Creates a rule with the StringEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with StringEquals operator.

Return type:

Rule

classmethod StringLessThan(variable, value)

Creates a rule with the StringLessThan operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with StringLessThan operator.

Return type:

Rule

classmethod StringGreaterThan(variable, value)

Creates a rule with the StringGreaterThan operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with StringGreaterThan operator.

Return type:

Rule

classmethod StringLessThanEquals(variable, value)

Creates a rule with the StringLessThanEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with StringLessThanEquals operator.

Return type:

Rule

classmethod StringGreaterThanEquals(variable, value)

Creates a rule with the StringGreaterThanEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with StringGreaterThanEquals operator.

Return type:

Rule

classmethod NumericEquals(variable, value)

Creates a rule with the NumericEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (int) – Constant value to compare variable against.
Returns:

Rule with NumericEquals operator.

Return type:

Rule

classmethod NumericLessThan(variable, value)

Creates a rule with the NumericLessThan operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (int) – Constant value to compare variable against.
Returns:

Rule with NumericLessThan operator.

Return type:

Rule

classmethod NumericGreaterThan(variable, value)

Creates a rule with the NumericGreaterThan operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (int) – Constant value to compare variable against.
Returns:

Rule with NumericGreaterThan operator.

Return type:

Rule

classmethod NumericLessThanEquals(variable, value)

Creates a rule with the NumericLessThanEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (int) – Constant value to compare variable against.
Returns:

Rule with NumericLessThanEquals operator.

Return type:

Rule

classmethod NumericGreaterThanEquals(variable, value)

Creates a rule with the NumericGreaterThanEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (int) – Constant value to compare variable against.
Returns:

Rule with NumericGreaterThanEquals operator.

Return type:

Rule

classmethod BooleanEquals(variable, value)

Creates a rule with the BooleanEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (bool) – Constant value to compare variable against.
Returns:

Rule with BooleanEquals operator.

Return type:

Rule

classmethod TimestampEquals(variable, value)

Creates a rule with the TimestampEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with TimestampEquals operator.

Return type:

Rule

classmethod TimestampLessThan(variable, value)

Creates a rule with the TimestampLessThan operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with TimestampLessThan operator.

Return type:

Rule

classmethod TimestampGreaterThan(variable, value)

Creates a rule with the TimestampGreaterThan operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with TimestampGreaterThan operator.

Return type:

Rule

classmethod TimestampLessThanEquals(variable, value)

Creates a rule with the TimestampLessThanEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with TimestampLessThanEquals operator.

Return type:

Rule

classmethod TimestampGreaterThanEquals(variable, value)

Creates a rule with the TimestampGreaterThanEquals operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – Constant value to compare variable against.
Returns:

Rule with TimestampGreaterThanEquals operator.

Return type:

Rule

classmethod And(rules)

Creates a compound rule with the And operator.

Parameters:rules (list(BaseRule)) – List of rules to compound together.
Returns:Compound rule with And operator.
Return type:CompoundRule
classmethod Or(rules)

Creates a compound rule with the Or operator.

Parameters:rules (list(BaseRule)) – List of rules to compound together.
Returns:Compound rule with Or operator.
Return type:CompoundRule
classmethod Not(rule)

Creates a negation for a rule.

Parameters:rule (BaseRule) – Rule to Negate.
Returns:Rule with Not operator.
Return type:NotRule