1
+ # validated: 2024-01-23 DS 70b60e3a7465 button/Trigger.java
1
2
from types import SimpleNamespace
2
3
from typing import Callable , overload
3
4
@@ -44,6 +45,7 @@ def __init__(self, loop: EventLoop, condition: Callable[[], bool]):
44
45
45
46
def __init__ (self , * args , ** kwargs ):
46
47
def init_loop_condition (loop : EventLoop , condition : Callable [[], bool ]):
48
+ assert callable (condition )
47
49
self ._loop = loop
48
50
self ._condition = condition
49
51
@@ -77,7 +79,7 @@ def init_condition(condition: Callable[[], bool]):
77
79
1. (self: Trigger)
78
80
2. (self: Trigger, condition: () -> bool)
79
81
3. (self: Trigger, loop: EventLoop, condition: () -> bool)
80
-
82
+
81
83
Invoked with: { format_args_kwargs (self , * args , ** kwargs )}
82
84
"""
83
85
)
@@ -90,8 +92,10 @@ def onTrue(self, command: Command) -> Self:
90
92
:returns: this trigger, so calls can be chained
91
93
"""
92
94
95
+ state = SimpleNamespace (pressed_last = self ._condition ())
96
+
93
97
@self ._loop .bind
94
- def _ (state = SimpleNamespace ( pressed_last = self . _condition ()) ):
98
+ def _ ():
95
99
pressed = self ._condition ()
96
100
if not state .pressed_last and pressed :
97
101
command .schedule ()
@@ -107,8 +111,10 @@ def onFalse(self, command: Command) -> Self:
107
111
:returns: this trigger, so calls can be chained
108
112
"""
109
113
114
+ state = SimpleNamespace (pressed_last = self ._condition ())
115
+
110
116
@self ._loop .bind
111
- def _ (state = SimpleNamespace ( pressed_last = self . _condition ()) ):
117
+ def _ ():
112
118
pressed = self ._condition ()
113
119
if state .pressed_last and not pressed :
114
120
command .schedule ()
@@ -122,14 +128,16 @@ def whileTrue(self, command: Command) -> Self:
122
128
changes to `False`.
123
129
124
130
Doesn't re-start the command if it ends while the condition is still `True`. If the command
125
- should restart, see RepeatCommand.
131
+ should restart, see :class:`commands2. RepeatCommand` .
126
132
127
133
:param command: the command to start
128
134
:returns: this trigger, so calls can be chained
129
135
"""
130
136
137
+ state = SimpleNamespace (pressed_last = self ._condition ())
138
+
131
139
@self ._loop .bind
132
- def _ (state = SimpleNamespace ( pressed_last = self . _condition ()) ):
140
+ def _ ():
133
141
pressed = self ._condition ()
134
142
if not state .pressed_last and pressed :
135
143
command .schedule ()
@@ -145,14 +153,16 @@ def whileFalse(self, command: Command) -> Self:
145
153
condition changes to `True`.
146
154
147
155
Doesn't re-start the command if it ends while the condition is still `False`. If the command
148
- should restart, see RepeatCommand.
156
+ should restart, see :class:`commands2. RepeatCommand` .
149
157
150
158
:param command: the command to start
151
159
:returns: this trigger, so calls can be chained
152
160
"""
153
161
162
+ state = SimpleNamespace (pressed_last = self ._condition ())
163
+
154
164
@self ._loop .bind
155
- def _ (state = SimpleNamespace ( pressed_last = self . _condition ()) ):
165
+ def _ ():
156
166
pressed = self ._condition ()
157
167
if state .pressed_last and not pressed :
158
168
command .schedule ()
@@ -170,8 +180,10 @@ def toggleOnTrue(self, command: Command) -> Self:
170
180
:returns: this trigger, so calls can be chained
171
181
"""
172
182
183
+ state = SimpleNamespace (pressed_last = self ._condition ())
184
+
173
185
@self ._loop .bind
174
- def _ (state = SimpleNamespace ( pressed_last = self . _condition ()) ):
186
+ def _ ():
175
187
pressed = self ._condition ()
176
188
if not state .pressed_last and pressed :
177
189
if command .isScheduled ():
@@ -190,8 +202,10 @@ def toggleOnFalse(self, command: Command) -> Self:
190
202
:returns: this trigger, so calls can be chained
191
203
"""
192
204
205
+ state = SimpleNamespace (pressed_last = self ._condition ())
206
+
193
207
@self ._loop .bind
194
- def _ (state = SimpleNamespace ( pressed_last = self . _condition ()) ):
208
+ def _ ():
195
209
pressed = self ._condition ()
196
210
if state .pressed_last and not pressed :
197
211
if command .isScheduled ():
@@ -212,6 +226,7 @@ def __bool__(self) -> bool:
212
226
return self ._condition ()
213
227
214
228
def __and__ (self , other : Callable [[], bool ]) -> "Trigger" :
229
+ assert callable (other )
215
230
return Trigger (lambda : self () and other ())
216
231
217
232
def and_ (self , other : Callable [[], bool ]) -> "Trigger" :
@@ -224,6 +239,7 @@ def and_(self, other: Callable[[], bool]) -> "Trigger":
224
239
return self & other
225
240
226
241
def __or__ (self , other : Callable [[], bool ]) -> "Trigger" :
242
+ assert callable (other )
227
243
return Trigger (lambda : self () or other ())
228
244
229
245
def or_ (self , other : Callable [[], bool ]) -> "Trigger" :
0 commit comments