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 or Path 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 StringEqualsPath(variable, value)

Creates a rule with the StringEqualsPath operator.

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

Rule with StringEqualsPath 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 StringLessThanPath(variable, value)

Creates a rule with the StringLessThanPath operator.

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

Rule with StringLessThanPath 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 StringGreaterThanPath(variable, value)

Creates a rule with the StringGreaterThanPath operator.

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

Rule with StringGreaterThanPath 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 StringLessThanEqualsPath(variable, value)

Creates a rule with the StringLessThanEqualsPath operator.

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

Rule with StringLessThanEqualsPath 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 StringGreaterThanEqualsPath(variable, value)

Creates a rule with the StringGreaterThanEqualsPath operator.

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

Rule with StringGreaterThanEqualsPath 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 NumericEqualsPath(variable, value)

Creates a rule with the NumericEqualsPath operator.

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

Rule with NumericEqualsPath 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 NumericLessThanPath(variable, value)

Creates a rule with the NumericLessThanPath operator.

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

Rule with NumericLessThanPath 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 NumericGreaterThanPath(variable, value)

Creates a rule with the NumericGreaterThanPath operator.

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

Rule with NumericGreaterThanPath 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 NumericLessThanEqualsPath(variable, value)

Creates a rule with the NumericLessThanEqualsPath operator.

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

Rule with NumericLessThanEqualsPath 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 NumericGreaterThanEqualsPath(variable, value)

Creates a rule with the NumericGreaterThanEqualsPath operator.

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

Rule with NumericGreaterThanEqualsPath 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 BooleanEqualsPath(variable, value)

Creates a rule with the BooleanEqualsPath operator.

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

Rule with BooleanEqualsPath 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 TimestampEqualsPath(variable, value)

Creates a rule with the TimestampEqualsPath operator.

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

Rule with TimestampEqualsPath 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 TimestampLessThanPath(variable, value)

Creates a rule with the TimestampLessThanPath operator.

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

Rule with TimestampLessThanPath 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 TimestampGreaterThanPath(variable, value)

Creates a rule with the TimestampGreaterThanPath operator.

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

Rule with TimestampGreaterThanPath 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 TimestampLessThanEqualsPath(variable, value)

Creates a rule with the TimestampLessThanEqualsPath operator.

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

Rule with TimestampLessThanEqualsPath 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 TimestampGreaterThanEqualsPath(variable, value)

Creates a rule with the TimestampGreaterThanEqualsPath operator.

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

Rule with TimestampGreaterThanEqualsPath operator.

Return type:

Rule

classmethod IsNull(variable, value)

Creates a rule with the IsNull operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (bool) – Whether the value at variable is equal to the JSON literal null or not.
Returns:

Rule with IsNull operator.

Return type:

Rule

classmethod IsPresent(variable, value)

Creates a rule with the IsPresent operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (bool) – Whether a field at variable exists in the input or not.
Returns:

Rule with IsPresent operator.

Return type:

Rule

classmethod IsString(variable, value)

Creates a rule with the IsString operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (bool) – Whether the value at variable is a string or not.
Returns:

Rule with IsString operator.

Return type:

Rule

classmethod IsNumeric(variable, value)

Creates a rule with the IsNumeric operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (bool) – Whether the value at variable is a number or not.
Returns:

Rule with IsNumeric operator.

Return type:

Rule

classmethod IsTimestamp(variable, value)

Creates a rule with the IsTimestamp operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (bool) – Whether the value at variable is a timestamp or not.
Returns:

Rule with IsTimestamp operator.

Return type:

Rule

classmethod IsBoolean(variable, value)

Creates a rule with the IsBoolean operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (bool) – Whether the value at variable is a boolean or not.
Returns:

Rule with IsBoolean operator.

Return type:

Rule

classmethod StringMatches(variable, value)

Creates a rule with the StringMatches operator.

Parameters:
  • variable (str) – Path to the variable to compare.
  • value (str) – A string pattern that may contain one or more * characters to compare the value at variable to. The * character can be escaped using two backslashes. The comparison yields true if the variable matches the pattern, where * is a wildcard that matches zero or more characters.
Returns:

Rule with StringMatches 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