Total Complexity | 3 |
Total Lines | 40 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import curio |
||
2 | import async_btree as bt |
||
3 | import contextvars |
||
4 | |||
5 | name = contextvars.ContextVar('name',default="") |
||
6 | |||
7 | def some_action(): |
||
8 | print("continue here...") |
||
9 | |||
10 | async def say_hello(): |
||
11 | print(f"Hello: {name.get()}") |
||
12 | |||
13 | async def is_name_set(): |
||
14 | print("current name is " + name.get()) |
||
15 | return name.get() != "" |
||
16 | |||
17 | |||
18 | |||
19 | greet_with_name = bt.decision(is_name_set, |
||
20 | bt.always_success(child=bt.action(target=say_hello)) |
||
21 | ) |
||
22 | |||
23 | b_tree = bt.sequence( |
||
24 | children=[ |
||
25 | greet_with_name, |
||
26 | bt.always_success(child=bt.action(target=some_action)), |
||
27 | ] |
||
28 | ) |
||
29 | |||
30 | if __name__ == '__main__': |
||
31 | |||
32 | name = contextvars.ContextVar('name') |
||
33 | name.set("Hans") |
||
34 | |||
35 | curio.run(b_tree) |
||
36 | |||
37 | abstract_tree_tree_1 = bt.analyze(b_tree) |
||
38 | # output the tree: |
||
39 | print(bt.stringify_analyze(abstract_tree_tree_1)) |