5
5
"""
6
6
7
7
import contextlib
8
- from datetime import datetime , timedelta
8
+ from datetime import UTC , datetime , timedelta
9
9
from time import sleep
10
- from typing import Any , Callable , Iterator , TypedDict
10
+ from typing import Any , Iterator , Protocol , TypedDict
11
11
12
+ import numpy as np
12
13
import numpy .typing as npt
13
14
14
15
from genie_python .genie_api_setup import (
21
22
from genie_python .utilities import check_break
22
23
23
24
25
+ class PrePostCmd (Protocol ):
26
+ def __call__ (self , ** kwargs : Any ) -> str | None :
27
+ pass
28
+
29
+
24
30
@usercommand
25
31
@helparglist ("" )
26
32
def get_manager_mode () -> bool :
@@ -52,7 +58,7 @@ def assert_in_manager_mode() -> None:
52
58
53
59
54
60
@contextlib .contextmanager
55
- def motor_in_set_mode (pv_name : str ) -> Iterator :
61
+ def motor_in_set_mode (pv_name : str ) -> Iterator [ None ] :
56
62
"""
57
63
Uses a context to place motor into set mode and ensure that it leaves
58
64
set mode after context has ended. If it can not set the mode correctly
@@ -134,7 +140,7 @@ def get_pv_from_block(block: str) -> str:
134
140
@usercommand
135
141
@helparglist ("pv str" )
136
142
@log_command_and_handle_exception
137
- def pv_exists (pv : str , is_local : bool = False ) -> None :
143
+ def pv_exists (pv : str , is_local : bool = False ) -> bool :
138
144
"""
139
145
Check if PV exists.
140
146
@@ -159,21 +165,21 @@ def wait_for_pv(
159
165
value: The value to wait for
160
166
maxwait (int, optional): The maximum time to wait for in seconds
161
167
"""
162
- start_time = datetime .utcnow ( )
168
+ start_time = datetime .now ( UTC )
163
169
while True :
164
170
curr_value = __api .get_pv_value (pv )
165
171
if curr_value == value :
166
172
break
167
173
if maxwait is not None :
168
- if timedelta (seconds = maxwait ) < datetime .utcnow ( ) - start_time :
174
+ if timedelta (seconds = maxwait ) < datetime .now ( UTC ) - start_time :
169
175
break
170
176
sleep (DELAY_IN_WAIT_FOR_SLEEP_LOOP )
171
177
check_break (2 )
172
178
173
179
174
180
@usercommand
175
181
@helparglist ("" )
176
- def set_begin_precmd (begin_precmd : Callable [[ Any ], str | None ] ) -> None :
182
+ def set_begin_precmd (begin_precmd : PrePostCmd ) -> None :
177
183
"""
178
184
Set the function to call before the begin command.
179
185
@@ -186,7 +192,7 @@ def set_begin_precmd(begin_precmd: Callable[[Any], str | None]) -> None:
186
192
187
193
@usercommand
188
194
@helparglist ("" )
189
- def set_begin_postcmd (begin_postcmd : Callable [[ Any ], str | None ] ) -> None :
195
+ def set_begin_postcmd (begin_postcmd : PrePostCmd ) -> None :
190
196
"""
191
197
Set the function to call after the begin command.
192
198
@@ -198,7 +204,7 @@ def set_begin_postcmd(begin_postcmd: Callable[[Any], str | None]) -> None:
198
204
199
205
@usercommand
200
206
@helparglist ("" )
201
- def set_abort_precmd (abort_precmd : Callable [[ Any ], str | None ] ) -> None :
207
+ def set_abort_precmd (abort_precmd : PrePostCmd ) -> None :
202
208
"""
203
209
Set the function to call before the abort command.
204
210
@@ -210,7 +216,7 @@ def set_abort_precmd(abort_precmd: Callable[[Any], str | None]) -> None:
210
216
211
217
@usercommand
212
218
@helparglist ("" )
213
- def set_abort_postcmd (abort_postcmd : Callable [[ Any ], str | None ] ) -> None :
219
+ def set_abort_postcmd (abort_postcmd : PrePostCmd ) -> None :
214
220
"""
215
221
Set the function to call after the abort command.
216
222
@@ -222,7 +228,7 @@ def set_abort_postcmd(abort_postcmd: Callable[[Any], str | None]) -> None:
222
228
223
229
@usercommand
224
230
@helparglist ("" )
225
- def set_end_precmd (end_precmd : Callable [[ Any ], str | None ] ) -> None :
231
+ def set_end_precmd (end_precmd : PrePostCmd ) -> None :
226
232
"""
227
233
Set the function to call before the end command.
228
234
@@ -234,7 +240,7 @@ def set_end_precmd(end_precmd: Callable[[Any], str | None]) -> None:
234
240
235
241
@usercommand
236
242
@helparglist ("" )
237
- def set_end_postcmd (end_postcmd : Callable [[ Any ], str | None ] ) -> None :
243
+ def set_end_postcmd (end_postcmd : PrePostCmd ) -> None :
238
244
"""
239
245
Set the function to call after the end command.
240
246
@@ -246,7 +252,7 @@ def set_end_postcmd(end_postcmd: Callable[[Any], str | None]) -> None:
246
252
247
253
@usercommand
248
254
@helparglist ("" )
249
- def set_pause_precmd (pause_precmd : Callable [[ Any ], str | None ] ) -> None :
255
+ def set_pause_precmd (pause_precmd : PrePostCmd ) -> None :
250
256
"""
251
257
Set the function to call before the pause command.
252
258
@@ -258,7 +264,7 @@ def set_pause_precmd(pause_precmd: Callable[[Any], str | None]) -> None:
258
264
259
265
@usercommand
260
266
@helparglist ("" )
261
- def set_pause_postcmd (pause_postcmd : Callable [[ Any ], str | None ] ) -> None :
267
+ def set_pause_postcmd (pause_postcmd : PrePostCmd ) -> None :
262
268
"""
263
269
Set the function to call after the pause command.
264
270
@@ -270,7 +276,7 @@ def set_pause_postcmd(pause_postcmd: Callable[[Any], str | None]) -> None:
270
276
271
277
@usercommand
272
278
@helparglist ("" )
273
- def set_resume_precmd (resume_precmd : Callable [[ Any ], str | None ] ) -> None :
279
+ def set_resume_precmd (resume_precmd : PrePostCmd ) -> None :
274
280
"""
275
281
Set the function to call before the resume command.
276
282
@@ -282,7 +288,7 @@ def set_resume_precmd(resume_precmd: Callable[[Any], str | None]) -> None:
282
288
283
289
@usercommand
284
290
@helparglist ("" )
285
- def set_resume_postcmd (resume_postcmd : Callable [[ Any ], str | None ] ) -> None :
291
+ def set_resume_postcmd (resume_postcmd : PrePostCmd ) -> None :
286
292
"""
287
293
Set the function to call after the resume command.
288
294
@@ -400,7 +406,9 @@ def get_exp_data(
400
406
@usercommand
401
407
@helparglist ("" )
402
408
@log_command_and_handle_exception
403
- def get_spectrum_data (with_spec_zero : bool = True , with_time_bin_zero : bool = False ) -> npt .NDArray :
409
+ def get_spectrum_data (
410
+ with_spec_zero : bool = True , with_time_bin_zero : bool = False
411
+ ) -> npt .NDArray [np .float32 ]:
404
412
"""
405
413
Get the event mode spectrum data as ND array.
406
414
0 commit comments