Skip to main content

Useful behaviors built on top of the py_trees library.

Project description

py_branches

py_branches provides higher-level functionality designed to sit on top of the py_trees library. It extends py_trees with reusable behaviors and decorators for common patterns such as alternating execution, probabilistic selection, blackboard-driven conditionals, and time-based pausing.

Installation

From PyPI:

pip install py_branches

From source (editable):

git clone https://github.com/snwu1996/py_branches.git
cd py_branches
pip install -e .

Modules

Module Description
alternating Cycle through behaviors in fixed patterns or run a child every N ticks
blackboard Read/write/gate behaviors based on py_trees blackboard variables
pause Time-based pauses — uniform random duration or YAML-defined schedules
random Probabilistic behavior execution and weighted random selectors

Basic Usage

Alternating — cycle through behaviors

import py_trees
from py_branches.alternating import run_alternating

a = py_trees.behaviours.Success(name="A")
b = py_trees.behaviours.Success(name="B")
c = py_trees.behaviours.Success(name="C")

# Run A for 3 ticks, then B for 2 ticks, then C for 4 ticks, then repeat
root = run_alternating("Alternating", [a, b, c], [3, 2, 4])

Alternating — run a child every N ticks

from py_branches.alternating import RunEveryX, RunEveryRange

child = py_trees.behaviours.Success(name="Child")

# Run child every 5th tick
every_5 = RunEveryX(child, name="Every5", every_x_range=(5, 5))

# Run child only on iterations 4–6 out of every 10
windowed = RunEveryRange(child, name="Window", max_range=10, run_range=(4, 6))

Blackboard — conditional execution and variable management

import py_trees
from py_branches.blackboard import (
    IncrementBlackboardVariable,
    RunIfBlackboardVariableEquals,
    SetBlackboardVariableIfCondition,
)

# Set up blackboard
py_trees.blackboard.Blackboard.enable_activity_stream()
client = py_trees.blackboard.Client(name="setup")
client.register_key("counter", access=py_trees.common.Access.WRITE)
client.counter = 0

# Increment a blackboard counter each tick
increment = IncrementBlackboardVariable(
    name="Increment", variable_name="counter", increment_by=1
)

# Only run a child behavior when counter == 5
child = py_trees.behaviours.Success(name="AtFive")
gate = RunIfBlackboardVariableEquals(
    child, name="RunAt5", variable_name="counter", equals=5
)

Pause — random duration pause

from py_branches.pause import PauseUniform

# Pause for a random duration between 1.0 and 3.0 seconds
pause = PauseUniform(name="RandomPause", low=1.0, high=3.0)

Pause — schedule-based pause

from py_branches.pause import load_schedule_file, CheckPauseSchedule, PauseSchedule

schedule = load_schedule_file("configs/schedules/example_schedule.yaml")

# Returns SUCCESS when the current time falls inside a scheduled window
check = CheckPauseSchedule(name="CheckSchedule", schedule=schedule)

# Pauses until the current scheduled window ends
pause = PauseSchedule(name="PauseSchedule", schedule=schedule)

Random — probabilistic execution

import py_trees
from py_branches.random import RandomRun, random_selector

child = py_trees.behaviours.Success(name="Child")

# Execute child with 70% probability; return FAILURE otherwise
maybe = RandomRun(child, name="Maybe", probability=0.7)

# Weighted random selector: a=20%, b=30%, c=50%
a = py_trees.behaviours.Success(name="A")
b = py_trees.behaviours.Success(name="B")
c = py_trees.behaviours.Success(name="C")
selector = random_selector("WeightedSel", [a, b, c], [0.2, 0.3, 0.5])

Running Tests

pytest tests/

Documentation

Detailed documentation for each module is in the docs/ folder:

License

BSD License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_branches-1.2.1.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_branches-1.2.1-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file py_branches-1.2.1.tar.gz.

File metadata

  • Download URL: py_branches-1.2.1.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.13 Linux/6.17.0-1008-azure

File hashes

Hashes for py_branches-1.2.1.tar.gz
Algorithm Hash digest
SHA256 0f47ce3113a9df8ba9d180d26d79c9e598d35890a992b886b87529d4e606b27c
MD5 91ef4ead0b4beae412bf6c9d37127270
BLAKE2b-256 abe00058bd2a287e28ec20221ce2c2a0fb2ba24824dd84f99fd69490f4e924ce

See more details on using hashes here.

File details

Details for the file py_branches-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: py_branches-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.13 Linux/6.17.0-1008-azure

File hashes

Hashes for py_branches-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1823eac4135379ec20a0c670feb1a6d4854b615dc6aa4b1337cabde0931fac1f
MD5 0afb58196ce0510afcb7ce35eaa12f5a
BLAKE2b-256 2ed872e2b871d15bd94ec1b79ed8264b33c1718158eb084e70e20dd2e645440d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page