decorator
async_btree.decorator
Decorator module define all decorator function node.
Attributes
Classes
Functions
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |