Skip to content

Commit 48d08e3

Browse files
committed
ruff format files
1 parent f3ea9c9 commit 48d08e3

File tree

8 files changed

+107
-64
lines changed

8 files changed

+107
-64
lines changed

emuloop.py

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SetDefinition(Enum):
2020
SCAN = 3
2121

2222

23-
def float_or_keep(temp_or_field: str)-> Optional[float]:
23+
def float_or_keep(temp_or_field: str) -> Optional[float]:
2424
"""
2525
Convert the input to a float or None if the input is keep (to allow not changing of a
2626
temperature or field).
@@ -45,7 +45,7 @@ def float_or_keep(temp_or_field: str)-> Optional[float]:
4545
magnet_not_applicable = "N/A"
4646

4747

48-
def magnet_device_type(magnet_device: str)-> str:
48+
def magnet_device_type(magnet_device: str) -> str:
4949
"""
5050
Take the shortened magnet selection input e.g. ZF, LF or TF and cast it to the
5151
required string i.e. Active ZF, Danfysik, T20 Coils.
@@ -69,7 +69,7 @@ def magnet_device_type(magnet_device: str)-> str:
6969
raise ValueError("Magnet device must be one of {} or N/A".format(list(magnet_devices.keys())))
7070

7171

72-
def cast_custom_expression(expression:str)-> str:
72+
def cast_custom_expression(expression: str) -> str:
7373
"""
7474
Ensure a custom python expression is not empty (this will cause an error) by filling it with
7575
None (does nothing).
@@ -87,7 +87,7 @@ def cast_custom_expression(expression:str)-> str:
8787
return expression
8888

8989

90-
def inclusive_float_range_with_step_flip(start: float, stop:float, step:float) -> Generator:
90+
def inclusive_float_range_with_step_flip(start: float, stop: float, step: float) -> Generator:
9191
"""
9292
If we are counting downwards from start to stop automatically flips step to be negative.
9393
Inclusive of stop. Only tested for float values.
@@ -120,9 +120,11 @@ class DoRun(ScriptDefinition):
120120
active_zf = "Active ZF"
121121
possible_magnet_devices = [active_zf, "Danfysik", "T20 Coils"]
122122

123-
def get_help(self)-> str:
124-
return (f"Magnet device must be one of {list(magnet_devices.keys())} or if the field is "
125-
f"KEEP then it can be N/A.\nIf the field is zero magnet device must be ZF.\n")
123+
def get_help(self) -> str:
124+
return (
125+
f"Magnet device must be one of {list(magnet_devices.keys())} or if the field is "
126+
f"KEEP then it can be N/A.\nIf the field is zero magnet device must be ZF.\n"
127+
)
126128

127129
@cast_parameters_to(
128130
start_temperature=float_or_keep,
@@ -144,9 +146,9 @@ def estimate_time(
144146
stop_field: Optional[float] = 1.0,
145147
step_field: float = 1.0,
146148
custom: str = "None",
147-
mevents:float = 10,
149+
mevents: float = 10,
148150
magnet_device: str = "N/A",
149-
)-> int:
151+
) -> int:
150152
return 0
151153

152154
# Loop through a set of temperatures or fields using a start, stop and step mechanism
@@ -163,22 +165,22 @@ def estimate_time(
163165
)
164166
def run(
165167
self,
166-
start_temperature: Optional[float]= "keep", # type: ignore[reportArgumentType],
167-
stop_temperature: Optional[float]= "keep", # type: ignore[reportArgumentType],
168-
step_temperature: float =0,
169-
start_field: Optional[float]= "keep", # type: ignore[reportArgumentType],
170-
stop_field: Optional[float]= "keep", # type: ignore[reportArgumentType],
171-
step_field:float=0,
172-
custom:str="None",
173-
mevents:float=10,
174-
magnet_device:str="N/A",
175-
)-> None:
168+
start_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType],
169+
stop_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType],
170+
step_temperature: float = 0,
171+
start_field: Optional[float] = "keep", # type: ignore[reportArgumentType],
172+
stop_field: Optional[float] = "keep", # type: ignore[reportArgumentType],
173+
step_field: float = 0,
174+
custom: str = "None",
175+
mevents: float = 10,
176+
magnet_device: str = "N/A",
177+
) -> None:
176178
# Scan if start and stop are different, set once if they are equal or do not set if they are
177179
# None
178180
temp_set_definition = self.check_set_definition(start_temperature, stop_temperature)
179181
field_set_definition = self.check_set_definition(start_field, stop_field)
180182
# Use the instrument scripts to set the magnet device correctly
181-
import inst # type:ignore
183+
import inst # type:ignore
182184

183185
if field_set_definition != SetDefinition.UNDEFINED:
184186
self.set_magnet_device(magnet_device, inst)
@@ -210,7 +212,11 @@ def run(
210212
assert start_temperature is not None
211213
assert stop_temperature is not None
212214
self.run_scans(
213-
start_temperature, stop_temperature, step_temperature, mevents, inst.settemp
215+
start_temperature,
216+
stop_temperature,
217+
step_temperature,
218+
mevents,
219+
inst.settemp,
214220
)
215221
elif field_set_definition == SetDefinition.SCAN: # Run scans for the field
216222
assert start_field is not None
@@ -240,11 +246,17 @@ def set_magnet_device(self, magnet_device: str, inst: ModuleType) -> None:
240246
magnet_device (str): The string representation of the magnet device to select.
241247
inst (module): The instrument scripts module to set the magnet device with.
242248
"""
243-
magnet_to_function_map = {"Active ZF": inst.f0, "Danfysik": inst.lf0, "T20 Coils": inst.tf0}
249+
magnet_to_function_map = {
250+
"Active ZF": inst.f0,
251+
"Danfysik": inst.lf0,
252+
"T20 Coils": inst.tf0,
253+
}
244254
if g.cget("a_selected_magnet")["value"] != magnet_device:
245255
magnet_to_function_map[magnet_device]()
246256

247-
def check_set_definition(self, start_temp_or_field: Optional[float], stop_temp_or_field: Optional[float]) -> Enum:
257+
def check_set_definition(
258+
self, start_temp_or_field: Optional[float], stop_temp_or_field: Optional[float]
259+
) -> Enum:
248260
"""
249261
Check if we are running a scan, doing one set (a point) or not setting at all.
250262
@@ -297,7 +309,12 @@ def run_temp_and_field_scans(
297309
self.run_scans(start_field, stop_field, step_field, mevents, inst.setmag)
298310

299311
def run_scans(
300-
self, start: float, stop: float, step: float, mevents: float, set_parameter_func: Callable
312+
self,
313+
start: float,
314+
stop: float,
315+
step: float,
316+
mevents: float,
317+
set_parameter_func: Callable,
301318
) -> None:
302319
"""
303320
Run a scan for the given set_parameter_func
@@ -358,7 +375,9 @@ def parameters_valid(
358375
else:
359376
return None
360377

361-
def check_start_and_stop_valid(self, start: Optional[float], stop: Optional[float], variable_name: str) -> str:
378+
def check_start_and_stop_valid(
379+
self, start: Optional[float], stop: Optional[float], variable_name: str
380+
) -> str:
362381
"""
363382
Check that start and stop are either both None or both values.
364383
@@ -399,7 +418,11 @@ def check_if_start_or_stop_field_are_keep_then_magnet_is_na(
399418
return ""
400419

401420
def check_step_set_correctly(
402-
self, start: Optional[float], stop: Optional[float], step: float, variable_name: str
421+
self,
422+
start: Optional[float],
423+
stop: Optional[float],
424+
step: float,
425+
variable_name: str,
403426
) -> str:
404427
"""
405428
If we are scanning check that the step is positive and not zero.
@@ -426,7 +449,10 @@ def check_step_set_correctly(
426449
return reason
427450

428451
def check_magnet_selected_correctly(
429-
self, start_field: Optional[float], stop_field: Optional[float], magnet_device: str
452+
self,
453+
start_field: Optional[float],
454+
stop_field: Optional[float],
455+
magnet_device: str,
430456
) -> str:
431457
"""
432458
If we are setting a field check:

emulooptime.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ def get_help(self) -> str:
141141
)
142142
def estimate_time(
143143
self,
144-
start_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType]
145-
stop_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType]
144+
start_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType]
145+
stop_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType]
146146
step_temperature: float = 0,
147-
start_field: Optional[float] = "keep", # type: ignore[reportArgumentType]
148-
stop_field: Optional[float] = "keep", # type: ignore[reportArgumentType]
147+
start_field: Optional[float] = "keep", # type: ignore[reportArgumentType]
148+
stop_field: Optional[float] = "keep", # type: ignore[reportArgumentType]
149149
step_field: float = 0,
150150
custom: str = "None",
151151
mevents: float = 10,
@@ -189,11 +189,11 @@ def estimate_time(
189189
)
190190
def run(
191191
self,
192-
start_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType]
193-
stop_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType]
192+
start_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType]
193+
stop_temperature: Optional[float] = "keep", # type: ignore[reportArgumentType]
194194
step_temperature: float = 0,
195-
start_field: Optional[float] = "keep", # type: ignore[reportArgumentType]
196-
stop_field: Optional[float] = "keep", # type: ignore[reportArgumentType]
195+
start_field: Optional[float] = "keep", # type: ignore[reportArgumentType]
196+
stop_field: Optional[float] = "keep", # type: ignore[reportArgumentType]
197197
step_field: float = 0,
198198
custom: str = "None",
199199
mevents: float = 10,
@@ -204,7 +204,7 @@ def run(
204204
temp_set_definition = self.check_set_definition(start_temperature, stop_temperature)
205205
field_set_definition = self.check_set_definition(start_field, stop_field)
206206
# Use the instrument scripts to set the magnet device correctly
207-
import inst # type:ignore
207+
import inst # type:ignore
208208

209209
if field_set_definition != SetDefinition.UNDEFINED:
210210
self.set_magnet_device(magnet_device, inst)
@@ -236,7 +236,11 @@ def run(
236236
assert start_temperature is not None
237237
assert stop_temperature is not None
238238
self.run_scans(
239-
start_temperature, stop_temperature, step_temperature, mevents, inst.settemp
239+
start_temperature,
240+
stop_temperature,
241+
step_temperature,
242+
mevents,
243+
inst.settemp,
240244
)
241245
elif field_set_definition == SetDefinition.SCAN: # Run scans for the field
242246
assert start_field is not None
@@ -266,11 +270,17 @@ def set_magnet_device(self, magnet_device: str, inst: ModuleType) -> None:
266270
magnet_device (str): The string representation of the magnet device to select.
267271
inst (module): The instrument scripts module to set the magnet device with.
268272
"""
269-
magnet_to_function_map = {"Active ZF": inst.f0, "Danfysik": inst.lf0, "T20 Coils": inst.tf0}
273+
magnet_to_function_map = {
274+
"Active ZF": inst.f0,
275+
"Danfysik": inst.lf0,
276+
"T20 Coils": inst.tf0,
277+
}
270278
if g.cget("a_selected_magnet")["value"] != magnet_device:
271279
magnet_to_function_map[magnet_device]()
272280

273-
def check_set_definition(self, start_temp_or_field: Optional[float], stop_temp_or_field: Optional[float]) -> Enum:
281+
def check_set_definition(
282+
self, start_temp_or_field: Optional[float], stop_temp_or_field: Optional[float]
283+
) -> Enum:
274284
"""
275285
Check if we are running a scan, doing one set (a point) or not setting at all.
276286
@@ -323,7 +333,12 @@ def run_temp_and_field_scans(
323333
self.run_scans(start_field, stop_field, step_field, mevents, inst.setmag)
324334

325335
def run_scans(
326-
self, start: float, stop: float, step: float, mevents: float, set_parameter_func: Callable
336+
self,
337+
start: float,
338+
stop: float,
339+
step: float,
340+
mevents: float,
341+
set_parameter_func: Callable,
327342
) -> None:
328343
"""
329344
Run a scan for the given set_parameter_func
@@ -384,8 +399,9 @@ def parameters_valid(
384399
else:
385400
return None
386401

387-
def check_start_and_stop_valid(self, start: Optional[float], stop: Optional[float],
388-
variable_name: str) -> str:
402+
def check_start_and_stop_valid(
403+
self, start: Optional[float], stop: Optional[float], variable_name: str
404+
) -> str:
389405
"""
390406
Check that start and stop are either both None or both values.
391407
@@ -426,7 +442,11 @@ def check_if_start_or_stop_field_are_keep_then_magnet_is_na(
426442
return ""
427443

428444
def check_step_set_correctly(
429-
self, start: Optional[float], stop: Optional[float], step: float, variable_name: str
445+
self,
446+
start: Optional[float],
447+
stop: Optional[float],
448+
step: float,
449+
variable_name: str,
430450
) -> str:
431451
"""
432452
If we are scanning check that the step is positive and not zero.
@@ -453,7 +473,10 @@ def check_step_set_correctly(
453473
return reason
454474

455475
def check_magnet_selected_correctly(
456-
self, start_field: Optional[float], stop_field: Optional[float], magnet_device: str
476+
self,
477+
start_field: Optional[float],
478+
stop_field: Optional[float],
479+
magnet_device: str,
457480
) -> str:
458481
"""
459482
If we are setting a field check:

example_loop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from genie_python.genie_script_generator import ScriptDefinition, cast_parameters_to
66

77

8-
def inclusive_float_range_with_step_flip(start: float, stop: float, step: float)-> Generator:
8+
def inclusive_float_range_with_step_flip(start: float, stop: float, step: float) -> Generator:
99
"""
1010
If we are counting downwards from start to stop automatically flips step to be negative.
1111
Inclusive of stop. Only tested for float values.

test_emu/test_casters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import unittest
22

33
import numpy as np
4+
from hamcrest import assert_that, calling, raises
5+
46
from emuloop import cast_custom_expression as cast_custom_expression_emuloop
57
from emuloop import float_or_keep as float_or_keep_emuloop
68
from emuloop import magnet_device_type as magnet_device_type_emuloop
79
from emuloop import magnet_devices as magnet_devices_emuloop
810
from emulooptime import float_or_keep as float_or_keep_emu
911
from emulooptime import magnet_device_type as magnet_device_type_emu
1012
from emulooptime import magnet_devices as magnet_devices_emu
11-
from hamcrest import assert_that, calling, raises
1213

1314

1415
class TestMagnetCaster(unittest.TestCase):

test_emu/test_emuloop_param_validation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import unittest
22

3-
from emuloop import DoRun, magnet_devices
43
from parameterized import parameterized
54

5+
from emuloop import DoRun, magnet_devices
6+
67
start_stop_must_both_be_keep_error_message = (
78
"If start {0} or stop {0} is keep, the other must also be keep\n"
89
)

test_emu/test_emuloop_run.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ def test_GIVEN_both_temp_field_scans_WHEN_run_THEN_scans_run_once_for_each_set(
3535
inst.lf0.assert_called_once()
3636
self.assertEqual(inst.settemp.call_count, 10)
3737
self.assertEqual(inst.setmag.call_count, 10 * 10)
38-
self.assertEqual(
39-
self.check_mevents_mock.call_count, 10 * 10
40-
)
38+
self.assertEqual(self.check_mevents_mock.call_count, 10 * 10)
4139

4240
@patch.dict("sys.modules", inst=inst)
4341
@patch("six.moves.builtins.eval")
@@ -60,9 +58,7 @@ def test_GIVEN_temp_scan_field_point_WHEN_run_THEN_setmag_called_once_AND_scan_r
6058
inst.lf0.assert_called_once()
6159
self.assertEqual(inst.settemp.call_count, 10)
6260
inst.setmag.assert_called_once()
63-
self.assertEqual(
64-
self.check_mevents_mock.call_count, 10
65-
)
61+
self.assertEqual(self.check_mevents_mock.call_count, 10)
6662

6763
@patch.dict("sys.modules", inst=inst)
6864
@patch("six.moves.builtins.eval")
@@ -85,9 +81,7 @@ def test_GIVEN_field_scan_temp_point_WHEN_run_THEN_settemp_called_once_AND_scan_
8581
inst.tf0.assert_called_once()
8682
inst.settemp.assert_called_once()
8783
self.assertEqual(inst.setmag.call_count, 10)
88-
self.assertEqual(
89-
self.check_mevents_mock.call_count, 10
90-
)
84+
self.assertEqual(self.check_mevents_mock.call_count, 10)
9185

9286
@patch.dict("sys.modules", inst=inst)
9387
@patch("six.moves.builtins.eval")
@@ -133,9 +127,7 @@ def test_GIVEN_one_of_start_stop_field_is_keep_AND_temp_scan_WHEN_run_THEN_setma
133127
inst.tf0.assert_not_called()
134128
self.assertEqual(inst.settemp.call_count, 10)
135129
inst.setmag.assert_not_called()
136-
self.assertEqual(
137-
self.check_mevents_mock.call_count, 10
138-
)
130+
self.assertEqual(self.check_mevents_mock.call_count, 10)
139131

140132
@patch.dict("sys.modules", inst=inst)
141133
@patch("six.moves.builtins.eval")
@@ -181,9 +173,7 @@ def test_GIVEN_one_of_start_stop_temp_is_keep_AND_field_scan_WHEN_run_THEN_sette
181173
inst.tf0.assert_called_once()
182174
self.assertEqual(inst.setmag.call_count, 10)
183175
inst.settemp.assert_not_called()
184-
self.assertEqual(
185-
self.check_mevents_mock.call_count, 10
186-
)
176+
self.assertEqual(self.check_mevents_mock.call_count, 10)
187177

188178
@patch.dict("sys.modules", inst=inst)
189179
@patch("six.moves.builtins.eval")

0 commit comments

Comments
 (0)