데이터를 지속적으로 변경하고 싶다!!
#기본 함수
def wave_modulating(steps):
step_size = 2 * math.pi / steps
amplitude = yield
for step in range(steps):
radians = step * step_size
fraction = math.sin(radians)
output = amplitude * fraction
amplitude = yield ouput
send메서드 이용
#제너레이터가 yield식에 도달하지 못했기에 send의 첫 입력은 None
def run_modulating(it): amplitudes = [None,7,7,7,2,2,2,10,10,10,10,10] for amlitude in amplitudes: output = it.send(amplitude) transmit(output)
run_modulating(wave_modulating(12))
def complex_wave_modulating(): yield from wave_modulating(3) yield from wave_modulating(4) yield from wave_modulating(5)
run_modulating(complex_wave_modulating())
문제점
해결
wave함수에 이터레이터 전달
def wave_cascading(amplitude_it, steps):
step_size = 2 math.pi / steps for step in range(steps):
radians = step step_size fraction = next(amplitude_it) #다음 입력 받기
합성할 상황이 있다면 yield from
def complex_wave_cascading(amplitude_it):
yield from wave_cascading(amplitude_it, 3) yield from wave_cascading(amplitude_it, 4) ` yield from wave_cascading(amplitude_it, 5)
제너레이터로 넘기기
def run_cascading(): amplitudes = [7,7,7,2,2,2,2,10,10,10,10,10] it = complex_wave_cascading(iter(amplitudes)) for amplitude in amplitudes: output = next(it) transmit(output) run_cascading() ```