我想听听你对设计的建议。我有一个控制温度的烤箱,我正在做一些与温度相关的测量。我基本上是在设置温度,测量一些东西然后继续前进。
我想出了两个设计,当然是简化的,如下所示。第一个使用基于回调的方法:
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)
第二种设计是基于生成器的设计
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)
我倾向于第二种设计,但我对你的建议很感兴趣。如果有更好的方法,请不要犹豫告诉我。