I'd like to get your advice for a design. I've got an Oven controlling the temperature and I'm doing some temperature dependent measurements. I'm basically setting the temperature, measure some stuff and move on.
I came up with two designs, simplified of course, which are shown below. The first one uses a callback based approach:
class Oven(object):
# ... some methods
def step_temperature(start, stop, num, rate, callback):
temperatures = np.linspace(start, stop, num)
for t in temperatures:
self.temperature = t, rate # sweep to temperature with given rate
self._wait_for_stability() # wait until temperature is reached.
callback(t) # execute the measurement
# Use Case
oven = Oven()
oven.step_temperature(start=20, stop=200, num=10, rate=1, callback=measure_stuff)
The second design is a generator based design
class Oven(object):
# ... some methods
def step_temperature(start, stop, num, rate):
temperatures = np.linspace(start, stop, num)
for t in temperatures:
self.temperature = t, rate
self._wait_for_stability()
yield t
# Use Case
oven = Oven()
for t in oven.step_temperature(start=20, stop=200, num=10, rate=1):
measure_stuff(t)
I'm tending towards the second design, but I'm interrested in your suggestions. If there is a even better way don't hesitate to tell me.
for x in the_generator(): callback(x)
then I see no reason to use a generator, just call thecallback
inside the method. – Selftaught