Total Complexity | 7 |
Total Lines | 68 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | """Tutorial 1 code. |
||
2 | |||
3 | |||
4 | This sample should print: |
||
5 | Hello: John |
||
6 | battery ok |
||
7 | battery dbl check |
||
8 | GripperInterface Open |
||
9 | approach_object: house |
||
10 | GripperInterface Close |
||
11 | |||
12 | """ |
||
13 | import curio |
||
14 | import async_btree as bt |
||
15 | |||
16 | |||
17 | async def approach_object(name: str): |
||
18 | print(f"approach_object: {name}") |
||
19 | |||
20 | |||
21 | def check_battery(): |
||
22 | print("battery ok") |
||
23 | # you should return a success |
||
24 | return bt.SUCCESS |
||
25 | |||
26 | |||
27 | def check_again_battery(): |
||
28 | print("battery dbl check") |
||
29 | # you should return a success |
||
30 | return bt.SUCCESS |
||
31 | |||
32 | |||
33 | async def say_hello(name: str): |
||
34 | # This method should be used with bt.always_success decorator |
||
35 | # no return as q falsy meaning |
||
36 | print(f"Hello: {name}") |
||
37 | |||
38 | |||
39 | class GripperInterface: |
||
40 | def __init__(self): |
||
41 | self._open = False |
||
42 | |||
43 | def open(self): |
||
44 | print("GripperInterface Open") |
||
45 | self._open = True |
||
46 | |||
47 | def close(self): |
||
48 | print("GripperInterface Close") |
||
49 | self._open = False |
||
50 | |||
51 | |||
52 | gripper = GripperInterface() |
||
53 | |||
54 | b_tree = bt.sequence( |
||
55 | children=[ |
||
56 | bt.always_success(child=bt.action(target=say_hello, name="John")), |
||
57 | bt.action(target=check_battery), |
||
58 | check_again_battery, # this will be encapsulated at runtime |
||
59 | bt.always_success(child=bt.action(target=gripper.open)), |
||
60 | bt.always_success(child=bt.action(target=approach_object, name="house")), |
||
61 | bt.always_success(child=bt.action(target=gripper.close)), |
||
62 | ] |
||
63 | ) |
||
64 | |||
65 | |||
66 | if __name__ == '__main__': |
||
67 | curio.run(b_tree) |
||
68 |