libstp.step.logic.defer

Classes

Defer

Defers step creation until execution time.

Run

Runs an arbitrary callable as a step.

Functions

defer(→ Defer)

Defer step construction until execution time.

run(→ Run)

Execute an arbitrary callable as a step.

Module Contents

class libstp.step.logic.defer.Defer(factory: Callable[[libstp.robot.api.GenericRobot], libstp.step.Step])

Bases: libstp.step.Step

Defers step creation until execution time.

The factory receives the robot and returns the step to run. This allows steps to use runtime values (sensor readings, computed results from earlier steps, etc.) that aren’t available at construction time.

factory
class libstp.step.logic.defer.Run(action: Callable[[libstp.robot.api.GenericRobot], None | Awaitable[None]])

Bases: libstp.step.Step

Runs an arbitrary callable as a step.

For simple side effects or computations that don’t produce a child step. The action can be sync or async.

action
libstp.step.logic.defer.defer(factory: Callable[[libstp.robot.api.GenericRobot], libstp.step.Step]) Defer

Defer step construction until execution time.

Wraps a factory callable that receives the robot instance and returns a step. The factory is called when the Defer step executes, not when the step tree is built. This allows steps to depend on runtime values such as sensor readings, odometry data, or results computed by earlier steps in a sequence.

Parameters:

factory – A callable that takes a GenericRobot and returns a Step to execute. Called exactly once when the deferred step runs.

Returns:

A step that lazily constructs and runs its child.

Return type:

Defer

Example:

from libstp.step.logic import defer

# Turn by an angle computed from a sensor reading at runtime
seq([
    scan_step,
    defer(lambda robot: turn_left(
        compute_angle_from_scan(robot)
    )),
])

# Drive a distance based on current odometry position
defer(lambda robot: drive_forward(
    target_x - robot.odometry().getPosition().x
))
libstp.step.logic.defer.run(action: Callable[[libstp.robot.api.GenericRobot], None | Awaitable[None]]) Run

Execute an arbitrary callable as a step.

Wraps a sync or async callable so it can be used inline in a step sequence. This is useful for one-off side effects, logging, variable assignments, or any imperative code that does not warrant its own step class. The callable receives the robot instance and its return value is ignored (unless it returns an awaitable, which is then awaited).

Parameters:

action – A callable that takes a GenericRobot. May be synchronous (returning None) or asynchronous (returning an Awaitable[None]).

Returns:

A step that calls action when executed.

Return type:

Run

Example:

from libstp.step.logic import run

# Emergency stop before turning
seq([
    run(lambda robot: robot.drive.hard_stop()),
    turn_left(90),
])

# Log odometry mid-sequence
run(lambda robot: print(f"Heading: {robot.odometry().getHeading()}"))