Index
async_btree
Declare async btree api.
Attributes
AsyncInnerFunction = Callable[[], Awaitable[Any]]
module-attribute
Function signature of async function implementation.
CallableFunction = Callable[..., Awaitable[Any]] | Callable
module-attribute
Something callable with or without async.
FAILURE = not SUCCESS
module-attribute
Failure constant.
SUCCESS = True
module-attribute
Success constant.
Classes
BTreeRunner
Context manager that runs behavior trees against a configurable async backend.
On __enter__, a snapshot of the caller's ContextVar state is captured via
copy_context(). Each run() call executes in an isolated copy of that snapshot —
mutations inside a run do not escape to the caller and do not accumulate across
successive run() calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Backend
|
async runtime to use — "asyncio" (default), "trio", or "asyncio+uvloop". |
'asyncio'
|
Source code in async_btree/runner.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
Functions
run(target, *args, **kwargs)
Run an async callable to completion using the configured backend.
Must be called within a with BTreeRunner() as runner: block.
Each call runs in an isolated copy of the context captured at __enter__.
ContextVar mutations inside target do not escape to the caller and do not
accumulate across successive run() calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Callable[..., Awaitable[R]]
|
async callable (coroutine function) |
required |
*args
|
Any
|
positional arguments passed to target |
()
|
**kwargs
|
Any
|
keyword arguments passed to target |
{}
|
Returns:
| Type | Description |
|---|---|
R
|
whatever target returns |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if called outside of the context manager |
Source code in async_btree/runner.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
ControlFlowException
Bases: Exception
Wraps an exception to give it falsy meaning without losing the original cause.
Instances are always falsy (bool(e) returns False), so they can be
returned as a FAILURE status while still carrying the original exception.
Source code in async_btree/definition.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
Functions
__init__(exception)
Initialize with the original exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
Exception
|
the original exception to wrap. |
required |
Source code in async_btree/definition.py
67 68 69 70 71 72 73 74 75 | |
instantiate(exception)
classmethod
Return exception unchanged if already a ControlFlowException, otherwise wrap it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
Exception
|
the exception to wrap if needed. |
required |
Returns:
| Type | Description |
|---|---|
ControlFlowException
|
a falsy exception suitable for use as FAILURE. |
Source code in async_btree/definition.py
86 87 88 89 90 91 92 93 94 95 96 | |
Node
Bases: NamedTuple
Resolved snapshot of a behaviour tree node, produced by analyze().
Holds the display name, resolved property values, and resolved child edges for a single node in the abstract tree.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
display name of the node. |
properties |
list[tuple[str, Any]]
|
resolved |
edges |
list[tuple[str, list[Any]]]
|
resolved |
Source code in async_btree/analyze.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
Functions
__str__()
Return the stringified tree representation of this node.
Source code in async_btree/analyze.py
34 35 36 | |
NodeMetadata
Bases: NamedTuple
Metadata attached to a node function describing its name, properties, and child edges.
Used by analyze() and stringify_analyze() to build and display the abstract tree.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
display name of the node. |
properties |
List[str] | None
|
names of scalar attributes to include in the tree view. |
edges |
List[str] | None
|
names of child-bearing attributes. When |
Source code in async_btree/definition.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
Functions
alias(name, node, properties=None)
classmethod
Return a copy of node with a new name and optional property override.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
new display name. |
required |
node
|
NodeMetadata
|
source metadata to copy edges from. |
required |
properties
|
List[str] | None
|
if given, replaces |
None
|
Returns:
| Type | Description |
|---|---|
NodeMetadata
|
new instance with updated name and properties. |
Source code in async_btree/definition.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
Functions
action(target, **kwargs)
Declare an action leaf node.
Wraps target as an awaitable closure. Any exception raised by target
is caught and re-raised as a ControlFlowException, giving it falsy meaning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
CallableFunction
|
sync or async callable to invoke. |
required |
kwargs
|
keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function. |
Raises:
| Type | Description |
|---|---|
ControlFlowException
|
wrapping any exception raised by |
Source code in async_btree/leaf.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
afilter(corofunc, iterable)
async
Filter an iterable or an async iterable with an async function.
This simplify writing of filtering by a function on something iterable between 'async for ...' and 'for...' .
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corofunc
|
Callable[[Any], Awaitable[bool]]
|
filter async function |
required |
iterable
|
Union[AsyncIterable, Iterable]
|
iterable or async iterable collection which will be applied. |
required |
Returns:
| Type | Description |
|---|---|
AsyncGenerator[Any]
|
an async iterator of item which satisfy corofunc(item) == True |
Example
[i async for i in amap(inc, afilter(even, [0, 1, 2, 3, 4]))]
Source code in async_btree/utils.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
alias(child, name)
Define an alias on our child.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
child function to decorate |
required |
name
|
str
|
name of function tree |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function. |
Source code in async_btree/decorator.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
always_failure(child)
Wrap child so the node always returns a falsy value.
If child returns a falsy result, that original result is preserved and returned.
If child returns a truthy result, returns FAILURE instead.
Exceptions are re-raised as ControlFlowException.
Note
To suppress exceptions as well, wrap child with ignore_exception first:
always_failure(child=ignore_exception(myfunction))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to wrap. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns the original falsy child
result unchanged, or |
Raises:
| Type | Description |
|---|---|
ControlFlowException
|
if child raises an exception. |
Source code in async_btree/decorator.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
always_success(child)
Wrap child so the node always returns a truthy value.
If child returns a truthy result, that original result is preserved and returned.
If child returns a falsy result, returns SUCCESS instead.
Exceptions are re-raised as ControlFlowException.
Note
To suppress exceptions as well, wrap child with ignore_exception first:
always_success(child=ignore_exception(myfunction))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to wrap. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns the original truthy child
result unchanged, or |
Raises:
| Type | Description |
|---|---|
ControlFlowException
|
if child raises an exception. |
Source code in async_btree/decorator.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
amap(corofunc, iterable)
async
Map an async function onto an iterable or an async iterable.
This simplify writing of mapping a function on something iterable between 'async for ...' and 'for...' .
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corofunc
|
Callable[[Any], Awaitable[T]]
|
coroutine function |
required |
iterable
|
Union[AsyncIterable, Iterable]
|
iterable or async iterable collection which will be applied. |
required |
Returns:
| Type | Description |
|---|---|
AsyncGenerator[T, None]
|
AsyncGenerator[T]: an async iterator of corofunc(item) |
Example
[i async for i in amap(inc, afilter(even, [0, 1, 2, 3, 4]))]
Source code in async_btree/utils.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
condition(target, **kwargs)
Declare a condition leaf node.
Delegates to is_success(action(target, **kwargs)) — returns SUCCESS if target
is truthy, FAILURE otherwise. Exceptions from target propagate as
ControlFlowException.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
CallableFunction
|
sync or async callable evaluated as a boolean. |
required |
kwargs
|
keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns |
Source code in async_btree/leaf.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
condition_guard(condition, child)
Run child only if condition is truthy; return SUCCESS without running child if condition is falsy.
Semantic shorthand for decision(condition, success_tree=child) with no failure tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
CallableFunction
|
sync or async callable evaluated as a boolean. |
required |
child
|
CallableFunction
|
callable run only when condition is truthy. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns child result when condition
is truthy, or |
Source code in async_btree/control.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
cooldown(child, delay, throttled_value=SUCCESS)
Skip child if called again before delay seconds have elapsed since the last run.
Last-run time is stored in the closure — it persists across BTreeRunner.run() ticks
for the lifetime of this node instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to throttle. |
required |
delay
|
float
|
minimum seconds between successive executions of child. |
required |
throttled_value
|
Any
|
value returned when child is skipped. Defaults to |
SUCCESS
|
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that runs child and returns its result
when the cooldown has elapsed, or |
Source code in async_btree/decorator.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
decision(condition, success_tree, failure_tree=None)
Create a decision node.
If condition is truthy, return evaluation of success_tree.
Otherwise, return evaluation of failure_tree if set, or SUCCESS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
CallableFunction
|
sync or async callable evaluated as a boolean. |
required |
success_tree
|
CallableFunction
|
callable evaluated when condition is truthy. |
required |
failure_tree
|
CallableFunction | None
|
callable evaluated when condition is falsy.
Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function. |
Source code in async_btree/control.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
decorate(child, decorator, **kwargs)
Post-process child result with a decorator function.
Runs child eagerly, then passes the result as the first argument to decorator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to run first. |
required |
decorator
|
CallableFunction
|
sync or async callable with signature
|
required |
kwargs
|
additional keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns |
Source code in async_btree/decorator.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
delay(child, seconds)
Wait seconds before running child.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to run after the delay. |
required |
seconds
|
float
|
number of seconds to wait before executing child. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that sleeps then returns child result. |
Source code in async_btree/decorator.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
do_while(child, condition)
Run child at least once, then repeat while condition is truthy.
Unlike repeat_while, the child always executes at least once before the condition
is checked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable executed each iteration. |
required |
condition
|
CallableFunction
|
sync or async callable checked after each iteration. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns the last child result. |
Source code in async_btree/control.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
fallback(children)
Execute children in sequence and succeed as soon as one succeeds; fail if all fail.
Children are evaluated in order from highest to lowest priority. Evaluation stops
at the first truthy result. selector is an alias for this function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
children
|
list[CallableFunction]
|
list of sync or async callables. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns the first truthy result, or the last falsy result if all children fail. |
Source code in async_btree/control.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
ignore_exception(child)
Wrap child so exceptions are caught and returned as a falsy value instead of propagating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to wrap. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns child result unchanged on
success, or a |
Source code in async_btree/decorator.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
inverter(child)
Invert node status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to invert. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns |
Source code in async_btree/decorator.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
is_failure(child)
Return SUCCESS if child is falsy, FAILURE otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to test. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns |
Source code in async_btree/decorator.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
is_success(child)
Create a conditional node which test if child success.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
child function to decorate |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function which return SUCCESS if child return SUCCESS else FAILURE. |
Source code in async_btree/decorator.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
node_metadata(name=None, properties=None, edges=None)
Decorator that attaches NodeMetadata to a function as __node_metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Optional[str]
|
override display name; defaults to the function name left-stripped of leading underscores. |
None
|
properties
|
Optional[List[str]]
|
names of scalar attributes to expose in the tree view. |
None
|
edges
|
Optional[List[str]]
|
names of child-bearing attributes. When |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[P, R]], FunctionWithMetadata[P, R]]
|
the decorator function. |
Source code in async_btree/definition.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
parallel_race(children)
Run children concurrently; return the result of the first child to finish.
As soon as one child completes, all remaining children are cancelled.
Returns FAILURE if children is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
children
|
list[CallableFunction]
|
list of sync or async callables. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
AsyncInnerFunction |
AsyncInnerFunction
|
an awaitable function that returns the winner's result. |
Raises:
| Type | Description |
|---|---|
ControlFlowException
|
wrapping any exception raised by a child. |
Source code in async_btree/parallele.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
random_selector(children)
Execute children in a random order, succeeding as soon as one succeeds.
Children are reshuffled on every call, so the evaluation order differs each tick.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
children
|
list[CallableFunction]
|
list of sync or async callables. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns |
Source code in async_btree/control.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
repeat_n(child, n)
Run child exactly n times regardless of its result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to run. |
required |
n
|
int
|
number of times to execute child. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns the last child result,
or |
Source code in async_btree/control.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
repeat_until(condition, child)
Repeat child until condition becomes truthy, stopping when condition is met.
Returns the last child evaluation, or FAILURE if condition is truthy on the first check
(no iteration occurs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
CallableFunction
|
sync or async callable evaluated before each iteration. |
required |
child
|
CallableFunction
|
sync or async callable executed each iteration. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function. |
Source code in async_btree/control.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
repeat_while(condition, child)
Repeat child while condition is truthy, stopping when condition becomes falsy.
Returns the last child evaluation, or FAILURE if condition is falsy on the first check.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
CallableFunction
|
sync or async callable evaluated before each iteration. |
required |
child
|
CallableFunction
|
sync or async callable executed each iteration. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function. |
Source code in async_btree/control.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
retry(child, max_retry=3)
Retry child evaluation on failure until it succeeds or max_retry attempts are exhausted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to retry. |
required |
max_retry
|
int
|
maximum number of attempts (default 3). Use |
3
|
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns the first truthy result, or
the last falsy result (which may be a |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if |
Source code in async_btree/decorator.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
retry_until_failed(child)
Retry child until failed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
child function to decorate |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function which try to evaluate child until it failed. |
Source code in async_btree/decorator.py
298 299 300 301 302 303 304 305 306 307 308 309 | |
retry_until_success(child)
Retry child until success.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
child function to decorate |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function which try to evaluate child until it succeed. |
Source code in async_btree/decorator.py
285 286 287 288 289 290 291 292 293 294 295 | |
run(target, *args, backend='asyncio', **kwargs)
Run a behavior tree callable to completion.
Convenience wrapper around BTreeRunner for one-shot execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Callable[..., Awaitable[Any]]
|
async callable (coroutine function) |
required |
*args
|
Any
|
positional arguments passed to target |
()
|
backend
|
Backend
|
async runtime — "asyncio" (default), "trio", or "asyncio+uvloop" |
'asyncio'
|
**kwargs
|
Any
|
keyword arguments passed to target |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
whatever target returns |
Source code in async_btree/utils.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
selector(children)
Alias of fallback. Execute children in sequence and succeed as soon as one succeeds.
Children are evaluated in order from highest to lowest priority. Evaluation stops at the first truthy result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
children
|
list[CallableFunction]
|
list of sync or async callables. |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns the first truthy result, or the last falsy result if all children fail. |
Source code in async_btree/control.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
sequence(children, success_threshold=None)
Return a function which executes children in sequence.
success_threshold generalizes traditional sequence/fallback behaviour and
must be in [0, len(children)]. Defaults to None, which means len(children)
(all children must succeed).
- If #success reaches
success_threshold, returns the list of results collected so far. - If #failure reaches
len(children) - success_threshold + 1, returns the last falsy result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
children
|
list[CallableFunction]
|
list of sync or async callables. |
required |
success_threshold
|
int | None
|
minimum number of children that must succeed.
Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns a list of results on success, or the last falsy result on failure. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if |
Source code in async_btree/control.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
stringify_analyze(target, indent=0, label=None)
Stringify a Node tree into a human-readable indented representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Node
|
node to stringify. |
required |
indent
|
int
|
current indentation level (default 0). |
0
|
label
|
Optional[str]
|
edge label to prefix the node with (default |
None
|
Returns:
| Type | Description |
|---|---|
str
|
indented string representation of the node tree. |
Source code in async_btree/analyze.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
switch(condition, cases, default=None)
Route to a child based on the return value of condition.
Evaluates condition to get a key, looks it up in cases, and runs the
matching child. Falls back to default if no case matches. Returns FAILURE
if no case matches and no default is provided.
The case keys are shown in stringify_analyze output. The children themselves
are not traversed by analyze().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
CallableFunction
|
sync or async callable that returns a hashable key. |
required |
cases
|
dict[Any, CallableFunction]
|
mapping of key → child callable. |
required |
default
|
CallableFunction | None
|
callable run when no case matches. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns the matched child's result,
or |
Source code in async_btree/control.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
timeout_after(child, delay)
Run child with a time limit; return FAILURE if the deadline is exceeded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
CallableFunction
|
sync or async callable to run. |
required |
delay
|
float
|
maximum seconds to wait before returning |
required |
Returns:
| Type | Description |
|---|---|
AsyncInnerFunction
|
an awaitable function that returns child result on
success, or |
Source code in async_btree/decorator.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |