Skip to content

Commit 63d2e1e

Browse files
committed
Pulled get steps out as utility function
1 parent 51d3eb9 commit 63d2e1e

File tree

5 files changed

+27
-33
lines changed

5 files changed

+27
-33
lines changed

emuloop.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from genie_python import genie as g
77
from genie_python.genie_script_generator import ScriptDefinition, cast_parameters_to
88

9+
from script_utilities import get_steps
10+
911

1012
class SetDefinition(Enum):
1113
"""
@@ -104,16 +106,7 @@ def inclusive_float_range_with_step_flip(start: float, stop: float, step: float)
104106
>>> inclusive_float_range_with_step_flip(0.5, 2, 0.5) == [0.5, 1, 1.5, 2]
105107
>>> inclusive_float_range_with_step_flip(2, 0.5, 0.5) == [2, 1.5, 1, 0.5]
106108
"""
107-
modulo = abs(stop - start) % abs(step)
108-
if stop > start:
109-
vstop = stop - modulo
110-
else:
111-
vstop = stop + modulo
112-
for i in np.linspace(start, vstop, int(abs(vstop - start) / abs(step)) + 1):
113-
if ((i >= start) and (i <= stop)) or (
114-
(i >= stop) and (i <= start)
115-
): # Check inserted here to ensure scan remains within defined range
116-
yield i
109+
yield from get_steps(start, step, stop)
117110

118111

119112
class DoRun(ScriptDefinition):

emulooptime.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from genie_python import genie as g
77
from genie_python.genie_script_generator import ScriptDefinition, cast_parameters_to
88

9+
from script_utilities import get_steps
10+
911

1012
class SetDefinition(Enum):
1113
"""
@@ -104,16 +106,7 @@ def inclusive_float_range_with_step_flip(start: float, stop: float, step: float)
104106
>>> inclusive_float_range_with_step_flip(0.5, 2, 0.5) == [0.5, 1, 1.5, 2]
105107
>>> inclusive_float_range_with_step_flip(2, 0.5, 0.5) == [2, 1.5, 1, 0.5]
106108
"""
107-
modulo = abs(stop - start) % abs(step)
108-
if stop > start:
109-
vstop = stop - modulo
110-
else:
111-
vstop = stop + modulo
112-
for i in np.linspace(start, vstop, int(abs(vstop - start) / abs(step)) + 1):
113-
if ((i >= start) and (i <= stop)) or (
114-
(i >= stop) and (i <= start)
115-
): # Check inserted here to ensure scan remains within defined range
116-
yield i
109+
yield from get_steps(start, step, stop)
117110

118111

119112
class DoRun(ScriptDefinition):

example_loop.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import Generator, Optional
22

3-
import numpy as np
43
from genie_python import genie as g
54
from genie_python.genie_script_generator import ScriptDefinition, cast_parameters_to
65

6+
from script_utilities import get_steps
7+
78

89
def inclusive_float_range_with_step_flip(start: float, stop: float, step: float) -> Generator:
910
"""
@@ -23,16 +24,7 @@ def inclusive_float_range_with_step_flip(start: float, stop: float, step: float)
2324
>>> inclusive_float_range_with_step_flip(2, 0.5, 0.5) == [2, 1.5, 1, 0.5]
2425
"""
2526
# Get the modulo so we know to stop early like arrange if the steps don't fit evenly.
26-
modulo = abs(stop - start) % abs(step)
27-
if stop > start:
28-
vstop = stop - modulo
29-
else:
30-
vstop = stop + modulo
31-
for i in np.linspace(start, vstop, int(abs(vstop - start) / abs(step)) + 1):
32-
if ((i >= start) and (i <= stop)) or (
33-
(i >= stop) and (i <= start)
34-
): # Check inserted here to ensure scan remains within defined range
35-
yield i
27+
yield from get_steps(start, step, stop)
3628

3729

3830
class DoRun(ScriptDefinition):

script_utilities.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Generator
2+
3+
import numpy as np
4+
5+
6+
def get_steps(start: float, step: float, stop: float) -> Generator[float, None, None]:
7+
modulo = abs(stop - start) % abs(step)
8+
if stop > start:
9+
vstop = stop - modulo
10+
else:
11+
vstop = stop + modulo
12+
for i in np.linspace(start, vstop, int(abs(vstop - start) / abs(step)) + 1):
13+
if ((i >= start) and (i <= stop)) or (
14+
(i >= stop) and (i <= start)
15+
): # Check inserted here to ensure scan remains within defined range
16+
yield i

test_emu/test_casters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import unittest
22

3-
import numpy as np
43
from hamcrest import assert_that, calling, raises
54

65
from emuloop import cast_custom_expression as cast_custom_expression_emuloop
@@ -10,6 +9,7 @@
109
from emulooptime import float_or_keep as float_or_keep_emu
1110
from emulooptime import magnet_device_type as magnet_device_type_emu
1211
from emulooptime import magnet_devices as magnet_devices_emu
12+
from script_utilities import get_steps
1313

1414

1515
class TestMagnetCaster(unittest.TestCase):
@@ -82,7 +82,7 @@ def test_GIVEN_keep_different_cases_WHEN_cast_THEN_return_none(self):
8282
self.assertIsNone(float_or_keep_emuloop(keep.upper()))
8383

8484
def test_GIVEN_string_convertable_to_float_WHEN_cast_THEN_return_casted_value(self):
85-
for float_val in np.linspace(4.0, 5.0, int(abs(4.0 - 5.0) / 0.2) + 1):
85+
for float_val in get_steps(4.0, 0.2, 5.0):
8686
self.assertEqual(float_or_keep_emu(str(float_val)), float_val)
8787
self.assertEqual(float_or_keep_emuloop(str(float_val)), float_val)
8888

0 commit comments

Comments
 (0)